简易进度条

话不多少,上代码:

public class MyProgressView extends View {

private Paint mPant;
private Paint textPant;
private String color = "#3F51B5";

private RectF rectF;
private int nowAngle = 0;

private Path path;

//动画是否完成
private boolean circleFinished = false;


public MyProgressView(Context context) {
    super(context, null);
    intView();
}

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

public MyProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    intView();
}

private void intView() {
    textPant = new Paint();
    textPant.setColor(Color.parseColor(color));
    textPant.setStrokeWidth(1);
    textPant.setStyle(Paint.Style.FILL);
    textPant.setTextAlign(Paint.Align.CENTER);
    
    mPant = new Paint();
    mPant.setColor(Color.parseColor(color));
    mPant.setStrokeWidth(10);
    mPant.setStyle(Paint.Style.STROKE);

    path = new Path();

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            int min=(getHeight() > getWidth() ? getWidth() : getHeight())/2;

            int startX = (min / 2 / 2);
            int startY = (min / 2 / 2);
            path.moveTo(startX, startY);
            for (int i = 0; i < (min / 2 / 2); i++) {
                path.lineTo(startX + 1, startY + 1);
                startX = startX + 1;
                startY = startY + 1;
            }

            for (int i = 0; i < (min / 2); i++) {
                path.lineTo(startX + i + 5, startY - i);
            }
        }
    }, 400);
}


/**
 * 设置进度
 */
public void setPencent(int pencent) {
    nowAngle = (pencent * 360 / 100);
    circleFinished = false;
    invalidate();

    if (nowAngle == 360) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                int min=(getHeight() > getWidth() ? getWidth() : getHeight())/2;
                ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 1.2f, 1.0f, 1.2f, min / 2, min / 2);
                scaleAnimation.setDuration(300);
                MyProgressView.this.startAnimation(scaleAnimation);
                circleFinished = true;
            }
        }, 200);
    }
}

/**
 * 设置颜色
 *
 * @param color
 */
public void setColor(String color) {
    this.color = color;
    mPant.setColor(Color.parseColor(color));
    textPant.setColor(Color.parseColor(color));
    invalidate();
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    
    int min=(getHeight() > getWidth() ? getWidth() : getHeight())/2;
    //画圆环
    rectF = new RectF(10,10,min ,min);
    canvas.drawArc(rectF, -90, nowAngle, false, mPant);


    //画文本
    if (nowAngle == 0) {
        textPant.setTextSize(min / 8);
        canvas.drawText("点击下载", min/2, min/2, textPant);
    }else if(!circleFinished){
        textPant.setTextSize(min / 8);
        canvas.drawText(((nowAngle * 100) / 360) + "%", min / 2, min / 2, textPant);
    }
    

    //画√
    if (circleFinished) {
        canvas.drawPath(path, mPant);
    }
}

}

调用setPencent直接设置进度,完成后有个缩放动画

image.png
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,564评论 25 709
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,752评论 0 17
  • 快乐幸福的童年 我兴致勃勃的帮助妈妈干活了,我先去厨房拿了块抹布,擦起了茶机,左擦擦右擦擦,擦完了茶几我又去拿笤帚...
    知足常乐朵儿阅读 3,928评论 0 3
  • 回到家,父亲就给我安排了个活,帮他朋友看看项目。我心想,老家大都传统生意,重度依赖关系人脉,我能做什么呢? 这人确...
    心甲阅读 1,033评论 1 0
  • 她想她不懂这栋楼的构造, 人们都说 她年纪太小 一把火就可以纵情燃烧 ---唐八妹
    关馨仁阅读 1,586评论 0 1