美文网首页
简易进度条

简易进度条

作者: 层林尽染lr | 来源:发表于2018-05-11 13:20 被阅读0次

    话不多少,上代码:

    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

    相关文章

      网友评论

          本文标题:简易进度条

          本文链接:https://www.haomeiwen.com/subject/tsjfdftx.html