[英]How to start and stop an animation of a rotating ImageView with button
我想按播放按钮,然后旋转光盘: imageRound
。 暂停时,我想将光盘停在当前位置,然后在播放时从当前位置继续。
我试图每次获取与imageRound.getRotation()
的角度,但每次都回到0。 我的代码如下:
playButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (playShow) {
playButton.setBackgroundResource(R.drawable.play_pressed);
} else {
playButton.setBackgroundResource(R.drawable.pause_pressed);}
return true; //handle the touch event
case MotionEvent.ACTION_UP:
if (playShow) {
playButton.setBackgroundResource(R.drawable.pause_default);
RotateAnimation rotate = new RotateAnimation(imageRound.getRotation(), 360, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(5000);
imageRound.startAnimation(rotate);
playShow=false;
} else {
playButton.setBackgroundResource(R.drawable.play_default);
imageRound.setRotation(imageRound.getRotation()); //(Not working) Set angle and stop animation
imageRound.clearAnimation();
playShow=true;
}
return true; // handle the touch event
}
return false;
}
});
imageRound.getRotation()
每次返回0吗? RotateAnimation不会更改视图的属性。 使用ObjectAnimator.ofFloat(imageview ,"rotation", 0f, 360f)
这将直接更改视图的旋转属性。
在SanthoshKumar的帮助下,这是可以正常工作的代码,可以解决我的问题:
playButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (playShow) {
playButton.setBackgroundResource(R.drawable.play_pressed);
} else {
playButton.setBackgroundResource(R.drawable.pause_pressed);
}
return true; //handle the touch event
case MotionEvent.ACTION_UP:
if (playShow) {
playButton.setBackgroundResource(R.drawable.pause_default);
anim = ObjectAnimator.ofFloat(imageRound, "rotation", imageRound.getRotation(), 360f);
anim.setDuration(20000);
anim.setInterpolator(new LinearInterpolator());
anim.start();//
playShow = false;
if (mediaPlayer != null) {
mediaPlayer.start();
}
} else {
playButton.setBackgroundResource(R.drawable.play_default);
anim.cancel();
playShow = true;
if (mediaPlayer != null) {
mediaPlayer.pause();
}
}
return true; // handle the touch event
}
return false;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.