Android------animation effects in Android

Source: Internet
Author: User

There are four animation effects in Android:

Alphaanimation: Transparency Animation effect

Scaleanimation: Zoom animation effect

Translateanimation: Displacement animation effect

Rotateanimation: Rotate animation effect

1. Transparent animation effect

public class Mainactivity extends Activity {private alphaanimation aa; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); AA = new Alphaanimation (1, 0); aa.setduration (3000);//Set Listener event Aa.setanimationlistener (new Animationlistener () {@ overridepublic void Onanimationstart (Animation Animation) {toast.maketext (Mainactivity.this, "start", 0). Show (); @Overridepublic void Onanimationrepeat (Animation Animation) {toast.maketext (mainactivity.this, "repeat", 0). Show (); @Overridepublic void Onanimationend (Animation Animation) {toast.maketext (Mainactivity.this, "end", 0). Show ();}); Findviewbyid (R.ID.BTN). Setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) { V.startanimation (AA);}});}}


Complete the animation with an XML configuration file

Create an XML file: Right-click res-> Select android XML files

A Anim folder is generated in the Res directory after the creation is complete

Aa.xml:

<?xml version= "1.0" encoding= "Utf-8"? ><alpha xmlns:android= "Http://schemas.android.com/apk/res/android"    android:duration= "android:fromalpha="    1 "    android:toalpha=" 0 "></alpha>


The activity only needs to declare the alphaanimation and invoke it in the listener event of the button.

V.setanimation (Animationutils.loadanimation (MAINACTIVITY.THIS,R.ANIM.AA));


The effect is the same.

2. Zoom animation effect

public class Mainactivity extends Activity {private scaleanimation sa; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); sa = new Scaleanimation (0, 1, 0, 1, animation.relative_to_self, 0.5f,animation.relative_to_self, 0.5f); Sa.setduration (2000); Findviewbyid (R.ID.BTN). Setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) { V.startanimation (SA);}});}}


Through the XML file:

<?xml version= "1.0" encoding= "Utf-8"? ><scale xmlns:android= "Http://schemas.android.com/apk/res/android"    android:duration= "android:fromxscale="    0 "    android:fromyscale=" 0 "    android:pivotx=" 50% "    android:pivoty= "50%"    android:toxscale= "1"    android:toyscale= "1" ></scale>


3. Displacement animation effect

public class Mainactivity extends Activity {private translateanimation ta; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); ta = new Translateanimation (0, 0, ta.setduration), Findviewbyid (R.ID.BTN) Setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {v.startanimation (TA);//V.startanimation ( Animationutils.loadanimation (//Mainactivity.this, R.anim.ta));}});}}


Xml:

<?xml version= "1.0" encoding= "Utf-8"? ><translate xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:duration=" "    android:fromxdelta=" 0 "    android:fromydelta=" 0 "    android: Toxdelta= "Android:toydelta="    ></translate>


4. Rotate animation effect

public class Mainactivity extends Activity {private rotateanimation ra; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);//RA = new Rotateanimation (0, N/a);//RA = new Rotateanimation (0, N/A, +), RA = new Rotateanimation (0, animation.relative_t O_self, 0.5f,animation.relative_to_self, 0.5f); ra.setduration (+); Findviewbyid (R.ID.BTN). Setonclicklistener ( New Onclicklistener () {@Overridepublic void OnClick (View v) {v.startanimation (RA);}});}}


Animation blending:

Animationset

public class Mainactivity extends Activity {animationset as; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); as = new Animationset (true); As.setduration (2000); Alphaanimation AA = new Alphaanimation (0, 1); Aa.setduration (2000); Translateanimation ta = new translateanimation (0, 0, $), ta.setduration (+); as.addanimation (TA); As.addanimation (AA); Findviewbyid (R.ID.BTN). Setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick ( View v) {v.startanimation (AS);}});}}


To create an XML file select Set:

<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android"    android:duration= "android:shareinterpolator=" "    true" >    <alpha        android:fromalpha= "0"        android:toalpha= "1"/>    <translate        android:fromxdelta= "0"        android:fromydelta= "0"        Android:toxdelta= "Android:toydelta="        /></set>


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android------animation effects in Android

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.