Advanced article-user interface: 7.android Animation-Custom View animations

Source: Internet
Author: User

1. Transparency Animation

Animation of changes in the source code to achieve transparency:

(1) Add a button,id to btn in the layout file.

(2) Mainactivity.java

Findviewbyid (R.ID.BTN). Setonclicklistener (new  View.onclicklistener () {            @Override              publicvoid  OnClick (view view) {                new alphaanimation (0,1);                Aa.setduration (+);                View.startanimation (AA);            }        });

To configure transparency animation effects using an XML file:

(1) Create a new XML file under the Res folder Aa.xml (type is animation, where the folder is Anim):

<? XML version= "1.0" encoding= "Utf-8" ?> <  xmlns:android= "http://schemas.android.com/apk/res/android"    android: Fromalpha= "0"    android:toalpha= "1"    android:duration = "+" > </ Alpha >

(2) Mainactivity.java:

Findviewbyid (R.ID.BTN). Setonclicklistener (new  View.onclicklistener () {            @Override              publicvoid  OnClick (view view) {                view.startanimation (animationutils.loadanimation ( Mainactivity.  This , R.anim.aa));            }        );

Both of these methods can implement a button click and the button will show a change in transparency from 0 to 1 animation.

2. Rotate animation effect

The same button enables the rotation effect.

To implement a rotational animation in the source code:

Mainactivity.java:

 Public classMainactivityextendsappcompatactivity {Privaterotateanimation RA; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); RA=NewRotateanimation (0,360, animation.relative_to_self,0.5f,animation.relative_to_self,0.5f);//from 0 degrees to 360 degrees, the type of rotation center is relative to itself and the position is 0.5f,0.5fRa.setduration (1000); Findviewbyid (R.ID.BTN). Setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {view.startanimation (RA);    }        }); }}

To configure the rotation animation effect with an XML file:

Create a new XML file Ra.xml

<?XML version= "1.0" encoding= "Utf-8"?><Rotatexmlns:android= "Http://schemas.android.com/apk/res/android"android:fromdegrees= "0"android:todegrees= " the"android:duration= "the"Android:pivotx= "50%"Android:pivoty= "50%"></Rotate>

Mainactivity.java Code Ibid.

3. Displacement animation effect

The same makes the above buttons achieve the displacement animation effect.

To implement a rotational animation in the source code:

Mainactivity.java:

 Public classMainactivityextendsappcompatactivity {Privatetranslateanimation ta; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Ta=NewTranslateanimation (0,200,0,200);//Two 0 means moving the 200,200 to the right by 200,200 relative to the position of the element, rather than moving it relative to (0,0).Ta.setduration (1000); Findviewbyid (R.ID.BTN). Setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {view.startanimation (TA);    }        }); }}

To configure a displacement animation effect using an XML file:

Create a new XML file Ta.xml

<?XML version= "1.0" encoding= "Utf-8"?><Translatexmlns:android= "Http://schemas.android.com/apk/res/android"Android:fromxdelta= "0"Android:toxdelta= "$"Android:fromydelta= "0"Android:toydelta= "$"android:duraion= "+"></Translate>

Mainactivity.java Code Ibid.

4. Zoom animation effect

The same button enables the Zoom animation effect.

To implement scaled animations in source code:

Mainactivity.java:

 Public classMainactivityextendsappcompatactivity {Privatescaleanimation sa; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); SA=NewScaleanimation (0,1,0,1,animation.relative_to_self,0.5f,animation.relative_to_self,0.5f);//0,1,0,1 means that the x direction is scaled from 0 times to 1 time times, y is the same. The following parameter is the Zoom center.Sa.setduration (1000); Findviewbyid (R.ID.BTN). Setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {view.startanimation (SA);    }        }); }}

Use the XML file to configure the Zoom animation effect:

Create a new XML file Sa.xml

<?XML version= "1.0" encoding= "Utf-8"?>< Scalexmlns:android= "Http://schemas.android.com/apk/res/android"Android:fromxscale= "0"Android:toxscale= "1"Android:fromyscale= "0"Android:toyscale= "1"android:duration= "+"Android:pivotx= "50%"Android:pivoty= "50%"></ Scale>

Mainactivity.java Code Ibid.

5. Animation effect Blending

Similarly, the buttons are blended to animate the effect.

To implement mixed animations in source code:

Mainactivity.java:

 Public classMainactivityextendsappcompatactivity {PrivateAnimationset as; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); as=NewAnimationset (true); As.setduration (1000); Alphaanimation AA=NewAlphaanimation (0,1); //Aa.setduration (the);as.addanimation (AA); Translateanimation Ta=NewTranslateanimation (200,0,200,0); //Ta.setduration (the);//after the test, this sentence is not added.as.addanimation (TA); Findviewbyid (R.ID.BTN). Setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {view.startanimation (AS);    }        }); }}

To configure a blended animation effect using an XML file:

Create a new XML file Sa.xml

<?XML version= "1.0" encoding= "Utf-8"?><Setxmlns:android= "Http://schemas.android.com/apk/res/android"android:duration= "+"Android:shareinterpolator= "true">        <AlphaAndroid:fromalpha= "0"Android:toalpha= "1"/>    <TranslateAndroid:fromxdelta= "$"Android:toxdelta= "0"Android:fromydelta= "$"Android:toydelta= "0"/></Set>

Mainactivity.java Code Ibid.

Because do gif dynamic diagram to think about the trouble, so do not post. I have seen, the effect is very good haha.

Advanced article-user interface: 7.android Animation-Custom View animations

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.