簡體   English   中英

如何在Android上繪制逆時針圓弧?

[英]How to draw a reverse clockwise arc on Android?

如何使用畫布在Android上繪制逆時針圓弧? 如果帆布不能,這里有什么解決方案嗎?

Path.arcTo()參數SweepAngle指的是旋轉角度,如果sweepAngle為正,則弧為順時針方向;如果sweepAngle為負,則弧為逆時針方向。

該代碼在我的生產環境中使用,它繪制了一個半圓環,路徑在外半徑上是順時針方向,在內半徑上是逆時針方向:

drawpercent = 0.85;

radiusPathRectF = new android.graphics.RectF((float)CentreX - (float)Radius, (float)CentreY - (float)Radius,  (float)CentreX + (float)Radius, (float)CentreY + (float)Radius);
innerradiusPathRectF = new android.graphics.RectF((float)CentreX - (float)InnerRadius, (float)CentreY - (float)InnerRadius, (float)CentreX + (float)InnerRadius, (float)CentreY + (float)InnerRadius);

Path p = new Path(); //TODO put this outside your draw() function,  you should never have a "new" keyword inside a fast loop.

                degrees = (360 + (DegreesStart)) % 360;
                radians = (360 - degrees + 90) * Math.PI / 180.0;
                //radians = Math.toRadians(DegreesStart);
                int XstartOuter = (int)Math.round((Math.cos(radians) * Radius + CentreX));
                int YstartOuter = (int)Math.round((Math.sin(-radians)* Radius + CentreY));
                int XstartInner = (int)Math.round((Math.cos(radians) * InnerRadius + CentreX));
                int YstartInner = (int)Math.round((Math.sin(-radians) * InnerRadius + CentreY));

                degrees = (360 + (DegreesStart + drawpercent * DegreesRotation)) % 360;
                //radians = degrees * Math.PI / 180.0;
                radians = (360 - degrees + 90) * Math.PI / 180.0;
                //radians = Math.toRadians(DegreesStart + drawpercent * DegreesRotation);
                int XendOuter = (int)Math.round((Math.cos(radians) * Radius + CentreX));
                int YendOuter = (int)Math.round((Math.sin(-radians) * Radius + CentreY));
                int XendInner = (int)Math.round((Math.cos(radians) * InnerRadius + CentreX));
                int YendInner = (int)Math.round((Math.sin(-radians) * InnerRadius + CentreY));

                //draw a path outlining the semi-circle ring.
                p.moveTo(XstartInner, YstartInner);
                p.lineTo(XstartOuter, YstartOuter);
                p.arcTo(radiusPathRectF, (float)DegreesStart - (float)90, (float)drawpercent * (float)DegreesRotation);
                p.lineTo(XendInner, YendInner);
                p.arcTo(innerradiusPathRectF, (float)degrees - (float)90, -1 * (float)drawpercent * (float)DegreesRotation);
                p.close();

                g.clipPath(p);

                g.drawBitmap(bitmapCircularBarImage, bitmapRect0, bitmapRectXY, paint);

檢查此代碼,

    public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

private static class AnimView extends View {
    private Paint myPaint;
    private Paint myPaint2;
    private Paint myFramePaint;
    private RectF bigOval;
    public TextView value;
    private RectF bigOval2;
    private float myStart;
    private float mySweep;
    private float SWEEP_INC = 3;
    private float SWEEP_INC2 = 5;
    // Use this flag to control the direction of the arc's movement
    private boolean addToCircle = true;

    public AnimView(Context context) {
        super(context);
        init();
    }

    public AnimView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        myPaint = new Paint();

        myPaint.setAntiAlias(true);

        myPaint.setStyle(Paint.Style.STROKE);

        myPaint.setColor(Color.GREEN);

        myPaint.setStrokeWidth(10);

        bigOval = new RectF(40, 10, 280, 250);

        myFramePaint = new Paint();
        myFramePaint.setAntiAlias(true);
        myFramePaint.setColor(Color.WHITE);
    }

    private void drawArcs(Canvas canvas, RectF oval, boolean useCenter,
            Paint paint) {
        canvas.drawRect(oval, myFramePaint);
        canvas.drawArc(oval, myStart, mySweep, false, paint);

    }

    public void setIncrement(float newIncrement) {
        SWEEP_INC = newIncrement;

    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawArcs(canvas, bigOval, true, myPaint);
        value = (TextView) findViewById(R.id.value);
        drawArcs(canvas, bigOval, true, myPaint);
        myStart = -90;
        // If the arc is currently getting bigger, decrease the value of
        // mySweep
        if (addToCircle) {
            mySweep -= SWEEP_INC;
        }
        // If the arc is currently getting smaller, increase the value of
        // mySweep
        else {
            mySweep += SWEEP_INC;
        }
        // If the animation has reached the end, reverse it

        invalidate();
    }
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM