贝塞尔曲线 仪表板速度绘制

首先阅读Paint 画笔

https://www.jianshu.com/p/df46f0893a83

 

 

以下是半圆绘制


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;



/**
 * 骑行速度绘制
 */
public class DriveChart extends View {
    private static final float DEFAULT_SIZE = 100;
    private static final float FULL_CIRCLE_ANGLE = 260f;
    //RectF 的精度是float
    private RectF chartRect;
    private int strokeWidth = 13;
    private int defaultSize;
    private float density;
    private float maxStat = 120;
    private int degree = 0;
    private boolean isShade;


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

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

    public DriveChart(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.density = getContext().getResources().getDisplayMetrics().density;
        this.defaultSize = (int) (DEFAULT_SIZE * density);
        chartRect = new RectF();
    }

    /**
     * 1、MeasureSpec.UNSPECIFIED -> 未指定尺寸
     * <p>
     * 2、MeasureSpec.EXACTLA -> 精确尺寸,控件的宽高指定大小或者为FILL_PARENT
     * <p>
     * 3、MeasureSpec.AT_MOST -> 最大尺寸,控件的宽高为WRAP_CONTENT,控件大小一般随着控件的子空间或内容进行变化,此时控件尺寸只要不超过父控件允许的最大尺寸
     * ---------------------
     *
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSpec = MeasureSpec.getMode(widthMeasureSpec);
        int heightSpec = MeasureSpec.getMode(heightMeasureSpec);
        int width = 0;
        int height = 0;
        switch (widthSpec) {
            case MeasureSpec.AT_MOST:
                width = defaultSize;
                break;
            case MeasureSpec.EXACTLY:
                width = getMeasuredWidth();
                break;
        }

        switch (heightSpec) {
            case MeasureSpec.AT_MOST:
                height = defaultSize;
                break;
            case MeasureSpec.EXACTLY:
                height = getMeasuredHeight();
                break;
        }
        int chartSize = Math.min(width, height);
        int centerX = width / 2 - chartSize / 2;
        int centerY = height / 2 - chartSize / 2;
        int strokeHalf = strokeWidth / 2;

        chartRect.set(centerX + strokeHalf, centerY + strokeHalf, centerX + chartSize - strokeHalf, centerY + chartSize - strokeHalf);
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //最大角度200
        if (degree > 0) {
            drawChart(canvas, calculatePercents(degree));
        }
    }

    public void setDegree(int degree) {
        this.degree = degree;
        invalidate();
    }

    public int getDegree() {
        return degree;
    }

    private float calculatePercents(float degree) {
        return degree * FULL_CIRCLE_ANGLE / maxStat;
    }

    private void drawChart(Canvas canvas, float degree) {
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        LinearGradient mLinearGradient = new LinearGradient(
                0,
                0,
                FULL_CIRCLE_ANGLE,
                0,
                getResources().getColor(R.color.color2A69F6),
                getResources().getColor(R.color.color8813FF),
                Shader.TileMode.CLAMP
        );
        paint.setStrokeWidth(strokeWidth);
        paint.setAntiAlias(true);
        //设置画笔笔刷类型
        paint.setStrokeCap(Paint.Cap.ROUND);
        //设置渐变色
        if (isShade) {
            paint.setShader(mLinearGradient);
            canvas.drawArc(chartRect, 140, degree, false, paint);
        } else {
            paint.setColor(getResources().getColor(R.color.color20222A));
            canvas.drawArc(chartRect, 140, FULL_CIRCLE_ANGLE, false, paint);
        }


    }

    public void setShade(boolean shade) {
        isShade = shade;
        invalidate();
    }

    public void setStrokeWidth(int strokeWidth) {
        this.strokeWidth = (int) (strokeWidth * density);
        invalidate();
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值