前几天在群里面有人找圆形可颜色渐变进度条,其中主要的知识点是SweepGradient;
mSweepGradient = new SweepGradient(240, 360, new int[] { Color.CYAN, Color.DKGRAY, Color.GRAY, Color.LTGRAY, Color.MAGENTA, Color.GREEN, Color.TRANSPARENT, Color.BLUE }, null);
如上:第三个参数为渐变颜色内容,前两个是坐标信息,240:渲染中心点x 坐标;360:渲染中心y 点坐标。
先绘制圆形:
package com.soyoungboy.sweepgradientprogress; import android.content.Context; import android.graphics.BlurMaskFilter; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.EmbossMaskFilter; import android.graphics.Paint; import android.graphics.RectF; import android.util.AttributeSet; import android.view.View; /** * 圆形颜色渐变的进度条 * @author soyoungboy * */ public class SweepGradientCircleProgressBar extends View { private Paint pathPaint; private Paint fillArcPaint; // 设置光源的方向 private float[] direction = new float[] {1, 1, 1}; // 设置环境光亮度 private float light = 0.4f; // 选择要应用的反射等级 private float specular = 6; private EmbossMaskFilter emboss; private RectF oval ; private BlurMaskFilter mBlur; // view重绘的标记 private boolean reset = false; // 向 mask应用一定级别的模糊 private float blur = 3.5f; private int arcradus = 30; public SweepGradientCircleProgressBar(Context context ,AttributeSet attrs) { super(context,attrs); initPaint(); oval = new RectF(); emboss = new EmbossMaskFilter(direction, light, specular, blur); mBlur = new BlurMaskFilter(20, BlurMaskFilter.Blur.NORMAL); } //初始化画笔操作 private void initPaint() { //初始化画笔操作 pathPaint = new Paint(); // 设置是否抗锯齿 pathPaint.setAntiAlias(true); // 帮助消除锯齿 pathPaint.setFlags(Paint.ANTI_ALIAS_FLAG); // 设置中空的样式 pathPaint.setStyle(Paint.Style.STROKE); pathPaint.setDither(true); pathPaint.setStrokeJoin(Paint.Join.ROUND); fillArcPaint = new Paint(); // 设置是否抗锯齿 fillArcPaint.setAntiAlias(true); // 帮助消除锯齿 fillArcPaint.setFlags(Paint.ANTI_ALIAS_FLAG); // 设置中空的样式 fillArcPaint.setStyle(Paint.Style.STROKE); fillArcPaint.setDither(true); fillArcPaint.setStrokeJoin(Paint.Join.ROUND); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int height = getMeasuredWidth(); int width = getMeasuredWidth(); //半径 = 宽/2-圆环的宽度 int radius = width/2-arcradus; int cx = width/2; int cy = height/2; //绘制画笔颜色 pathPaint.setColor(Color.RED); //画笔的宽度 pathPaint.setStrokeWidth(10); pathPaint.setMaskFilter(emboss); canvas.drawCircle(cx, cy, radius, pathPaint); } }
效果如下:
时间: 2024-10-13 19:47:11