简体   繁体   English

我怎样才能在 canvas android 中绘制这个 [暂停]

[英]How can I draw this in canvas android [on hold]

I am trying to draw this figure but I am unable to draw it.我正在尝试绘制这个数字,但我无法绘制它。

I don't want to use any library.我不想使用任何库。 Just want to draw in canvas android.只想在canvas android中画图。 在此处输入图像描述

Consider - center of circle is center of screen ie canvas.translate(centerX, centerY)考虑 - 圆心是屏幕的中心,即 canvas.translate(centerX, centerY)

update:更新:

@Override
    protected void onDraw(Canvas canvas) {
        radius = dip2px(getContext(), 150);//252
        canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
        canvas.save();
        canvas.translate(getWidth() / 2, getHeight() / 2);
        for (int i = -35; i <= 215; i += 1) {
            drawOuterLine(i, canvas);
        }
        canvas.restore();
    }

    private void drawOuterLine(int i, Canvas canvas) {
        Paint paint2 = new Paint();
        paint2.setAntiAlias(true);
        paint2.setColor(getResources().getColor(R.color.ruler_white));
        paint2.setStrokeWidth(dip2px(getContext(), 1));

        Coordinate coordinate = getCoordinate(radius, i);
        Coordinate coordinate1;
        float x = coordinate.getX();
        float y = coordinate.getY();
        float r;
        if (i == -35 || i == -11 || i == 15 || i == 39 || i == 65 || i == 89 || i == 115 || i == 139 || i == 165 || i == 189 || i == 215) {
            r = radius - padding;
            coordinate1 = getCoordinate(r, i);
            float x1 = coordinate1.getX();
            float y1 = coordinate1.getY();
            canvas.drawLine(-x1, -y1, -x, -y, paint2);
            String text = String.valueOf(showDegree[showDegreeCounter]);
            if (showDegreeCounter<10) showDegreeCounter++;
            Path path = getTextPath(text, mOutDegreePaint, i, radius
                    - padding - fontSize * 5 / 4);
            canvas.drawTextOnPath(text, path, 0, 0, mOutDegreePaint);
        } else if (i % 2 != 0) {
            r = radius - padding * 3 / 5;
            coordinate1 = getCoordinate(r, i);
            float x1 = coordinate1.getX();
            float y1 = coordinate1.getY();
            canvas.drawLine(-x1, -y1, -x, -y, paint2);
        }
    }

Here is the snap I have achieved but the problem is those degrees are also being rotated and I don't want that.这是我实现的快照,但问题是这些度数也在旋转,我不希望这样。 Please help me with this.请帮我解决一下这个。

It is a comprehensive problem but I can explain how to achieve the meter markings.这是一个综合性问题,但我可以解释如何实现仪表标记。

Using a combination of Canvas.drawArc() and PathDashPathEffect , we can achieve the markings.使用Canvas.drawArc()PathDashPathEffect的组合,我们可以实现标记。 We'll use an arc for the current speed and arc for the leftover speed.我们将使用弧线作为当前速度,使用弧线作为剩余速度。

We define a value for the total number of degrees in the circle representing the speedometer.我们为代表速度计的圆圈中的总度数定义一个值。 In that drawing it looks like about 270 degrees total is available.在那幅图中,它看起来总共有大约 270 度可用。

float totalDegrees = 270.0f

Compute the percentage of the total speed that the current speed represents and assign that to a float variable.计算当前速度代表的总速度的百分比并将其分配给浮点变量。

    float currentSpeed = 20;
    float maximumSpeed = 100;
    float currentSpeedPercent = currentSpeed/maximumSpeed;

Multiply this value by the total degrees available to get the sweep angle for the first arc.将此值乘以可用的总度数以获得第一条弧的扫角。 So the start angle for the first arc will be 0 and the sweep angle will be what we just calculated.所以第一条弧的起始角为 0,而后掠角就是我们刚刚计算的。

     float sweepAngleCurrentSpeed = currentSpeedPercent * totalDegrees;
    float startAngleCurrentSpeed = 235.0f; // 235 degrees is based on starting the // speedometer at the angle shown in the drawing.

Now that we know the first arc, we can easily create the arc for the remaining speed.现在我们知道了第一条弧线,我们可以轻松地为剩余速度创建弧线。 The start angle for that arc will be equal to the sweep angle of the first arc, and the sweep angle will be equal to 270(total degrees available) - startAngle.该弧的起始角度将等于第一个弧的扫角,并且扫角将等于 270(可用总度数) - startAngle。

float startAngleRemainingSpeed = startAngleCurrentSpeed + sweepAngleCurrentSpeed;
    float sweepAngleRemainingSpeed = totalDegrees - sweepAngleCurrentSpeed;

Create a Paint object that will be used to paint the tick marks.创建用于绘制刻度线的Paint object。

Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE); // style must be set to STROKE in order to do tick
// mark dashes
paint.setPathEffect(new DashPathEffect(new float[] {10,20}, 0)); // Represents the dashes

Define a bounds that we're going to draw the speedometer in. Let's use a 100 X 100 rectangle as an example.定义我们要在其中绘制速度计的边界。让我们以 100 X 100 矩形为例。

RectF bounds = new RectF(0,0,100,100);

Set a color for the tick marks for the current speed.为当前速度的刻度线设置颜色。

paint.setColor(Color.YELLOW);

Draw the arc for the current speed:绘制当前速度的圆弧:

canvas.drawArc(bounds, startAngleCurrentSpeed, sweepAngleCurrentSpeed, true, paint);

Set the color for the tick marks of the remaining speed.设置剩余速度刻度线的颜色。

paint.setColor(Color.GRAY);

Draw the arc for the remaining speed:画出剩余速度的圆弧:

canvas.drawArc(bounds, startAngleRemainingSpeed, sweepAngleRemainingSpeed, true, paint);

This would draw a basic unlabeled speedometer.这将绘制一个基本的未标记速度计。 Using math based on the bounds of the rectangle.使用基于矩形边界的数学。 You can determine where to position the tick marks.您可以确定 position 在哪里打勾。 Or you can use ``` canvas.rotate()/canvas.translate()或者你可以使用``` canvas.rotate()/canvas.translate()

to assist in placing the speed labels.



声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM