如何暂停和恢复动画?

时间:2018-01-03 10:32:05

标签: java android animation imageview

我有一个圆形的Imageview,我可以通过点击按钮开始旋转它,但是当它停在那个位置时我想再次按下按钮。我怎么能这样做?

这是Animation

 <?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator">
    <rotate
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="360"
        android:duration="10000"
        android:repeatCount="infinite"
        android:repeatMode="restart" />
 </set>

这是MainActivity

Animation cd1;
ImageView imageview;
Button button1;

cd1 = AnimationUtils.loadAnimation(this, R.anim.cd_player1);

button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageview.startAnimation(cd1);
                }
            }
        });

谢谢!

1 个答案:

答案 0 :(得分:1)

试试这种方式,

 private ObjectAnimator anim;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.property_animations_flow);

        ImageView someImage = (ImageView) findViewById(R.id.some_image);

        anim = ObjectAnimator.ofFloat(someImage, "rotation", 0, 360);
        anim.setDuration(1000);
        anim.setRepeatCount(5);
        anim.setRepeatMode(ObjectAnimator.RESTART);
      }

}

取决于您想要在onclick中调用的内容

anim.start()   // start the animation from the beginning
anim.end()     // end the animation
anim.cancel()  // cancel the animation 
anim.pause()   // added in API 19; pause the animation
anim.resume()  // added in API 19; resume a paused animation