【安卓】Android自定义View环形进度条,适用于网络加载Loding

版权声明:我是Extends-- https://blog.csdn.net/qq_25955641/article/details/88397534

先上图,再说话:

 然后是核心类的编写

public class ProgressView extends View {
    private Context context;
    private Paint paint;
    private AlertDialog dialog;
    private String text;
    private int progress = 0;
    private int Angle;
    private int incolor;
    private int procolor;
    private int roundwidth = 20;
    private int i=1;

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            setProgress(i);
            setText(getProgress()+"%");
            if(i<100){
                add();
                i++;
            }else {
                i=0;
                dialog.dismiss();
            }
        }
    };

    private void add() {
        handler.sendEmptyMessageDelayed(1,30);
    }

    public ProgressView(Context context) {
        super(context);
        this.context = context;
        init(context);
    }

    public void show() {
        dialog.show();
        dialog.setCanceledOnTouchOutside(false);
        Window window = dialog.getWindow();
        window.setBackgroundDrawableResource(R.color.tm);
        window.setLayout(300,300);
        WindowManager.LayoutParams pl = window.getAttributes();
        pl.dimAmount = 0.2f;
        window.setAttributes(pl);
        add();
    }

    public void dismiss() {
        dialog.dismiss();
    }


    private void init(Context context) {
        paint = new Paint();
        dialog = new AlertDialog.Builder(context).setView(this).create();
        incolor = getResources().getColor(R.color.colorPrimary);
        procolor = getResources().getColor(R.color.colorAccent);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int center = getWidth()/2;
        int redius = (center - roundwidth /2 );

        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(roundwidth);
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);
        canvas.drawCircle(center,center,redius-roundwidth,paint);
        canvas.save();

        paint.setColor(incolor);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(roundwidth);
        paint.setStyle(Paint.Style.STROKE);
        redius = redius - roundwidth;
        canvas.drawCircle(center,center,redius,paint);

        paint.setColor(procolor);
        RectF rectF = new RectF(center-redius,center-redius,
                center+redius,center+redius);
        canvas.drawArc(rectF,-90,Angle,false,paint);

        canvas.save();

        paint.setStyle(Paint.Style.FILL);
        canvas.translate(center,center);
        canvas.rotate(Angle-90);
        canvas.translate(redius,0);
        canvas.drawCircle(0,0,roundwidth,paint);
        canvas.restore();

        canvas.translate(center,center);
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(0);
        if(!TextUtils.isEmpty(text)){
            paint.setTypeface(Typeface.DEFAULT_BOLD);
            paint.setTextSize(redius/text.length());
            float tw = paint.measureText(text);
            canvas.drawText(text,-tw/2,redius/text.length()/2,paint);
        }

    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
        Angle = 360 * progress/100;
        invalidate();
    }
}

 再加上简单的调用

ProgressView pv = new ProgressView(this);
pv.show();//显示
pv.dismiss();//销毁

猜你喜欢

转载自blog.csdn.net/qq_25955641/article/details/88397534