如何暂停和恢复动画?

问题描述 投票:1回答:1

我有一个圆形的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);
                }
            }
        });

谢谢 !

java android animation imageview
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.