Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

animating ImageButton in android?

I am really new to animations in android (and pretty much anything else). Is there a way to animate an ImageButton? I just want to rotate the button for sometimes. Thats all. Any help ?

Thanks.

like image 758
Ahsan Avatar asked Sep 27 '11 04:09

Ahsan


People also ask

How do you animate a view?

You can use the view animation system to perform tweened animation on Views. Tween animation calculates the animation with information such as the start point, end point, size, rotation, and other common aspects of an animation.

What is Property animation in Android?

A property animation changes a property's (a field in an object) value over a specified length of time. To animate something, you specify the object property that you want to animate, such as an object's position on the screen, how long you want to animate it for, and what values you want to animate between.


2 Answers

Try this code snippet.

rotate.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate

    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="0"
    android:duration="1000" />

</set>

in java file

ImageButton imgbt = (ImageButton)findViewById(R.id.your_id);
Animation ranim = (Animation)AnimationUtils.loadAnimation(context, R.anim.rotate);
imgbt.setAnimation(ranim);
like image 200
Niranj Patel Avatar answered Sep 21 '22 05:09

Niranj Patel


rotate.xml

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

Java Code :

RotateAnimation rotateAnimation = (RotateAnimation) AnimationUtils.loadAnimation(context,R.anim.rotate);
view.startAnimation(rotateAnimation);
like image 29
Sankar V Avatar answered Sep 20 '22 05:09

Sankar V