繁体   English   中英

Android中的无限按钮动画

[英]Infinite button animation in Android

我想使按钮永久旋转,就在按下它之后。 但这不起作用

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button2 = (Button) findViewById(R.id.button2);





    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            button2.setClickable(false);
            while(true) {
                button2.setRotation(button2.getRotation() + 1);

            }
        }
    });
}`

当您按下一个按钮时,它将无限旋转。

rotation.xml(res> anim> rotation.xml)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <rotate
        android:duration="500"
        android:pivotX="50%"
        android:pivotY="50%"
        android:startOffset="0"
        android:fromDegrees="0"
        android:toDegrees="360"
        android:repeatCount="infinite"/>
</set>

您的按钮监听器

btn.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
     Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
     animation.setFillAfter(true);
     btn.startAnimation(animation);
  }
});

单击使用以下代码

RotateAnimation rotate = new RotateAnimation(
            0, 360, 
            Animation.RELATIVE_TO_SELF, 0.5f, 
            Animation.RELATIVE_TO_SELF, 0.5f
        );
        rotate.setDuration(ROTATE_ANIMATION_DURATION);
        rotate.setRepeatCount(Animation.INFINITE);
        button2.startAnimation(rotate);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM