Android开发之绘制自定义进度条 | 渐变进度条 | 斜角进度条的方法

老套路先上图

 

先看上面的斜角进度条的实现方法:

package cn.yhsh.appwidget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;

/**
 * @author xiayiye5
 * @date 2022/9/6 17:50
 * 自定义progressBar进度条
 */
public class BitmapUtils {
    public static Bitmap createProgressBitmap(Context context, long curPosition, long duration) {
        final int width = 240;
        final int height = 6;
        final int x_offset = Math.round(width * 0.02f);
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);

        Paint paint = new Paint();
        paint.setColor(getColor(context, R.color.remote_view_progress_bar_bg_color));
        //draw progress bar bg.
        drawPath(x_offset, width, width - x_offset, 0, height, canvas, paint);
        if (duration == 0) {
            return bitmap;
        }
        if (curPosition + height > duration) {
            curPosition = duration;
        }
        float curWidth = (float) (width - x_offset) * curPosition / duration;
        if (curWidth > 0) {
            paint = new Paint();
            Shader mShader = new LinearGradient(0, 0, curWidth, height,
                    new int[]{getColor(context, R.color.remote_view_current_position_bg_start_color),
                            getColor(context, R.color.remote_view_current_position_bg_middle_color),
                            getColor(context, R.color.remote_view_current_position_bg_end_color)
                    },
                    null, Shader.TileMode.CLAMP);
            paint.setShader(mShader);
            //draw current progress bar.
            drawPath(x_offset, curWidth + x_offset, curWidth, 0, height, canvas, paint);

            //draw progress bar thumb.
            final int thumbWidth = 3;
            paint = new Paint();
            paint.setColor(getColor(context, R.color.remote_view_current_position_bg_thumb_color));
            drawPath(curWidth + x_offset, curWidth + x_offset + thumbWidth,
    
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于 Android 自定义进度条控件的实现,我可以为您提供一些思路和示例代码。 首先,我们需要继承自 View 或者 ProgressBar,然后在 onDraw 方法实现自己的绘制逻辑。具体来说,需要绘制背景、进度条和文本等内容。 示例代码如下: ```java public class CustomProgressBar extends ProgressBar { private Paint mPaint; private String mText; private Rect mRect; public CustomProgressBar(Context context) { super(context); init(); } public CustomProgressBar(Context context, AttributeSet attrs) { super(context, attrs); init(); } public CustomProgressBar(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { mPaint = new Paint(); mPaint.setAntiAlias(true); mRect = new Rect(); } @Override protected synchronized void onDraw(Canvas canvas) { super.onDraw(canvas); // 绘制背景 mPaint.setColor(Color.GRAY); mPaint.setStyle(Paint.Style.FILL); canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint); // 绘制进度条 mPaint.setColor(Color.BLUE); mPaint.setStyle(Paint.Style.FILL); float progress = getProgress() * 1.0f / getMax(); canvas.drawRect(0, 0, getWidth() * progress, getHeight(), mPaint); // 绘制文本 mPaint.setColor(Color.WHITE); mPaint.setTextSize(24); mText = getProgress() + "%"; mPaint.getTextBounds(mText, 0, mText.length(), mRect); float x = getWidth() / 2f - mRect.centerX(); float y = getHeight() / 2f - mRect.centerY(); canvas.drawText(mText, x, y, mPaint); } } ``` 这个自定义控件实现了一个简单的水平进度条,包括了背景、进度条和文本三个部分。当然,您可以根据自己的需求进行更改和扩展。 希望这个示例代码能够帮助到您,如果您还有其他问题,欢迎继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值