在imageview中旋转动画

时间:2014-09-29 13:41:42

标签: android android-animation

我尝试用animation.i写一些代码来旋转imageview。这是我的xml动画代码

<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" />

这是一个java代码

     final ImageView myImage = (ImageView)findViewById(R.id.rot);
     final Animation myRotation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotator);

     myImage.setAnimation(myRotation);

     Handler ha=new Handler();
     ha.postDelayed(new Runnable() {

        @Override
        public void run() {
             myRotation.setDuration(1000);

        }

    }, 1000);
     myRotation.cancel();

我尝试在持续时间结束时取消动画。但我可以旋转imageview但我无法取消aniamtion 我怎么能解决我的问题?如果有人知道解决方案,请帮助我

2 个答案:

答案 0 :(得分:1)

您不需要为动画使用处理程序。

请使用:

动画xml:

<rotate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="1" 
    android:duration="1000"/>

活动:

myImage.startAnimation(myRotation);  //this will stop animation after 1000 miliseconds as `repeatCount` is 1.

希望它有所帮助。

答案 1 :(得分:0)

试试这些:

myImage.startAnimation(myRotation);  // to start animation
myImage.cancelAnimation(); // to cancel animation
相关问题