java - 如何使用按钮启动和停止旋转 ImageView 的动画

标签 java android animation rotation

我想按播放按钮,然后旋转光盘: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 不会更改 View 的属性。使用 ObjectAnimator.ofFloat(imageview ,"rotation", 0f, 360f) 这会直接更改 View 的旋转属性。

关于java - 如何使用按钮启动和停止旋转 ImageView 的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55323419/

相关文章:

osgi - 如何定义最低 GRE 要求(1.5 _ 或更高 _)?

java - Java中的方程重排

java - fest 和带有自定义 map 的断言-j 之间的细微差别

java - 为什么我在 Java/Android 中得到空 SQLite 字段的 "0.0"?

android - 点击外部edittext失去焦点

animation - 将刚体模拟保存为动画

java - 500 java.lang.OutOfMemoryError 后重定向

java - eclipse 在我的项目旁边显示感叹号

java - FAB 到 AlertDialog 的动画 xml?

javascript - 悬停时动画表格背景 - Jquery