Adding animations to app's UI creates a very interactive enviornment and an appealing attraction .
Using XML , the android animations are very simple as you just need a few bunch of files and a few lines of code :
Step 1 -
Create a XML file - res ⇒ anim ⇒ fade_in.xml
If you dont got any "anim" folder , just create a one under "res"
The fade in / fade out android animations just increase or decrease there's alpha value respectively.
Following is the code for XML file -
Fade In :
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
set
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:fillAfter
=
"true"
>
<
alpha
android:duration
=
"1000"
android:fromAlpha
=
"0.0"
android:interpolator
=
"@android:anim/accelerate_interpolator"
android:toAlpha
=
"1.0"
/>
</
set
>
Fade Out XML is just opposite of Fade In ( yes that much easy ):
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
set
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:fillAfter
=
"true"
>
<
alpha
android:duration
=
"1000"
android:fromAlpha
=
"1.0"
android:interpolator
=
"@android:anim/accelerate_interpolator"
android:toAlpha
=
"0.0"
/>
</
set>
Step 2 - LOAD THE ANIMATION
Create yours LoadAnimation.java activity and call an Animation object by loading fade_in.xml file using a static function of AnimationUtils.loadAnimation()
public
class
FadeInActivity
extends
Activity
{
ImageView
image;
// Animation
Animation fadeInAnim;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super
.onCreate(savedInstanceState);
setContentView(R.layout.your_layout_reference);
image
= (ImageView) findViewById(R.id.your_img_id);
// load the animation
f
adeinAnim = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_in);
// start the animation
image.startAnimation(fadeInAnim;);
}
Thats all folks
XML ATTRIBUTE MEANING -
You should have a feeling about following XML animation attributes for getting varities of cool animation effects . Different attributes causes major animation differention providing platform for creativity to be exposed :
android:duration – How long the animation should last
android:startOffset – Waiting time for animation to start. Used mainly for sequential animation effects
android:interpolator – Rate of change in animation
android:fillAfter – This defines whether to apply the animation transformation after the animation completion or not. If it sets to false the element changes to its previous state after the animation. This attribute should be use with <set> node
android:repeatMode – Repeating yours animation
android:repeatCount – Number of repetitions on animation. Setting the value as "infinite" will make animation to popup in infinite loop
Enjoy
No comments:
Post a Comment