使用动画在画布上画一个圆圈 [英] Draw a circle on canvas using Animation

查看:35
本文介绍了使用动画在画布上画一个圆圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用动画创建一个圆圈.

I need to create a circle using animation.

我需要将持续时间设置为 2000,直到我的圆圈以 360 度结束.

I need to set duration 2000 till my circle will be end at 360 degrees.

课堂内容

 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, animValue, i++, 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, 0);
        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);
        }
    }

问题是我的圆没有在圆的末端结束而是在中间的某个地方停止.线没有在我的圆的末端结束......

The problem is that my circle doesnt finish at the end of circle it stops somewhere in middle.The line doesnt finish in the end of my circle...

[![Img][1]][1]

[![Img][1]][1]

[1]: https://i.stack.imgur.com/oAz1Q.png

推荐答案

可以试试下面的代码,效果是这样的

You can try the following code, and the effect is like this

课堂内容

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);

        }
    }

这篇关于使用动画在画布上画一个圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆