android 自定义八边形进度条

自定义八边形动画效果图如下

绘制步骤:

1.先绘制橙色底部八边形实心

2.黑色画笔绘制第二层,让最外层显示一条线条宽度即可

3.再用黄色画笔绘制黄色部分

4.使用渐变画笔根据当前进度绘制覆盖黄色部分

5.使用黑色画笔根据当前进度绘制刻度条

6.黑色画笔绘制第三层留出黄色部分的宽度

完整代码

javascript 复制代码
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RadialGradient;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

public class EightView extends View {
    private Paint mPaint;
    private Paint roundPaint;
    private Paint bgPaint;
    private float mR, mCx, mCy,mR1,mR2;
    private static final int mN = 8;
    private static final float DEGREES_UNIT = 360 / mN; //正N边形每个角  360/mN能整除
    private int width = 0;
    private int height = 0;


    //外部描边色:#d06d38

    private int[] colors = new int[]{
            Color.parseColor("#e3682f"),
            Color.parseColor("#e3682f"),
            Color.parseColor("#e59f3a"),
            Color.parseColor("#e59f3a"),
    };

    public EightView(Context context) {
        this(context, null);
    }

    public EightView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EightView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        //   PorterDuffXfermode porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
        //绘制8边形外环
        mPaint = new Paint();
        mPaint.setColor(Color.parseColor("#e3682f"));
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeCap(Paint.Cap.ROUND); // 设置转弯处为圆角
        // 当绘图样式为 STROKE 时,该方法用于指定线条连接处的拐角样式,能使绘制的图形更加平滑
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeWidth(50F);
        //  mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST));
        mPaint.setAntiAlias(true);

        //绘制刻度圆,然后进行裁剪,按照外环的大小
        roundPaint=new Paint();
        roundPaint.setStyle(Paint.Style.FILL);
        roundPaint.setAntiAlias(true);
        roundPaint.setStrokeWidth(8);
        //  roundPaint.setXfermode(porterDuffXfermode);

        //绘制黑色背景,bgPaint


        bgPaint=new Paint();
        bgPaint.setColor(Color.YELLOW);
        bgPaint.setStyle(Paint.Style.FILL);
        bgPaint.setAntiAlias(true);
        //    bgPaint.setXfermode(porterDuffXfermode);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getSize(widthMeasureSpec);
        height = getSize(heightMeasureSpec);
        if (width < height) {
            height = width;
        } else {
            width = height;
        }
        setMeasuredDimension(width, height);
    }

    private int getSize(int measureSpec) {
        int mySize = 100;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        switch (mode) {
            case MeasureSpec.UNSPECIFIED: {//如果没有指定大小:就设置为默认大小
                mySize = 100;
                break;
            }
            case MeasureSpec.AT_MOST: //如果测里模式是最大取值为size,我们将大小取最大值,你也可以取其他值
            case MeasureSpec.EXACTLY: {//如果是固定的大小,那就不要去改变它
                mySize = size;
                break;
            }
        }
        return mySize;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        float mW = getMeasuredWidth();
        float mH = getMeasuredHeight();

        if (mW < mH) {
            mH = mW;
        } else {
            mW = mH;
        }
        //----mCx-230.0---mCy-230.0---mR-172.5
        mCx = mW / 2;
        mCy = mH / 2;
        //mCx和mCy中的较小者
        mR = Math.min(mCx, mCy) / 4 * 3;

        mR1 = mW/7*3;
        mR2=mR1-5;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.rotate(23, mCx, mCy);
//        canvas.rotate(-45, mCx, mCy);
        canvas.drawColor(bgColor);
        mPaint.setColor(Color.parseColor("#e3682f"));
        float d1 = (float) (2 * mR1 * Math.sin(Math.toRadians(DEGREES_UNIT / 2)));
        float c1 = mCy - mR1;
        float y1 = (d1 * d1 + mCy * mCy - c1 * c1 - mR1 * mR1) / (2 * (mCy - c1));
        float x1 = (float) (mCx + Math.sqrt(-1 * c1 * c1 + 2 * c1 * y1 + d1 * d1 - y1 * y1));
        for (int i = 0; i < 8; i++) {
            canvas.save();
            canvas.rotate(DEGREES_UNIT * i, mCx, mCy);
            canvas.drawLine(mCx, c1, x1, y1, mPaint);
            canvas.restore();
        }

        mPaint.setColor(bgColor);
        float d2 = (float) (2 * mR2 * Math.sin(Math.toRadians(DEGREES_UNIT / 2)));
        float c2 = mCy - mR2;
        float y2 = (d2 * d2 + mCy * mCy - c2 * c2 - mR2 * mR2) / (2 * (mCy - c2));
        float x2 = (float) (mCx + Math.sqrt(-1 * c2 * c2 + 2 * c2 * y2 + d2 * d2 - y2 * y2));
        for (int i = 0; i < 8; i++) {
            canvas.save();
            canvas.rotate(DEGREES_UNIT * i, mCx, mCy);
            // canvas.drawLine(mCx, mCy, mCx, c, mPaint);//中间的连接线
            canvas.drawLine(mCx, c2, x2, y2, mPaint);
            canvas.restore();
        }
        //离屏绘制
        int layer1 = canvas.saveLayer(0, 0, width, height, mPaint);
        int layer2 = canvas.saveLayer(0, 0, width, height, bgPaint);
        int layer3 = canvas.saveLayer(0, 0, width, height, roundPaint);

        mPaint.setColor(Color.parseColor("#e59f3a"));
        float d = (float) (2 * mR * Math.sin(Math.toRadians(DEGREES_UNIT / 2)));
        float c = mCy - mR;
        float y = (d * d + mCy * mCy - c * c - mR * mR) / (2 * (mCy - c));
        float x = (float) (mCx + Math.sqrt(-1 * c * c + 2 * c * y + d * d - y * y));
        for (int i = 0; i < 8; i++) {
            canvas.save();
            canvas.rotate(DEGREES_UNIT * i, mCx, mCy);
            canvas.drawLine(mCx, c, x, y, mPaint);
            canvas.restore();
        }
        if (gradient==null){
            gradient =new RadialGradient(mCx,mCy, mCx/5,
                    colors,null, Shader.TileMode.MIRROR);
        }
        bgPaint.setShader(gradient);
        /**
         * left--矩形左侧的X坐标
         * top--矩形顶部的Y坐标
         * right--矩形右侧的X坐标
         * bottom--矩形底部的Y坐标
         */
        if (rectF==null){
            rectF = new RectF(0, 0, width, height);
        }
        bgPaint.setXfermode(mode);
        canvas.drawArc(rectF, 67, indexCircle, true, bgPaint);//底部圆
        //最后将画笔去除Xfermode
        bgPaint.setXfermode(null);

        //绘制刻度线
        canvas.translate(width / 2, height / 2);
        roundPaint.setXfermode(mode);
        roundPaint.setColor(bgColor);
        //90度12根,360度48根 270度36  //90度8根,360度30根
        int index=indexCircle*46/360;
        for (int i = 0; i < index; i++) {//共48等分,根据绘制的角度更改
            canvas.save();//画布保存
            int degress=i * 8+67;
            if (degress>360){
                degress=degress-360;
            }
            canvas.rotate(degress);//绘制图标的旋转360 + i * 36
//            int alpha = (int) ((i / 60f * 255 + circleAlpha) % 255);
//            mPaint.setAlpha(alpha);//设置画笔的透明度[0-255],0是完全透明,255是完全不透明
            canvas.translate(width/2-mCircleWidth, 0);//绘图坐标的平移。
            canvas.drawLine(0, 0, mCircleWidth, 0, roundPaint);//绘制线.drawLine,drawLines绘制多条线
            canvas.restore();//合并保存后的图层
        }
        //最后将画笔去除Xfermode
        roundPaint.setXfermode(null);

        //恢复图层
        canvas.restoreToCount(layer1);
        canvas.restoreToCount(layer2);
        canvas.restoreToCount(layer3);
    }
    private  RadialGradient gradient;
    private  RectF rectF;
    private PorterDuffXfermode mode=new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP);

    private int mCircleWidth = 120;//圆弧的长度
    private int indexCircle=0;
    private int bgColor=Color.BLACK;

    public void setBgColor(int BgColor){
        bgColor=BgColor;
    }

    public void setAngle(int mStepNum) {
        mStepNum=360-mStepNum;
        if (mStepNum > 360) {
            mStepNum = 360;
        }
        if (mStepNum < 0) {
            mStepNum = 0;
        }
        this.indexCircle = mStepNum;
        invalidate();
    }
}
相关推荐
Calvin8808283 分钟前
Android Studio 的革命性更新:Project Quartz 和 Gemini,开启 AI 开发新时代!
android·人工智能·android studio
敲代码敲到头发茂密1 小时前
【大语言模型】LangChain 核心模块介绍(Memorys)
android·语言模型·langchain
H1002 小时前
重构(二)
android·重构
拓端研究室3 小时前
R基于贝叶斯加法回归树BART、MCMC的DLNM分布滞后非线性模型分析母婴PM2.5暴露与出生体重数据及GAM模型对比、关键窗口识别
android·开发语言·kotlin
zhangphil4 小时前
Android简洁缩放Matrix实现图像马赛克,Kotlin
android·kotlin
m0_512744644 小时前
极客大挑战2024-web-wp(详细)
android·前端
lw向北.4 小时前
Qt For Android之环境搭建(Qt 5.12.11 Qt下载SDK的处理方案)
android·开发语言·qt
不爱学习的啊Biao4 小时前
【13】MySQL如何选择合适的索引?
android·数据库·mysql
Clockwiseee4 小时前
PHP伪协议总结
android·开发语言·php
mmsx11 小时前
android sqlite 数据库简单封装示例(java)
android·java·数据库