Reputation: 1
I want to make the button rotate forever, right after pressing it. But it doesn't work
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);
}
}
});
}`
Upvotes: 0
Views: 160
Reputation: 2222
When you press a button, it will be rotated infinitely.
rotate.xml (res > anim > rotate.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>
Your Button Listener
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);
}
});
Upvotes: 1
Reputation: 1195
use below code on click
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);
Upvotes: 0