java - How to start and stop an animation of a rotating ImageView with button
问题描述
I want to press play button and then spin a disc: imageRound
. At pause I want to stop the disc at current position, and then on play continue from current position.
I am trying to get each time the angle with imageRound.getRotation()
but it goes back to 0 each time. My code is as follows:
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)
这直接改变视图的旋转属性。
推荐阅读
- sql - SQL to Exclude a Value from a Sequence
- python - 基于 keras 1D ResNeXT 的网络的性能问题
- amazon-redshift - Redshift - Find average sales by month
- python-3.x - 从字典中的方法返回值合并不均匀的对应元素
- azure - Can't find my registered Azure AD Applications
- wordpress - WordPress RESTAPI - 限制未知
- flutter - 我无法打包在 pubspec.yaml 中获取包“英文单词”
- amazon-web-services - AWS auroradb 自动关闭连接
- mfc - 使用 MFC 将桌面屏幕截图捕获为矢量图像并放入剪贴板
- python - 重命名 GZIP 存档中的文件而不解压缩它