Simple animation in android

Hi, here is a way to create a very simple animation in android.

Probably you know that there are tow types of animations in android.
1. tween animation and 2. Frame animation

I'm describing a simple example of tween animation. Let's start.

suppose we have a ImageView on a LinearLayout. we want to create a animation so that the image will move from a point to another point. First we have to create a xml file in the res/layout folder in which we'll show the image. It may be something like this -


<?xml version="1.0" encoding="utf-8"?

>


<
LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center" android:layout_gravity="center" android:layout_height="fill_parent" android:layout_width="fill_parent"



>


<
ImageView android:background="@drawable/animtest" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/myimg"





>


<

/ImageView



>


<
/LinearLayout



>



here "animtest" is a 20X20px png image in the drawable folder. edit this for your purposes or create a 20X20px png image and save it in the drawable folder if you want to use this code without modifing :)
you can just copy and paste this code and save it as main.xml ( or as you wish) in res/layout folder of your eclipse project.

no we'll create the animation. To create a animation we have to create a new folder named "anim" inside the "res" folder. then create a xml file inside res/anim and save the file by anyname. Obviusly the file extension should be .xml Here i'm saving the xml file as anim_test.xml

now open the xml file and paste this little code -


<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator">
<translate
android:fromXDelta="-10"
android:fromYDelta="-10"
android:toXDelta="100"
android:toYDelta="100"
android:duration="600"
/>
</set>


note that this line "
<?xml version="1.0" encoding="utf-8"?>" shouldn't be here. otherwise you'll get errors. if you create xml file using eclipse, it'll write this line automatically for you. you must delete it.

we are about to finish. we have created the descrption of our animation. please read the android documentation if you want to understand the meaning of the xml tags in detail. in short the tag says that it's a translate animation i.e the object/view on which this animation will be applied will move from a point (android:fromXDelta , android:fromYDelta) to (android:toXDelta, android:toYDelta) in " android:duration" milliseconds. The current location of the view is 0,0 . minus sign indicates that this point is above the current location.

Now, we have to use this animation described in res/anim in our java code. this is a way to do this -


import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class myAnimation extends Activity {
private ImageView tv;
private Animation an;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

tv = (ImageView)findViewById(R.id.myimg);
an = AnimationUtils.loadAnimation(this,R.anim.anim_test);
tv.startAnimation(an);
}
}

that's all.

Comments

Popular posts from this blog

Run tasks in background in Spring

Conditional field inclusion in Jackson and Spring Boot

How to configure Wildfly 10 to use MySQL