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

时间:2019-03-24 11:42:25

标签: 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;
                }
            });

2 个答案:

答案 0 :(得分:1)

imageRound.getRotation()每次返回0吗? RotateAnimation不会更改视图的属性。使用ObjectAnimator.ofFloat(imageview ,"rotation", 0f, 360f)这将直接更改视图的旋转属性。

答案 1 :(得分:0)

在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;
            }
        });