Android uses XML to make an in-depth analysis of the animated UI _android

Source: Internet
Author: User
Tags stub time interval

Effect: http://www.56.com/u82/v_OTM4MDk5MTk.html
Step one: Create the Anim folder to place the animated XML file
Under the Res folder, create a Anim subfolder.



Step two: Load the animation
Then create a animation class in the activity and then load the animated XML with the Animationutils class

Copy Code code as follows:

Animation Animfadein;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_fadein);

Txtmessage = (TextView) Findviewbyid (r.id.txtmessage);
btnstart = (Button) Findviewbyid (R.id.btnstart);

Load animation
Animfadein = Animationutils.loadanimation (Getapplicationcontext (),
R.ANIM.FADE_IN);



Step Three: Set up the animation listener
If you want to monitor an animated event, such as start, end, etc., you need to implement the Animationlistener listener and rewrite the following methods.
onanimationend (Animation Animation)-called at the end of the animation
Onanimationrepeat (Animation Animation)-Called when the animation repeats
Onaniamtionstart (Animation Animation)-called when the animation starts
Copy Code code as follows:

@Override
public void Onanimationend (Animation Animation) {
Use after the end of the animation

Check for fade in animation
if (animation = = Animfadein) {
Toast.maketext (Getapplicationcontext (), "Animation Stopped",
Toast.length_short). Show ();
}

}

@Override
public void Onanimationrepeat (Animation Animation) {
Used when animation is repeated

}

@Override
public void Onanimationstart (Animation Animation) {
When the animation starts to use

}

The final step: let the animation move up. You can invoke the Startanimation method using any UI element.
The following is called by a textview element.
txtmessage.startanimation (Animfadein);
Complete code:
Copy Code code as follows:

Fadeinactivity (Fade in animation)
? package com.chaowen.androidanimations;

Import INFO.ANDROIDHIVE.ANDROIDANIMATIONS.R;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import android.view.animation.Animation;
Import Android.view.animation.AnimationUtils;
Import Android.view.animation.Animation.AnimationListener;
Import Android.widget.Button;
Import Android.widget.TextView;
Import Android.widget.Toast;

/**
*
* @author Chaowen
*
*/
public class Fadeinactivity extends activity implements Animationlistener {

TextView Txtmessage;
Button btnstart;

Animation Animfadein;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_fadein);

Txtmessage = (TextView) Findviewbyid (r.id.txtmessage);
btnstart = (Button) Findviewbyid (R.id.btnstart);

Load animation
Animfadein = Animationutils.loadanimation (Getapplicationcontext (),
R.ANIM.FADE_IN);

Set up listening
Animfadein.setanimationlistener (this);

Button
Btnstart.setonclicklistener (New View.onclicklistener () {

@Override
public void OnClick (View v) {
Txtmessage.setvisibility (view.visible);

Start animation
Txtmessage.startanimation (Animfadein);
}
});

}

@Override
public void Onanimationend (Animation Animation) {
Use after the end of the animation

Check for fade in animation
if (animation = = Animfadein) {
Toast.maketext (Getapplicationcontext (), "Animation Stopped",
Toast.length_short). Show ();
}

}

@Override
public void Onanimationrepeat (Animation Animation) {
Used when animation is repeated

}

@Override
public void Onanimationstart (Animation Animation) {
When the animation starts to use

}

}

some important XML attributes
Important XML Animation Properties
android:duration The duration of the animation, in milliseconds
Android:startoffset the time interval between animations, starting from the last time the animation stopped to perform the next animation
Android:interpolator specifies an animation for the insertion device
Android:fillafter when set to True, the animation is applied after the animation is finished
Android:repeatmode defines repetitive behavior
Number of repetitions of android:repeatcount animation

Here are some basic animated XML.
Fade in: Fading in
Alpha is a gradient transparency effect, with values from 0 to 1
Fade_in.xml
Copy Code code as follows:

<?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: Fade out
In the exact opposite of fade in, the value is from 1 to 0.
Fade_out.xml
Copy Code code as follows:

<?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>

Cross fading: Cross fade in and out
Use both fade in and fade out to achieve crossover effects
Copy Code code as follows:

public class Crossfadeactivity extends activity implements Animationlistener {

TextView TxtMessage1, TxtMessage2;
Button btnstart;


Animation Animfadein, Animfadeout;

@Override
protected void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_crossfade);

TxtMessage1 = (TextView) Findviewbyid (r.id.txtmessage1);
TxtMessage2 = (TextView) Findviewbyid (r.id.txtmessage2);
btnstart = (Button) Findviewbyid (R.id.btnstart);

Load animations
Animfadein = Animationutils.loadanimation (Getapplicationcontext (),
R.ANIM.FADE_IN);
Animfadeout = Animationutils.loadanimation (Getapplicationcontext (),
R.anim.fade_out);

Set Animation listeners
Animfadein.setanimationlistener (this);
Animfadeout.setanimationlistener (this);

button click event
Btnstart.setonclicklistener (New View.onclicklistener () {

@Override
public void OnClick (View v) {

Txtmessage2.setvisibility (view.visible);

Txtmessage2.startanimation (Animfadein);


Txtmessage1.startanimation (animfadeout);
}
});

}

@Override
public void Onanimationend (Animation Animation) {



if (animation = = Animfadeout) {
Txtmessage1.setvisibility (View.gone);
}

if (animation = = Animfadein) {
Txtmessage2.setvisibility (view.visible);
}

}

@Override
public void Onanimationrepeat (Animation Animation) {
TODO auto-generated Method Stub

}

@Override
public void Onanimationstart (Animation Animation) {
TODO auto-generated Method Stub

}

}

BLink: Looming, cool
Blink.xml
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<set xmlns:android= "Http://schemas.android.com/apk/res/android" >
<alpha android:fromalpha= "0.0"
Android:toalpha= "1.0"
Android:interpolator= "@android: Anim/accelerate_interpolator"
android:duration= "600"
Android:repeatmode= "Reverse"
Android:repeatcount= "Infinite"/>
</set>

Zoom in: Zooming in
Zoom_in.xml
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<set xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:fillafter= "true" >

<scale
Xmlns:android= "Http://schemas.android.com/apk/res/android"
android:duration= "1000"
android:fromxscale= "1"
android:fromyscale= "1"
android:pivotx= "50%"
Android:pivoty= "50%"
Android:toxscale= "3"
android:toyscale= "3" >
</scale>

</set>

Zoom out: Shrinking
Zoom_out.xml
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<set xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:fillafter= "true" >

<scale
Xmlns:android= "Http://schemas.android.com/apk/res/android"
android:duration= "1000"
Android:fromxscale= "1.0"
Android:fromyscale= "1.0"
android:pivotx= "50%"
Android:pivoty= "50%"
android:toxscale= "0.5"
android:toyscale= "0.5" >
</scale>

</set>

Rotate: Rotate
Rotate.xml
Copy Code code as follows:

<?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:duration= "600"
Android:repeatmode= "Restart"
Android:repeatcount= "Infinite"
Android:interpolator= "@android: Anim/cycle_interpolator"/>

</set>

There are a few are no longer listed, interested in the source look. Click to download

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.