Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 如何在Canvas中旋转圆形动画大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在 android上的画布中连续旋转圆圈.我正在使用画布绘制圆圈,我不断旋转圆圈.如果可能的话,这是可能的
代码或示例可以帮助我非常感谢!

这是我在画布上绘制圆圈的代码

import android.app.Activity;
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.os.bundle;
    import android.view.View;

    public class AnimationActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        protected void onCreate(Bundle savedInstanceStatE)
        {
            super.onCreate(savedInstanceStatE);
            setContentView(new SampleView(this));
        }


    public class SampleView extends View
    {
        public SampleView(Context context)
        {
            super(context);
            // TODO Auto-generated constructor stub
        }

        @Override
        protected void onDraw(Canvas canvas)
        {
             Paint mPaint = new Paint();
             mPaint.setStyle(Paint.Style.stroke);
             mPaint.setstrokeWidth(10);
             mPaint.setColor(Color.RED);
             canvas.drawCircle(75,75,mPaint);
        }
    }
  }

提前致谢!

解决方法

您可以使用动画旋转您绘制的圆(使用Canvas).以下代码有效.我修改你的代码添加了必要的更改.

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.bundle;
import android.view.View;

public class AnimationActivity extends Activity {

    public class SampleView extends View {

        Paint mPaint = new Paint();
        private Animation anim;

        public SampleView(Context context) {
            super(context);
            mPaint.setStyle(Paint.Style.stroke);
            mPaint.setstrokeWidth(10);
            mPaint.setColor(Color.RED);
        }

        private void createAnimation(Canvas canvas) {
            anim = new RotateAnimation(0,360,getWidth()/2,getHeight()/2);
            anim.setRepeatMode(Animation.RESTART);
            anim.setRepeatCount(Animation.INFINITE);
            anim.setDuration(10000L);
            startAnimation(anim);
       }

        protected void onDraw(Canvas canvas) {

             int cx = getWidth()/2; // x-coordinate of center of the screen
             int cy = getHeight()/2; // y-coordinate of the center of the screen

             // Starts the animation to rotate the circle.
             if (anim == null) 
               createAnimation(canvas)

             canvas.drawCircle(cx,cy,150,mPaint); // drawing the circle.
        }        
    }

    protected void onCreate(Bundle savedInstanceStatE) {
         super.onCreate(savedInstanceStatE);
         setContentView(new SampleView(this));
    }
}

请享用!

大佬总结

以上是大佬教程为你收集整理的android – 如何在Canvas中旋转圆形动画全部内容,希望文章能够帮你解决android – 如何在Canvas中旋转圆形动画所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。