android 繪制 特殊進度條

實現(xiàn)這個效果的進度條:

1586332466(1).png
  • 話不多說上代碼,有問題聯(lián)系我

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

public class TestView extends View {

    private int width;
    private int height;

    private Paint paint;
    private Path path;

    private Paint progressPaint;
    private Path progressPath;

    private int progress=0;

    public void setProgress(int progress) {
        this.progress = progress;
        invalidate();
    }

    public TestView(Context context) {
        this(context,null);
    }

    public TestView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public TestView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        path = new Path();
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(5);
        paint.setColor(Color.parseColor("#FFFFFF"));
        paint.setStyle(Paint.Style.FILL);


        progressPath = new Path();
        progressPaint = new Paint();
        progressPaint.setAntiAlias(true);
        progressPaint.setStrokeWidth(5);
        progressPaint.setColor(Color.parseColor("#ED254D"));
        progressPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width =getMeasuredWidth();
        height = getMeasuredHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        float magin = 20;
        //繪制背景
        path.moveTo(1,magin);
        path.lineTo(1,height-magin);
        path.lineTo(width,height);
        path.lineTo(width,0);
        paint.setStrokeWidth(3);
        path.close();
        canvas.drawPath(path,paint);

        //繪制進度
        float progressWidth= (float) (width-width*(progress/100.0));
        float progressY = (float) (magin*(progress/100.0));

        progressPath.moveTo(progressWidth,progressY);
        progressPath.lineTo(progressWidth,height-progressY);
        progressPath.lineTo(width,height);
        progressPath.lineTo(width,0);
        progressPaint.setStrokeWidth(3);
        progressPath.close();
        canvas.drawPath(progressPath,progressPaint);
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容