Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Draw animatable drawable to view.
public class LoadingView extends View {
private CircularProgressDrawable mDrawable;
public LoadingView(Context pContext) {
super(pContext);
init();
}
public LoadingView(Context pContext, AttributeSet pAttributeSet) {
super(pContext, pAttributeSet);
init();
}
public LoadingView(Context pContext, AttributeSet pAttributeSet, int pDefaultStyle) {
super(pContext, pAttributeSet, pDefaultStyle);
init();
}
private void init() {
mDrawable = new CircularProgressDrawable(getContext());
mDrawable.setStyle(CircularProgressDrawable.DEFAULT);
mDrawable.setCallback(this);
}
@Override
protected void onVisibilityChanged(View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
if (visibility == VISIBLE) {
mDrawable.start();
} else {
mDrawable.stop();
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mDrawable.setBounds(0, 0, w, h);
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
mDrawable.draw(canvas);
}
@Override
protected boolean verifyDrawable(@NonNull Drawable who) {
return who == mDrawable || super.verifyDrawable(who);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.