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

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() return 每次都是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;
            }
        });