美文网首页
2. 使用线性渐变实现进度条

2. 使用线性渐变实现进度条

作者: 努力生活的西鱼 | 来源:发表于2021-04-26 23:14 被阅读0次

前段时间,项目里面有个需求,是要实现语音包下载进度的显示。

首先看几个需要用到的东西

RectF: 根据指定坐标创建一个长方形
用的别人的图
LinearGradient: 线性渐变
构造函数:
public LinearGradient(float x0, float y0, float x1, float y1, @NonNull @ColorInt int colors[],
            @Nullable float positions[], @NonNull TileMode tile)
- x0: 线性渐变起点的X轴坐标
- y0: 线性渐变起点的Y轴坐标
- x1: 线性渐变终点的X轴坐标
- y1: 线性渐变终点的Y轴坐标
- colors[]: 线性渐变颜色的组合
- positions[]: 取值范围为[0,1],代表上面颜色所在的位置

整体的思路就是,我们不停的设置进度值,然后不停的刷新。

/**
 * 设置进度,此为线程安全控件,由于考虑多线程的问题,需要同步
 * 刷新界面调用postInvalidate()能在非UI线程刷新
 *
 * @param progress
 */
public synchronized void setProgress(int progress) {
    if (progress > mMax) {
        progress = mMax;
    }

    this.mProgress = progress;
    postInvalidate();
}

/**
 * 绘制背景
 *
 * @param canvas
 */
private void drawBackground(Canvas canvas) {

    if (mBackgroundBounds == null) {
        mBackgroundBounds = new RectF();
        if (mRadius == 0) {
            mRadius = 52;
        }
        mBackgroundBounds.left = 0;
        mBackgroundBounds.top = 0;
        mBackgroundBounds.right = getMeasuredWidth();
        mBackgroundBounds.bottom = getMeasuredHeight();
    }

    mPercent = mProgress / (mMax + 0f);

    // 白天
    mNormalColor = ContextCompat.getColor(ContextHolder.getContext(),R.color.module_setting_color_voice_normal_day);
    mDownloadColor = ContextCompat.getColor(ContextHolder.getContext(),R.color.module_setting_color_voice_download_day);

        // 下载从灰变蓝的渐变
        linearGradient = new LinearGradient(
                // 渐变起始点坐标
                0, 0,
                // 渐变结束点坐标
                getMeasuredWidth(), 0,
                // 渐变颜色数组
                new int[]{mDownloadColor, mNormalColor},
                // 位置数组
                new float[]{mPercent, mPercent + 0.001f},
                // 空白区域的填充方法
                Shader.TileMode.CLAMP
        );

        // 从浅灰到深灰
        linearGradient2 = new LinearGradient(
                0,0,
                getMeasuredWidth(),getMeasuredHeight(),
                new int[]{Color.parseColor("#F2F2F2"),Color.parseColor("#F2F2F2")},
                new float[]{0,1},
                Shader.TileMode.CLAMP
        );
    }

    mPaint.setColor(mNormalColor);
    if (mPercent == 0) {
        mPaint.setShader(linearGradient2);
        canvas.drawRoundRect(mBackgroundBounds, mRadius, mRadius, mPaint);
    } else {
        mPaint.setShader(linearGradient);
        canvas.drawRoundRect(mBackgroundBounds, mRadius, mRadius, mPaint);
    }

}
晚安
2021-04-26

相关文章

  • 2. 使用线性渐变实现进度条

    前段时间,项目里面有个需求,是要实现语音包下载进度的显示。 首先看几个需要用到的东西 整体的思路就是,我们不停的设...

  • CSS3动态进度条

    CSS3的线性渐变生成动态进度条 渐变基本语法: 效果图如下:

  • iOS非线性变化渐变色

    需求分析 项目中很多时候要使用渐变色,但是系统框架给出的渐变都是线性渐变,那么想实现像阴影那样的非线性渐变就需要定...

  • 渐变色简单实现

    关于实现渐变色的解答,渐变分为线性渐变和径向渐变。 一、 线性渐变(Linear Gradients)- 向下/向...

  • Android背景渐变xml

    使用shape的gradient属性实现渐变 效果图 线性渐变 在drawable文件夹下新建shape资源: 属...

  • 03、CSS3-UI样式

    一、圆角 二、线性渐变 案例: 进度条 三、径向渐变 四、背景 五、遮罩 作者:西门奄链接:https://www...

  • HTML中canvas线性渐变的使用方法:

    HTML中canvas线性渐变的使用方法: canvas渐变分为两种 ,下面进行对线性渐变的讲解: .线性渐变; ...

  • RN - echarts实现渐变色

    方法一: 1.线性渐变 2.径向渐变 3.纹理填充 方法二: 使用echarts内置的渐变色生成器echarts....

  • Android 条形进度条

    可设置 线性渐变-背景色-进度条颜色-进度条高度效果图 步骤一:新建自定义控件BarPercentView继承Vi...

  • CSS中的渐变

    1.线性渐变 linear-gradient 线性渐变作用的事 背景图片 2. 径向渐变radial-gradie...

网友评论

      本文标题:2. 使用线性渐变实现进度条

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