Hello friends, In this android tutorial i explained how to give animations using XML at time of activity lunching.Creating animation is very simple to give animation effect on object like textview,imageview,label etc. Animations can be performed through either XML or JAVA code.
To know more about xml animation Click Here.
To know more about xml animation Click Here.
This file should be located res/anim/animation.xml. If you don’t have anim folder in your res/ directory create one. Following is example of simple animation using XML and it will play at lunching activity.
We need for this example:
1) anims.xml       // located res/anim/
2) bounce.xml     // located res/anim/
3) activity_main.xml
4) MainActivity.java
Step 1: Write code into anims.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator" >
    <alpha
        android:duration="1000"
        android:fromAlpha="1.1"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:toAlpha="0.1" />
    <translate
        android:duration="500"
        android:fromXDelta="0"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:toXDelta="100%p" />
</set>
Step 2: Write code into bounce.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:interpolator="@android:anim/bounce_interpolator" >
    <scale
        android:duration="1000"
        android:fromXScale="1.0"
        android:fromYScale="0.0"
        android:toXScale="1.0"
        android:toYScale="1.0" />
</set>
Step 3: Write code into activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <Button
        android:id="@+id/errorDemoButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="anim 1" />
    <Button
        android:id="@+id/createButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="anim 2" />
</LinearLayout>
Step 4: Write code into MainActivity.java
package dev.androidapplink.animatstartactivity;
import android.os.Bundle;
import android.app.Activity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
public class MainActivity extends Activity {
 // create variable anim xml file
 Animation anims_bounce;
 Animation anims_anims;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  // load the animation
  anims_bounce = AnimationUtils.loadAnimation(getApplicationContext(),
    R.anim.bounce);
  anims_anims = AnimationUtils.loadAnimation(getApplicationContext(),
    R.anim.anims);
  // its called when activity is lunching
  Button createButton2 = (Button) findViewById(R.id.createButton);
  createButton2.startAnimation(anims_bounce);
  Button errorDemoButton2 = (Button) findViewById(R.id.errorDemoButton);
  errorDemoButton2.startAnimation(anims_anims);
 }
}
Step 5:Now Run Your Project:
Button will come with animation at activity lunching:

 
 
 
 
 
 
 
 
 
 
 
No comments :
Post a Comment