Android 自定义View实现动画形式加载环形图

先看效果图(图片是gif,比较卡顿):

通过调用postInvalidateDelayed()延时绘制的方法,可以实现以上图片中的延时效果,下面来看具体代码:

1、自定义View类:

public class MyProgressView extends View {
    private Paint circlePaint;  //圆环的画笔
    private Paint bgPaint;      //环背景的画笔
    private RectF circleRectF;  //圆环依据的矩阵
    private int ringRadius=50;     //环的半径
    private int drawNums=60;   //绘制的次数,用来实现动画效果
    private int totalAngle=360; //总共的角度
    private int drawedAngle=0;  //已经绘制的角度
    private float percentDraw=0;    //需要绘制的百分比

    public MyProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(percentDraw==0)return;
        //画圆环的背景色
        canvas.drawArc(circleRectF,0,360,false,bgPaint);
        if(drawedAngle

2、在Activity中使用该自定义View:

public class MainActivity extends AppCompatActivity {
    private MyProgressView myProgressView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView(){
        myProgressView=findViewById(R.id.progress);
        myProgressView.setStyles(200,100,0.65f);
        //myProgressView.setStyles(200,100, Color.BLUE,Color.parseColor("#E1FFFF"),0.65f);
    }
}

3、最后是布局文件:



    

 

你可能感兴趣的:(Android,JAVA)