看看效果

Demo效果圖
總體的實(shí)現(xiàn)思路
- 用一個(gè)View(ImageView)容器來呈現(xiàn)動(dòng)畫
- 給ImageView設(shè)置一個(gè)Drawable(在這個(gè)drawable里面實(shí)現(xiàn)所有的繪制邏輯)
- 實(shí)現(xiàn)動(dòng)畫的效果的核心就是,當(dāng)ImageView調(diào)用Drawable的draw(系統(tǒng)自動(dòng)調(diào)用)方法的時(shí)候再次調(diào)用ImageView的重繪方法就實(shí)現(xiàn)了動(dòng)畫效果了
看看這個(gè)demo的實(shí)現(xiàn)
- 重寫了一個(gè)ImageView僅僅就是為了設(shè)置一個(gè)Drawable,不用重寫也可以。
public class LoadingView extends ImageView{
public LoadingView(Context context) {
super(context);
setImageDrawable(new LoadingDrawable(this));
}
public LoadingView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
setImageDrawable(new LoadingDrawable(this));
}
public LoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setImageDrawable(new LoadingDrawable(this));
}
}
- 核心就是這個(gè)LoadingDrawable,直接看draw方法吧
@Override
public void draw(@NonNull Canvas canvas) {
//默認(rèn)從左到右
int width = canvas.getWidth();
int height = canvas.getHeight();
int centerX = width / 2;
int centerY = height / 2;
//canvas.drawCircle(centerX,centerY,width / 2 - 8,paint);
for(int i=0;i < lineModels.size();i++){
drawLine(canvas,lineModels.get(i));
}
//updatedata
contannier.invalidate();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void drawLine(Canvas canvas, LineModel lineModel){
int width = canvas.getWidth() / 2;
int height = canvas.getHeight() / 2;
paint.setColor(lineModel.color);
canvas.drawArc(
width - lineModel.raudis - strokeWidth,height - lineModel.raudis - strokeWidth,
width + lineModel.raudis + strokeWidth,height + lineModel.raudis + strokeWidth,
lineModel.start,lineModel.end,
false,paint
);
lineModel.updata();
}
- 這里的**drawLine**就是具體的繪制過程,并且在繪制完成之后更新了下次需要繪制的各種信息
- 繪制完成之后調(diào)用了 **ImageView**的**invalidate**方法進(jìn)行循環(huán)繪制
實(shí)現(xiàn)思路來自
Nothing is certain in this life. The only thing i know for sure is that. I love you and my life. That is the only thing i know. have a good day
:)