我正在尝试绘制一个圆圈的动画。在我的自定义视图中,我有
private final Paint mPaint = new Paint() {
{
setDither(true);
setStyle(Paint.Style.STROKE);
setStrokeCap(Paint.Cap.ROUND);
setStrokeJoin(Paint.Join.ROUND);
setColor(Color.BLUE);
setStrokeWidth(30.0f);
setAntiAlias(true);
}
};
...
protected void onDraw(Canvas canvas) {
super.onDraw();
if (mOval == null) {
mOval = new RectF(getLeft(), getTop(), getRight(), getBottom());
}
if (mPath == null) {
mPath = new Path();
mPath.moveTo(0, getHeight() / 2);
}
float sweepAngle = Math.min((float) mElapsedTime / 1000 * 60 * 1, 1) * 360;
if (sweepAngle == 0) {
mPath.reset();
} else if (mCurrentAngle != sweepAngle) {
mPath.arcTo(mOval, mCurrentAngle, sweepAngle);
}
mCurrentAngle = sweepAngle;
canvas.drawPath(mPath, mPaint);
}
我会不定时更新mElapsedTime
并打电话invalidate()
。然而,屏幕上没有绘制任何内容。我尝试了几种变体,但没有成功。我做错了什么吗?有没有更简单的方法来做到这一点?给定圆的百分比,我希望能够使圆的大部分成为屏幕上绘制的内容。
这里有两件事:
你必须打电话canvas.drawOval(...)
在将圆弧绘制到椭圆上之前。不然就不会显示出来。这就是为什么我的方法不起作用。
Canvas
has a drawArc
需要起始角度和扫掠度数的方法。看Canvas.drawArc(RectF, float, float, boolean, Paint) https://developer.android.com/reference/android/graphics/Canvas.html#drawArc(android.graphics.RectF,%20float,%20float,%20boolean,%20android.graphics.Paint)。这就是我想要画圆圈的东西。
EDIT:这是我的相关代码View
子类:
private final Paint mArcPaint = new Paint() {
{
setDither(true);
setStyle(Paint.Style.STROKE);
setStrokeCap(Paint.Cap.ROUND);
setStrokeJoin(Paint.Join.ROUND);
setColor(Color.BLUE);
setStrokeWidth(40.0f);
setAntiAlias(true);
}
};
private final Paint mOvalPaint = new Paint() {
{
setStyle(Paint.Style.FILL);
setColor(Color.TRANSPARENT);
}
};
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
RectF mOval = new RectF(left, top, right, bottom); //This is the area you want to draw on
float sweepAngle = 270; //Calculate how much of an angle you want to sweep out here
canvas.drawOval(mOval, mOvalPaint);
canvas.drawArc(mOval, 269, sweepAngle, false, mArcPaint); //270 is vertical. I found that starting the arc from just slightly less than vertical makes it look better when the circle is almost complete.
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)