栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > Java

Android 自定义View 渐变进度条

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android 自定义View 渐变进度条

 渐变进度条?绘制两个圆角矩形不就完事了?

特殊情况 1. 进度过小   矩形的宽度小于高度  绘制的圆角就是一个竖着的长条  无法达到想要的效果。

解决方案 : 使用Path绘制求交集

 当宽度小于高度时  绘制的进度条左移

可自行抽取自定义属性  这里只记录一个简单实现

public class LinearProgressView extends View {
    private int mStartColor;//渐变开始颜色
    private int mEndColor;//渐变结束颜色
    private float mProgress = 100;//进度条总进度
    private int mbgColor;//背景边框颜色
    private float mCurrentProgress;//当前进度
    private int mRadius;
    private Paint mPaint;
    private LinearGradient gradient;
    private Path path;
    private Path pathBg;
    private Path pathPro;
    private RectF rectF;

    public LinearProgressView(Context context) {
        super(context, null);
    }

    public LinearProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs, 0);
        mStartColor = SystemUtil.getColor(R.color.linear_pro_start_color);
        mEndColor = SystemUtil.getColor(R.color.linear_pro_end_color);
        mbgColor = SystemUtil.getColor(R.color.linear_pro_bg_bolder_color);
        mRadius = SystemUtil.dp2px(10);
        init();
    }

    public LinearProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mStartColor = SystemUtil.getColor(R.color.linear_pro_start_color);
        mEndColor = SystemUtil.getColor(R.color.linear_pro_end_color);
        mbgColor = SystemUtil.getColor(R.color.linear_pro_bg_bolder_color);
        mRadius = SystemUtil.dp2px(10);
        init();
    }

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setAntiAlias(true);
        path = new Path();
        pathBg = new Path();
        pathPro = new Path();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    public void setCurrentProgressValue(float currentProgressValue, float allValue) {
        mCurrentProgress = currentProgressValue;
        mProgress = allValue;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (rectF == null) {
            rectF = new RectF();
        }
        //绘制背景
        mPaint.setColor(mbgColor);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(SystemUtil.dp2px(1));
        rectF.left = 0;
        rectF.bottom = getHeight();
        rectF.right = getWidth();
        rectF.top = 0;
        pathBg.addRoundRect(rectF, mRadius, mRadius, Path.Direction.CW);
        canvas.drawPath(pathBg, mPaint);
        //绘制进度条
        if (gradient == null) {
            gradient = new LinearGradient(0, getHeight() / 2, getWidth(), getHeight() / 2,
                     mStartColor, mEndColor , Shader.TileMode.MIRROR);
        }
        mPaint.setShader(gradient);
        mPaint.setStyle(Paint.Style.FILL);
        float progressValue = 0;
        if (mProgress != 0) {
            progressValue = mCurrentProgress / mProgress * getWidth();
        }
        if (progressValue>getWidth()){
            progressValue = getWidth();
        }
        rectF.bottom = getHeight();
        rectF.left = progressValue < getHeight() ? progressValue - getHeight() : 0;
        rectF.right = progressValue;
        rectF.top = 0;
        pathPro.addRoundRect(rectF, mRadius, mRadius, Path.Direction.CW);
        path.op(pathBg, pathPro, Path.Op.INTERSECT);
        canvas.drawPath(path, mPaint);
        path.reset();
        pathBg.reset();
        pathPro.reset();
        mPaint.reset();
    }
}

 

转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1036363.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号