欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android 自定义view仪表盘

程序员文章站 2024-03-24 09:37:04
...
package mda.jwipc.android.myas23;

import android.content.Context;
import android.graphics.*;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by 10090 on 2020/11/20.
 */
public class DashBoard extends View {
    private float mAngle = 120; //未被画弧的角度。//如果改为0的话就是画的一个圆形。
    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);//Paint.ANTI_ALIAS_FLAG防锯齿
    private float mRadius = Utils.dpToPx(150);//圆的半径
    private Path dash = new Path();//用来画刻度的path
    private PathDashPathEffect effect;//定义画可读的规则。
    private float copies = 20;//分成多少份?
    private float phase = 0;//从哪个位置开始画。
    private float mPointerLength = Utils.dpToPx(100);//指针的长度
    private int currentCopies = 2;//指针指向的当前刻度。

    {
        mPaint.setStyle(Paint.Style.STROKE);//  Paint.Style.STROKE 画线不填充。
        mPaint.setStrokeWidth(Utils.dpToPx(2));


        dash.addRect(0, 0, Utils.dpToPx(2), Utils.dpToPx(10), Path.Direction.CW);//这个Direction的方向要和圆弧的方向一致,否则坐标点就需要相反。 CW是顺时针,CCW是逆时针

        Path arc = new Path();//弧的路径。
        arc.addArc(getWidth() / 2 - mRadius, getHeight() / 2 - mRadius, getWidth() / 2 + mRadius, getHeight() / 2 + mRadius, 90 + mAngle / 2, 360 - mAngle);
        PathMeasure pathMeasure = new PathMeasure(arc, false);//测量弧的周长
        float length = pathMeasure.getLength();//弧的周长。
        //Utils.dpToPx(10)矩形的宽度, Utils.dpToPx(10)矩形的长度

        effect = new PathDashPathEffect(dash, (length - Utils.dpToPx(2)) / copies, phase, PathDashPathEffect.Style.ROTATE);
    }

    public DashBoard(Context context) {
        super(context);
    }

    public DashBoard(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public DashBoard(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //useCenter 倒数第二个参数,是否用线连接圆心。 true连接起来,false不连接
        //360 - mAngle绘制路过的角度
        //90 + mAngle / 2起始角度。
        //left 矩形的左边坐标起点
        //top 矩形坐标的高度起点
        //right 矩形的右边结束坐标点
        //bottom 矩形结束的底部坐标点
        //画线
        canvas.drawArc(getWidth() / 2 - mRadius, getHeight() / 2 - mRadius, getWidth() / 2 + mRadius, getHeight() / 2 + mRadius, 90 + mAngle / 2, 360 - mAngle, false, mPaint);

        //画刻度
        mPaint.setPathEffect(effect);
        canvas.drawArc(getWidth() / 2 - mRadius, getHeight() / 2 - mRadius, getWidth() / 2 + mRadius, getHeight() / 2 + mRadius, 90 + mAngle / 2, 360 - mAngle, false, mPaint);
        mPaint.setPathEffect(null);

        //画指针
        canvas.drawLine(getWidth() / 2, getHeight() / 2,
                (float) (Math.cos(Math.toRadians(getAngleFromMark(currentCopies))) * mPointerLength + getWidth() / 2),
                (float) (Math.sin(Math.toRadians(getAngleFromMark(currentCopies))) * mPointerLength + getHeight() / 2),mPaint);
    }

    public void setRadius(float radius) {
        mRadius = Utils.dpToPx(radius);
        invalidate();
    }

    public void setCopies(float copies) {
        this.copies = copies;
        invalidate();
    }

    public void setCurrentCopies(int currentCopies){
        this.currentCopies = currentCopies;
        invalidate();
    }

    int getAngleFromMark(int mark) {
        return (int) (90 + mAngle / 2 + (360 - mAngle) / copies * mark);
    }
}

dp转Px

/**
 * Created by 10090 on 2020/11/20.
 */
public class Utils {

    public static float dpToPx(float dp){
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, Resources.getSystem().getDisplayMetrics());
    }
}

 

android 自定义view仪表盘

相关标签: 学习日志