- Better create a custom view class say "ArcView" which will extend View class. Then add your draw code in onDraw method.
- For Animation, you can write separate class which will extend Animation class
- pass your custom view to Animation class and under applyTransformation() method, calculate angle then set it to your custom view and invalide the view.
You are done with Arc with nice animation.
View Class
public class ArcView extends View {
private final Paint mPaint;
private final RectF mRect;
private float arcAngle;
public ArcView(Context context, AttributeSet attrs) {
super(context, attrs);
// Set Angle to 0 initially
arcAngle = 0;
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(20);
mPaint.setColor(Color.RED);
mRect = new RectF(20, 20, 220, 220);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawArc(mRect, 90, arcAngle, false, mPaint);
}
public float getArcAngle() {
return arcAngle;
}
public void setArcAngle(float arcAngle) {
this.arcAngle = arcAngle;
}
}
Animation Class
public class ArcAngleAnimation extends Animation {
private ArcView arcView;
private float oldAngle;
private float newAngle;
public ArcAngleAnimation(ArcView arcView, int newAngle) {
this.oldAngle = arcView.getArcAngle();
this.newAngle = newAngle;
this.arcView = arcView;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation transformation) {
float angle = 0 + ((newAngle - oldAngle) * interpolatedTime);
arcView.setArcAngle(angle);
arcView.requestLayout();
}
}
Inside Activity, you can have below code to instantiate arc view.
ArcView arcView = (ArcView) findViewById(R.id.arcView);
ArcAngleAnimation animation = new ArcAngleAnimation(arcView, 360);
animation.setDuration(1000);
arcView.startAnimation(animation);
Inside layout,
<com.graph.app.Test.ArcView
android:id="@+id/arcView"
android:layout_width="match_parent"
android:layout_height="match_parent" />