Android下载进度圆自定义View实现

项目开发即时通讯发送小视频功能,就想着要做一个跟微信那样的下载进度圆

效果图

Android下载进度圆自定义View实现_第1张图片
进度圆

自定义View:

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.RectF;

import android.os.Build;

import android.util.AttributeSet;

import android.view.View;

/**

* 作者:wangjian

* 时间:2018/4/8 10:04

* 作用:用于小视频加载进度圆

*/

public class LoadingCircleView extends View {

    private PaintpaintBgCircle;            //背景圆

    private PaintpaintCircle;              //填充圆

    private PaintpaintProgressCircle;      //进度圆

    private float startAngle = -90f;        //开始角度

    private float sweepAngle =0;          //进度角度

    private int progressCirclePadding =0;  //进度圆与背景圆的间距

    private boolean fillIn =false;        //进度圆是否填充

    public LoadingCircleView(Context context) {

        super(context);

        init();

    }

    public LoadingCircleView(Context context, AttributeSet attrs) {

        super(context, attrs);

        init();

    }

    public LoadingCircleView(Context context, AttributeSet attrs, int defStyleAttr) {

        super(context, attrs, defStyleAttr);

        init();

    }

    public LoadingCircleView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {

        super(context, attrs, defStyleAttr, defStyleRes);

        init();

    }

    private void init() {

        progressCirclePadding = dip2px(getContext(), 3);

        paintBgCircle =new Paint();

        paintBgCircle.setAntiAlias(true);

        paintBgCircle.setStyle(Paint.Style.FILL);

        paintBgCircle.setColor(Color.WHITE);


        paintCircle =new Paint();

        paintCircle.setAntiAlias(true);

        paintCircle.setStyle(Paint.Style.FILL);

        paintCircle.setColor(Color.BLACK);


        paintProgressCircle =new Paint();

        paintProgressCircle.setAntiAlias(true);

        paintProgressCircle.setStyle(Paint.Style.FILL);

        paintProgressCircle.setColor(Color.WHITE);

    }

    @Override

    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);

        canvas.drawCircle(

                getMeasuredWidth() /2,

                getMeasuredWidth() /2,

                getMeasuredWidth() /2,

                paintBgCircle);

        canvas.drawCircle(

                getMeasuredWidth() /2,

                getMeasuredWidth() /2,

                getMeasuredWidth() /2 -progressCirclePadding /2,

                paintCircle);

        RectF rf =new RectF(

                progressCirclePadding,

                progressCirclePadding,

                getMeasuredWidth() -progressCirclePadding,

                getMeasuredWidth() -progressCirclePadding);

        canvas.drawArc(

                rf,

                startAngle,

                sweepAngle,

                true,

                paintProgressCircle);

        if (!fillIn) {

            canvas.drawCircle(

                    getMeasuredWidth() /2,

                    getMeasuredWidth() /2,

                    getMeasuredWidth() /2 -progressCirclePadding *2,

                    paintCircle);

        }

    }

    public void startAnimAutomatic(boolean fillIn) {

        setProgress(0, fillIn);

    }

    public void setProgress(int progress, boolean fillIn) {

        this.fillIn = fillIn;

        sweepAngle = (float) (360 /100.0 * progress);

        invalidate();

    }

    public int dip2px(Context context, float dpValue) {

        final float scale = context.getResources().getDisplayMetrics().density;

        return (int) (dpValue * scale +0.5f);

    }

}

有了自定义View,需要在点击下载的时候把 view给显示出来,然后在下载的线程的回调中使用自定义view的

setProgress(progress, fillIn)方法,progress是文件下载的进度,fillIn是绘制的进度圆是否填充

计算文件下载进度方式请参考我的另一篇文章 Android使用HttpGet下载文件计算下载进度

你可能感兴趣的:(Android下载进度圆自定义View实现)