有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何暂停和恢复动画?

我有一个圆形图像视图,我可以通过单击按钮开始旋转它,但我想在它停止在该位置时再次按下按钮。我该怎么做

这是Animation

 <?xml version="1.0" encoding="utf-8"?> 
<set xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:interpolator="@安卓:anim/linear_interpolator">
    <rotate
        安卓:fromDegrees="0"
        安卓:pivotX="50%"
        安卓:pivotY="50%"
        安卓:toDegrees="360"
        安卓:duration="10000"
        安卓:repeatCount="infinite"
        安卓:repeatMode="restart" />
 </set>

这是MainActivity

Animation cd1;
ImageView imageview;
Button button1;

cd1 = AnimationUtils.loadAnimation(this, R.anim.cd_player1);

button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageview.startAnimation(cd1);
                }
            }
        });

谢谢!


共 (1) 个答案

  1. # 1 楼答案

    试着这样

     private ObjectAnimator anim;
    
          @Override
          protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.property_animations_flow);
    
            ImageView someImage = (ImageView) findViewById(R.id.some_image);
    
            anim = ObjectAnimator.ofFloat(someImage, "rotation", 0, 360);
            anim.setDuration(1000);
            anim.setRepeatCount(5);
            anim.setRepeatMode(ObjectAnimator.RESTART);
          }
    
    }
    

    这取决于你想在onclick中调用什么

    anim.start()   // start the animation from the beginning
    anim.end()     // end the animation
    anim.cancel()  // cancel the animation 
    anim.pause()   // added in API 19; pause the animation
    anim.resume()  // added in API 19; resume a paused animation