Source code
package android.support.v7.widget;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.graphics.drawable.shapes.Shape;
import android.support.v4.graphics.drawable.DrawableWrapper;
import android.support.v7.internal.widget.TintManager;
import android.support.v7.internal.widget.TintTypedArray;
import android.util.AttributeSet;
import android.widget.ProgressBar;
class AppCompatProgressBarHelper {
private static final int[] TINT_ATTRS = new int[]{16843067, 16843068};
private Bitmap mSampleTile;
final TintManager mTintManager;
private final ProgressBar mView;
AppCompatProgressBarHelper(ProgressBar view, TintManager tintManager) {
this.mView = view;
this.mTintManager = tintManager;
}
void loadFromAttributes(AttributeSet attrs, int defStyleAttr) {
TintTypedArray a = TintTypedArray.obtainStyledAttributes(this.mView.getContext(), attrs, TINT_ATTRS, defStyleAttr, 0);
Drawable drawable = a.getDrawableIfKnown(0);
if (drawable != null) {
this.mView.setIndeterminateDrawable(tileifyIndeterminate(drawable));
}
drawable = a.getDrawableIfKnown(1);
if (drawable != null) {
this.mView.setProgressDrawable(tileify(drawable, false));
}
a.recycle();
}
private Drawable tileify(Drawable drawable, boolean clip) {
if (drawable instanceof DrawableWrapper) {
Drawable inner = ((DrawableWrapper) drawable).getWrappedDrawable();
if (inner != null) {
((DrawableWrapper) drawable).setWrappedDrawable(tileify(inner, clip));
}
} else if (drawable instanceof LayerDrawable) {
int i;
LayerDrawable background = (LayerDrawable) drawable;
int N = background.getNumberOfLayers();
Drawable[] outDrawables = new Drawable[N];
for (i = 0; i < N; i++) {
int id = background.getId(i);
Drawable drawable2 = background.getDrawable(i);
boolean z = id == 16908301 || id == 16908303;
outDrawables[i] = tileify(drawable2, z);
}
Drawable newBg = new LayerDrawable(outDrawables);
for (i = 0; i < N; i++) {
newBg.setId(i, background.getId(i));
}
return newBg;
} else if (drawable instanceof BitmapDrawable) {
Bitmap tileBitmap = ((BitmapDrawable) drawable).getBitmap();
if (this.mSampleTile == null) {
this.mSampleTile = tileBitmap;
}
Drawable shapeDrawable = new ShapeDrawable(getDrawableShape());
shapeDrawable.getPaint().setShader(new BitmapShader(tileBitmap, TileMode.REPEAT, TileMode.CLAMP));
if (clip) {
shapeDrawable = new ClipDrawable(shapeDrawable, 3, 1);
}
return shapeDrawable;
}
return drawable;
}
private Drawable tileifyIndeterminate(Drawable drawable) {
if (!(drawable instanceof AnimationDrawable)) {
return drawable;
}
AnimationDrawable background = (AnimationDrawable) drawable;
int N = background.getNumberOfFrames();
Drawable newBg = new AnimationDrawable();
newBg.setOneShot(background.isOneShot());
for (int i = 0; i < N; i++) {
Drawable frame = tileify(background.getFrame(i), true);
frame.setLevel(10000);
newBg.addFrame(frame, background.getDuration(i));
}
newBg.setLevel(10000);
return newBg;
}
private Shape getDrawableShape() {
return new RoundRectShape(new float[]{5.0f, 5.0f, 5.0f, 5.0f, 5.0f, 5.0f, 5.0f, 5.0f}, null, null);
}
Bitmap getSampleTime() {
return this.mSampleTile;
}
}