美文网首页
android 绘制 特殊进度条

android 绘制 特殊进度条

作者: 今天天气很好嗯 | 来源:发表于2020-04-08 15:54 被阅读0次

    实现这个效果的进度条:

    1586332466(1).png
    • 话不多说上代码,有问题联系我
    
    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);
        }
    }
    

    相关文章

      网友评论

          本文标题:android 绘制 特殊进度条

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