如何使用画布在Android上绘制逆时针弧?如果canvas不能,这里有什么解决办法吗?
问问题
2995 次
2 回答
1
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);
于 2015-07-24T14:10:48.730 回答
0
检查此代码,
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();
}
}
}
于 2013-05-30T04:17:31.867 回答