android 自定义园动画,如何在Android中绘制一个带有动画的圆

你必须绘制圆形视图,然后你应该创建一个动画。

创建圈子视图:

public class Circle extends View {

private static final int START_ANGLE_POINT = 90;

private final Paint paint;

private final RectF rect;

private float angle;

public Circle(Context context, AttributeSet attrs) {

super(context, attrs);

final int strokeWidth = 40;

paint = new Paint();

paint.setAntiAlias(true);

paint.setStyle(Paint.Style.STROKE);

paint.setStrokeWidth(strokeWidth);

//Circle color

paint.setColor(Color.RED);

//size 200x200 example

rect = new RectF(strokeWidth, strokeWidth, 200 + strokeWidth, 200 + strokeWidth);

//Initial Angle (optional, it can be zero)

angle = 120;

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

canvas.drawArc(rect, START_ANGLE_POINT, angle, false, paint);

}

public float getAngle() {

return angle;

}

public void setAngle(float angle) {

this.angle = angle;

}

}

创建动画类来设置新的角度:

public class CircleAngleAnimation extends Animation {

private Circle circle;

private float oldAngle;

private float newAngle;

public CircleAngleAnimation(Circle circle, int newAngle) {

this.oldAngle = circle.getAngle();

this.newAngle = newAngle;

this.circle = circle;

}

@Override

protected void applyTransformation(float interpolatedTime, Transformation transformation) {

float angle = oldAngle + ((newAngle - oldAngle) * interpolatedTime);

circle.setAngle(angle);

circle.requestLayout();

}

}

将圆圈放入您的布局:

android:id="@+id/circle"

android:layout_width="300dp"

android:layout_height="300dp" />

最后开始动画:

Circle circle = (Circle) findViewById(R.id.circle);

CircleAngleAnimation animation = new CircleAngleAnimation(circle, 240);

animation.setDuration(1000);

circle.startAnimation(animation);

其结果是:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值