Skip to content

Commit

Permalink
Drawee: start/stop Animatable progress bars
Browse files Browse the repository at this point in the history
  • Loading branch information
plamenko authored and tyronen committed Apr 6, 2015
1 parent e20f44d commit 427e744
Showing 1 changed file with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
Expand Down Expand Up @@ -407,14 +408,21 @@ private void setProgress(float progress) {
if (mProgressBarImageIndex < 0) {
return;
}
// display indefinite progressbar when not fully loaded, hide otherwise
Drawable progressBarDrawable = getLayerChildDrawable(mProgressBarImageIndex);
// display progressbar when not fully loaded, hide otherwise
if (progress >= 0.999f) {
if (progressBarDrawable instanceof Animatable) {
((Animatable) progressBarDrawable).stop();
}
fadeOutLayer(mProgressBarImageIndex);
} else {
if (progressBarDrawable instanceof Animatable) {
((Animatable) progressBarDrawable).start();
}
fadeInLayer(mProgressBarImageIndex);
}
// set drawable level, scaled to [0, 10000] per drawable specification
mFadeDrawable.getDrawable(mProgressBarImageIndex).setLevel(Math.round(progress * 10000));
progressBarDrawable.setLevel(Math.round(progress * 10000));
}

// SettableDraweeHierarchy interface
Expand Down Expand Up @@ -490,12 +498,13 @@ public void setControllerOverlay(@Nullable Drawable drawable) {
// Helper methods for accessing layers

/**
* Returns the parent drawable at the specified index.
* <p>
* If MatrixDrawable or ScaleTypeDrawable is found at that index, it will be returned as a parent.
* Otherwise, the FadeDrawable will be returned.
* Gets the drawable at the specified index while skipping MatrixDrawable and ScaleTypeDrawable.
*
* <p> If <code>returnParent</code> is set, parent drawable will be returned instead. If
* MatrixDrawable or ScaleTypeDrawable is found at that index, it will be returned as a parent.
* Otherwise, the FadeDrawable will be returned as a parent.
*/
private Drawable findLayerParent(int index) {
private Drawable getLayerDrawable(int index, boolean returnParent) {
Drawable parent = mFadeDrawable;
Drawable child = mFadeDrawable.getDrawable(index);
if (child instanceof MatrixDrawable) {
Expand All @@ -506,7 +515,7 @@ private Drawable findLayerParent(int index) {
parent = child;
child = parent.getCurrent();
}
return parent;
return returnParent ? parent : child;
}

/**
Expand All @@ -527,18 +536,25 @@ private Drawable findLayerParent(int index) {
/**
* Sets a child drawable at the specified index.
*
* <p> Note: This uses {@code findLayerParent} to find the parent drawable. Given drawable is
* <p> Note: This uses {@link #getLayerDrawable} to find the parent drawable. Given drawable is
* then set as its child.
*/
private void setLayerChildDrawable(int index, Drawable drawable) {
Drawable parent = findLayerParent(index);
Drawable parent = getLayerDrawable(index, true /* returnParent */);
if (parent == mFadeDrawable) {
mFadeDrawable.setDrawable(index, drawable);
} else {
((ForwardingDrawable) parent).setCurrent(drawable);
}
}

/**
* Gets the child drawable at the specified index.
*/
private Drawable getLayerChildDrawable(int index) {
return getLayerDrawable(index, false /* returnParent */);
}

private Drawable getEmptyPlaceholderDrawable() {
if (mEmptyPlaceholderDrawable == null) {
mEmptyPlaceholderDrawable = new ColorDrawable(Color.TRANSPARENT);
Expand Down

0 comments on commit 427e744

Please sign in to comment.