我想按播放按钮然后旋转光盘: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)
这会直接更改视图的rotation属性。
在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;
}
});