我正在使用RotateAnimation来旋转我在Android中用作自定义循环旋转器的图像。下面是我的rotate_indefinite .xml文件,我把它放在res/anim/目录下:

<?xml version="1.0" encoding="UTF-8"?>
<rotate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:duration="1200" />    

当我将此应用到我的ImageView使用AndroidUtils.loadAnimation(),它的工作很棒!

spinner.startAnimation( 
    AnimationUtils.loadAnimation(activity, R.anim.rotate_indefinitely) );

唯一的问题是,图像旋转似乎在每个周期的顶部暂停。

换句话说,图像旋转360度,短暂停顿,然后再次旋转360度,等等。

我怀疑问题是动画使用默认的插值器,如android:iterpolator="@android:anim/accelerate_interpolator" (AccelerateInterpolator),但我不知道如何告诉它不插值动画。

我怎么能关闭插值(如果这确实是问题),使我的动画周期顺利?


当前回答

以下是对我来说工作良好的代码片段:

    RotateAnimation rotate = new RotateAnimation(
            0, 359,
            Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f
    );

    rotate.setDuration(1500);
    rotate.setRepeatCount(Animation.INFINITE);
    yourView.startAnimation(rotate);

检查一下,它不是360,而是构造函数中的359,因为0和360在同一点。

其他回答

In Android, if you want to animate an object and make it move an object from location1 to location2, the animation API figures out the intermediate locations (tweening) and then queues onto the main thread the appropriate move operations at the appropriate times using a timer. This works fine except that the main thread is usually used for many other things — painting, opening files, responding to user inputs etc. A queued timer can often be delayed. Well written programs will always try to do as many operations as possible in background (non main) threads however you can’t always avoid using the main thread. Operations that require you to operate on a UI object always have to be done on the main thread. Also, many APIs will funnel operations back to the main thread as a form of thread-safety.

视图都绘制在相同的GUI线程上,该线程也用于所有用户交互。

所以,如果你需要快速更新GUI,或者渲染花费太多时间,影响用户体验,那么就使用SurfaceView。

旋转图像示例:

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {
    private DrawThread drawThread;

    public MySurfaceView(Context context) {
        super(context);
        getHolder().addCallback(this);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {   
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        drawThread = new DrawThread(getHolder(), getResources());
        drawThread.setRunning(true);
        drawThread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        boolean retry = true;
        drawThread.setRunning(false);
        while (retry) {
            try {
                drawThread.join();
                retry = false;
            } catch (InterruptedException e) {
            }
        }
    }
}


class DrawThread extends Thread{
    private boolean runFlag = false;
    private SurfaceHolder surfaceHolder;
    private Bitmap picture;
    private Matrix matrix;
    private long prevTime;

    public DrawThread(SurfaceHolder surfaceHolder, Resources resources){
        this.surfaceHolder = surfaceHolder;

        picture = BitmapFactory.decodeResource(resources, R.drawable.icon);

        matrix = new Matrix();
        matrix.postScale(3.0f, 3.0f);
        matrix.postTranslate(100.0f, 100.0f);

        prevTime = System.currentTimeMillis();
    }

    public void setRunning(boolean run) {
        runFlag = run;
    }

    @Override
    public void run() {
        Canvas canvas;
        while (runFlag) {
            long now = System.currentTimeMillis();
            long elapsedTime = now - prevTime;
            if (elapsedTime > 30){

                prevTime = now;
                matrix.preRotate(2.0f, picture.getWidth() / 2, picture.getHeight() / 2);
            }
            canvas = null;
            try {
                canvas = surfaceHolder.lockCanvas(null);
                synchronized (surfaceHolder) {
                    canvas.drawColor(Color.BLACK);
                    canvas.drawBitmap(picture, matrix, null);
                }
            } 
            finally {
                if (canvas != null) {
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
        }
    }
}

活动:

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

尝试使用toDegrees="359",因为360°和0°是相同的。

无论我怎么尝试,我都无法使用代码(和setRotation)来平滑旋转动画。我最终做的是把度数变化做得非常小,以至于小的停顿都不明显。如果你不需要做太多的旋转,执行这个循环的时间可以忽略不计。效果是平滑的旋转:

        float lastDegree = 0.0f;
        float increment = 4.0f;
        long moveDuration = 10;
        for(int a = 0; a < 150; a++)
        {
            rAnim = new RotateAnimation(lastDegree, (increment * (float)a),  Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            rAnim.setDuration(moveDuration);
            rAnim.setStartOffset(moveDuration * a);
            lastDegree = (increment * (float)a);
            ((AnimationSet) animation).addAnimation(rAnim);
        }

你可以使用下面的代码:

view.animate().rotation(360.0f).setDuration(1000);

你是正确的关于AccelerateInterpolator;你应该使用线性插值器代替。

你可以使用内置的android.R.anim。linear_interpolator从你的动画XML文件与android:interpolator="@android:anim/线性插值"。

或者你可以在你的项目中创建你自己的XML插值文件,例如命名为res/anim/linear_interpolator.xml:

<?xml version="1.0" encoding="utf-8"?>
<linearInterpolator xmlns:android="http://schemas.android.com/apk/res/android" />

并添加到你的动画XML:

android:interpolator="@anim/linear_interpolator"

特别注意:如果你的旋转动画在一个集合内,设置插值器似乎不起作用。将旋转设置为顶部元素可以修复它。(这将节省你的时间。)