Android加载动画系列——BalloonLoading
已经好久没有写博客了呢~,下文的源码并非小编原创,而是来自github上的一位大神,小编只是偶然在微博上看到了一篇文章,小编被gif中华丽的效果吸引啦!小编想自己动手敲一敲,做个笔记什么的,也方便以后万一遇到这样的需求就不需要到处去找资源,在此小编奉上原文地址:https://github.com/dinuscxj/LoadingDrawable
小编本着利人利己的原则来写这篇文章,恳请各位读者老爷不要鄙视小编(可怜状~)。
让我们先来看看效果图:
小编对Paint的使用不熟,所以只能简单的把源码贴出来供大家阅读。
1、LoadingDrawable.java源码如下:
public class LoadingDrawableextends Drawable implementsAnimatable {
private LoadingRenderermLoadingRender;
private final Callback mCallback= newCallback() {
@Override
public void invalidateDrawable(Drawabled) {
invalidateSelf();
}
@Override
public void scheduleDrawable(Drawabled, Runnable what,long when) {
scheduleSelf(what, when);
}
@Override
public void unscheduleDrawable(Drawabled, Runnable what) {
unscheduleSelf(what);
}
};
public LoadingDrawable(LoadingRendererloadingRender) {
this.mLoadingRender= loadingRender;
this.mLoadingRender.setCallback(mCallback);
}
@Override
public void draw(Canvascanvas) {
mLoadingRender.draw(canvas,getBounds());
}
@Override
public void setAlpha(intalpha) {
mLoadingRender.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFiltercf) {
mLoadingRender.setColorFilter(cf);
}
@Override
public int getOpacity(){
return PixelFormat.TRANSLUCENT;
}
@Override
public void start() {
mLoadingRender.start();
}
@Override
public void stop() {
mLoadingRender.stop();
}
@Override
public boolean isRunning(){
return mLoadingRender.isRunning();
}
@Override
public int getIntrinsicHeight(){
return (int) (mLoadingRender.getHeight()+1);
}
@Override
public int getIntrinsicWidth(){
return (int) (mLoadingRender.getWidth()+1);
}
}
2、抽象类LoadingRenderer源码如下:
public abstract class LoadingRenderer { private static final long ANIMATION_DURATION = 1333; private static final float DEFAULT_SIZE = 56.0f; private static final float DEFAULT_CENTER_RADIUS = 12.5f; private static final float DEFAULT_STROKE_WIDTH = 2.5f; protected float mWidth; protected float mHeight; protected float mStrokeWidth; protected float mCenterRadius; private long mDuration; private Drawable.Callback mCallback; private ValueAnimator mRenderAnimator; public LoadingRenderer(Context context) { setupDefaultParams(context); setupAnimators(); } public abstract void draw(Canvas canvas, Rect bounds); public abstract void computeRender(float renderProgress); public abstract void setAlpha(int alpha); public abstract void setColorFilter(ColorFilter cf); public abstract void reset(); public void start() { reset(); setDuration(mDuration); mRenderAnimator.start(); } public void stop() { mRenderAnimator.cancel(); } public boolean isRunning() { return mRenderAnimator.isRunning(); } public void setCallback(Drawable.Callback callback) { this.mCallback = callback; } protected void invalidateSelf() { mCallback.invalidateDrawable(null); } private void setupDefaultParams(Context context) { final DisplayMetrics metrics = context.getResources().getDisplayMetrics(); final float screenDensity = metrics.density; mWidth = DEFAULT_SIZE * screenDensity; mHeight = DEFAULT_SIZE * screenDensity; mStrokeWidth = DEFAULT_STROKE_WIDTH * screenDensity; mCenterRadius = DEFAULT_CENTER_RADIUS * screenDensity; mDuration =