如何暂停和恢复动画?

Bin*_*Cao 2 java animation android 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>
Run Code Online (Sandbox Code Playgroud)

这是 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);
                }
            }
        });
Run Code Online (Sandbox Code Playgroud)

谢谢 !

Che*_*tty 6

试试这个方法

 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);
      }

}
Run Code Online (Sandbox Code Playgroud)

取决于你想在你的 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
Run Code Online (Sandbox Code Playgroud)