Button Animation in Android
Here Below Example shows Animate Button in Android
Here Below Example shows Animate Button in Android
- translate
- alpha
- scale
- rotate
- complex
Example :
First Create Below Animation files and place in (res/anim folder)
For Translate Animation(anim_translate.xml)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<translate
android:fromXDelta="0"
android:toXDelta="100%p"
android:duration="500"
android:repeatCount="1"
android:repeatMode="reverse"/>
</set>
First Create Below Animation files and place in (res/anim folder)
For Translate Animation(anim_translate.xml)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<translate
android:fromXDelta="0"
android:toXDelta="100%p"
android:duration="500"
android:repeatCount="1"
android:repeatMode="reverse"/>
</set>
For Alpha Animation(anim_alpha.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:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="500"
android:repeatCount="1"
android:repeatMode="reverse" />
</set>
For Scale Animation(anim_scale.xml)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<scale
android:fromXScale="1.0"
android:toXScale="3.0"
android:fromYScale="1.0"
android:toYScale="3.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="500"
android:repeatCount="1"
android:repeatMode="reverse" />
</set>
For Rotate Animation(anim_rotate.xml)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator" >
<rotate
android:duration="500"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="1"
android:repeatMode="reverse"
android:startOffset="0"
android:toDegrees="360" />
</set>
For Complex Animation
Merge all above 4 animations
<?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:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="500"
android:repeatCount="1"
android:repeatMode="reverse" />
</set>
For Scale Animation(anim_scale.xml)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<scale
android:fromXScale="1.0"
android:toXScale="3.0"
android:fromYScale="1.0"
android:toYScale="3.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="500"
android:repeatCount="1"
android:repeatMode="reverse" />
</set>
For Rotate Animation(anim_rotate.xml)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator" >
<rotate
android:duration="500"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="1"
android:repeatMode="reverse"
android:startOffset="0"
android:toDegrees="360" />
</set>
For Complex Animation
Merge all above 4 animations
Android UI Layout(main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<Button
android:id="@+id/translate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:text="Translate"
/>
<Button
android:id="@+id/alpha"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:text="Alpha"
/>
<Button
android:id="@+id/scale"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:text="Scale"
/>
<Button
android:id="@+id/rotate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:text="Rotate"
/>
<Button
android:id="@+id/complex"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:text="Complex"
/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<Button
android:id="@+id/translate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:text="Translate"
/>
<Button
android:id="@+id/alpha"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:text="Alpha"
/>
<Button
android:id="@+id/scale"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:text="Scale"
/>
<Button
android:id="@+id/rotate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:text="Rotate"
/>
<Button
android:id="@+id/complex"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:text="Complex"
/>
</LinearLayout>
</RelativeLayout>
Android Activity Class(AndroidAnimButtonsActivity.java)
package com.androidsurya.androidbuttonanimation;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.widget.Button;
public class AndroidButtonAnimation extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Animation animTranslate = AnimationUtils.loadAnimation(this,
R.anim.anim_translate);
final Animation animAlpha = AnimationUtils.loadAnimation(this,
R.anim.anim_alpha);
final Animation animScale = AnimationUtils.loadAnimation(this,
R.anim.anim_scale);
final Animation animRotate = AnimationUtils.loadAnimation(this,
R.anim.anim_rotate);
Button btnTranslate = (Button) findViewById(R.id.translate);
Button btnAlpha = (Button) findViewById(R.id.alpha);
Button btnScale = (Button) findViewById(R.id.scale);
Button btnRotate = (Button) findViewById(R.id.rotate);
Button btnComplex = (Button) findViewById(R.id.complex);
btnTranslate.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
view.startAnimation(animTranslate);
}
});
btnAlpha.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
view.startAnimation(animAlpha);
}
});
btnScale.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
view.startAnimation(animScale);
}
});
btnRotate.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
view.startAnimation(animRotate);
}
});
btnComplex.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
AnimationSet sets = new AnimationSet(false);
sets.addAnimation(animAlpha);
sets.addAnimation(animScale);
sets.addAnimation(animRotate);
view.startAnimation(sets);
}
});
}
}
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.widget.Button;
public class AndroidButtonAnimation extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Animation animTranslate = AnimationUtils.loadAnimation(this,
R.anim.anim_translate);
final Animation animAlpha = AnimationUtils.loadAnimation(this,
R.anim.anim_alpha);
final Animation animScale = AnimationUtils.loadAnimation(this,
R.anim.anim_scale);
final Animation animRotate = AnimationUtils.loadAnimation(this,
R.anim.anim_rotate);
Button btnTranslate = (Button) findViewById(R.id.translate);
Button btnAlpha = (Button) findViewById(R.id.alpha);
Button btnScale = (Button) findViewById(R.id.scale);
Button btnRotate = (Button) findViewById(R.id.rotate);
Button btnComplex = (Button) findViewById(R.id.complex);
btnTranslate.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
view.startAnimation(animTranslate);
}
});
btnAlpha.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
view.startAnimation(animAlpha);
}
});
btnScale.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
view.startAnimation(animScale);
}
});
btnRotate.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
view.startAnimation(animRotate);
}
});
btnComplex.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
AnimationSet sets = new AnimationSet(false);
sets.addAnimation(animAlpha);
sets.addAnimation(animScale);
sets.addAnimation(animRotate);
view.startAnimation(sets);
}
});
}
}
Register Android Activity in Android Manifest File
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name="com.androidsurya.androidbuttonanimation.AndroidButtonAnimation"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name="com.androidsurya.androidbuttonanimation.AndroidButtonAnimation"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Output Screen Shot:
nice work thanx
ReplyDelete