在 android 中用 canvas 绘制四分之一仪表?

Draw quarter gauge with canvas in android?

如何创建与此相同的仪表? 此仪表类型是否存在任何库或 Maven? 我使用 canvas 作为圆规,但我无法创建这个

 RectF oval = getOval(canvas, 1);
    RectF oval2 = getOval(canvas, 1.1f);

    Paint paint = new Paint();
    paint.setColor(Color.DKGRAY);

    canvas.drawArc(oval2, 180, 90, true, paint);
    canvas.drawArc(oval, 180, 90, true, backgroundPaint);

    RectF innerOval = getOval(canvas, 0.9f);
    canvas.drawArc(innerOval, 180, 90, true, backgroundInnerPaint);

<GaugeView
        android:id="@+id/gauge"
        android:layout_width="@dimen/panel_gauge_height"
        android:layout_height="@dimen/panel_gauge_width"
        app:divisions="8"
        app:scaleEndValue="8"
        app:scaleStartAngle="50"
        app:scaleStartValue="0"
        app:showNeedle="true"
        app:subdivisions="2" />

  1. 您可以 google 找到一堆 GagueView。我想你已经这样做了。

  2. 这看起来很简单。所以你可以写你自己的习惯 View。您可以使用 drawArc 绘制 3 种不同颜色的弧线。您可以使用drawLine 来绘制测量点(只记得设置Paint.setStrokeCap(Cap.Round))。至于针,你可以用drawPath。通过一些努力和正确的坐标,您应该能够自己编写一个漂亮的 GaugeView。

  3. 如果觉得写自己的观点有难度,可以参考GitHub中的一些GaugeView。你会得到一个很好的起点。

更新:我根据你问题中的图片写了一个简单的GaugeView。计算以像素为单位,您可能希望将它们与 DisplayMetrics.density 相乘,以便它们以独立的像素为单位。此外,您可能希望通过 xml 在此处公开大部分值,以便您可以在布局中控制它们。这可能是一个很好的起点。

public class GaugeView extends View {

    private Paint arcPaint;

    public GaugeView(Context context) {
        super(context);
        initialize();
    }

    public GaugeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    public GaugeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize();
    }

    private void initialize() {
        arcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        arcPaint.setStyle(Paint.Style.STROKE);
        arcPaint.setStrokeWidth(15f);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int width = canvas.getWidth();
        int height = canvas.getHeight();

        int arcCenterX = width - 10;
        int arcCenterY = height - 10;

        final RectF arcBounds = new RectF(arcCenterX - 100, arcCenterY - 100, arcCenterX + 100, arcCenterY + 100);


        // Draw the arc
        canvas.drawArc(arcBounds, 180f, 20f, false, arcPaint);
        arcPaint.setColor(Color.DKGRAY);
        canvas.drawArc(arcBounds, 200f, 40f, false, arcPaint);
        arcPaint.setColor(Color.GRAY);
        canvas.drawArc(arcBounds, 2400f, 30f, false, arcPaint);

        // Draw the pointers
        final int totalNoOfPointers = 20;
        final int pointerMaxHeight = 25;
        final int pointerMinHeight = 15;

        int startX = arcCenterX - 120;
        int startY = arcCenterY;
        arcPaint.setStrokeWidth(5f);
        arcPaint.setStrokeCap(Paint.Cap.ROUND);

        int pointerHeight;
        for (int i = 0; i <= totalNoOfPointers; i++) {
            if(i%5 == 0){
                pointerHeight = pointerMaxHeight;
            }else{
                pointerHeight = pointerMinHeight;
            }
            canvas.drawLine(startX, startY, startX - pointerHeight, startY, arcPaint);
            canvas.rotate(90f/totalNoOfPointers, arcCenterX, arcCenterY);
        }
    }
}


最终的渲染图是这样的: