我需要使用动画创建一个圆。
我需要将持续时间设置为2000,直到我的圈子以360度结束。
课程内容
End if
活动内容
Option Explicit
问题是我的圆没有在圆的尽头结束而停在中间的某个地方。这条线在我的圆的尽头没有完成...
[![Img] [1]] [1]
f
答案 0 :(得分:1)
您可以尝试以下代码,效果是这样的
课程内容
class MyCustomView:View
{
private int animValue;
private int strokeWidth = 15;
//private int i = 0;
public MyCustomView(Context context) :
base(context)
{
}
public MyCustomView(Context context, IAttributeSet attrs) :
base(context, attrs)
{
animValue = 0;
}
public MyCustomView(Context context, IAttributeSet attrs, int defStyle) :
base(context, attrs, defStyle)
{
}
protected override void OnDraw(Canvas canvas)
{
base.OnDraw(canvas);
Paint paint = new Paint();
paint.SetStyle(Paint.Style.Stroke);
paint.StrokeWidth = (strokeWidth);
RectF rectF = new RectF();
rectF.Set(strokeWidth, strokeWidth, Width - strokeWidth, Width - strokeWidth);
paint.Color = (Color.Gray);
canvas.DrawArc(rectF, 0, 360, false, paint);
paint.Color = (Color.Blue);
canvas.DrawArc(rectF, 0, animValue, false, paint);
}
public void setValue(int animatedValue)
{
animValue = animatedValue;
Invalidate();
}
}
活动内容
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
var circleView = FindViewById<MyCustomView>(Resource.Id.circleView);
ValueAnimator valueAnimator = ValueAnimator.OfInt(0, 360);
valueAnimator.SetDuration(2000);
valueAnimator.AddUpdateListener(new AnimatorUpdateListener(this, circleView));
valueAnimator.Start();
}
private class AnimatorUpdateListener : Java.Lang.Object, ValueAnimator.IAnimatorUpdateListener
{
private MainActivity mainActivity;
private MyCustomView circleView;
public AnimatorUpdateListener(MainActivity mainActivity, MyCustomView circleView)
{
this.mainActivity = mainActivity;
this.circleView = circleView;
}
void ValueAnimator.IAnimatorUpdateListener.OnAnimationUpdate(ValueAnimator animation)
{
circleView.setValue((int)animation.AnimatedValue);
}
}