Android 如何在画布中旋转圆动画

Android 如何在画布中旋转圆动画,android,android-canvas,Android,Android Canvas,我想在android上的画布上连续旋转圆圈。我正在用画布画圆,我在不断地旋转圆。这是可能的,如果可能的话,怎么做 用代码或例子可以帮助我,非常感谢 以下是我在画布上绘制圆圈的代码: import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.

我想在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, 75, mPaint);
        }
    }
  }
提前谢谢

canvas.rotate(-rotate_angle, rotate_center_x, rotate_center_y);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
RectF oval3 = new RectF(rotate_center_x-150, rotate_center_y-50, rotate_center_x+150, rotate_center_y+50);
canvas.drawOval(oval3, paint);
//resume original angle
canvas.rotate(rotate_angle, rotate_center_x, rotate_center_y);

您可以使用动画旋转绘制的圆(使用画布)。下面的代码可以工作。我已经修改了您的代码并添加了必要的更改

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));
    }
}
享受吧

使用旋转动画