Android Animation Animation Simple example

Source: Internet
Author: User

Animation is an animated API that Android gives us, using animation we can achieve a series of animation effects, such as scaling animation, transparency animation, rotation animation, displacement animation, layout animation, frame animation and so on. Android Basic animation is divided into four types, namely tween Animation,frame animation,layout Animation,property Animation. Because the animation effect is not good with the picture to display, You can try the effect on your own hands. Here are the first three simple sample codes to help you get started with a simple animation.

(1) Tween Animation (transform animation)

Tween animation by effect can be divided into four kinds: alpha,scale,translate,rotate, they have a common attribute:

1. Duration: Duration of animation (MS);

2. Fillafter: Set to True, animation conversion is applied after the end of the animation;

3. Fillbefore: Set to True, animation conversion is applied before the animation starts;

4. Interpolator: Animation Insert (acceleration, deceleration plug);

5. RepeatCount: Animation repetition times;

6. Repeatmode: Positive sequence repeat/reverse repetition

7. Startoffset: The time interval between animations

  ①alpha (gradient transparency animation)

Mainactivity.java Main code:

ImageView image == animationutils.loadanimation (this, R.anim.alpha); Image.startanimation ( loadanimation);

Alpha.xml

<?XML version= "1.0" encoding= "Utf-8"?><Setxmlns:android= "Http://schemas.android.com/apk/res/android" >    <Alphaandroid:duration= "+"Android:fromalpha= "0.1"Android:toalpha= "1.0" >    </Alpha></Set>

②scale (gradient size scaling animation  )

Mainactivity.java Main code:

ImageView image == animationutils.loadanimation (this, R.anim.scale); Image.startanimation ( loadanimation);

Scale.xml  

<?XML version= "1.0" encoding= "Utf-8"?><Setxmlns:android= "Http://schemas.android.com/apk/res/android" >    < Scaleandroid:duration= "$"Android:fillafter= "false"Android:fromxscale= "0.0"Android:fromyscale= "0.0"Android:interpolator= "@android: Anim/accelerate_decelerate_interpolator"Android:pivotx= "50%"Android:pivoty= "50%"Android:toxscale= "1.0"Android:toyscale= "1.0" /></Set>

 ③translate (position move animation)

Mainactivity.java Main code:

ImageView image == animationutils.loadanimation (this, r.anim.translate); image.startanimation (loadanimation);

Translate.xml

<?XML version= "1.0" encoding= "Utf-8"?><Setxmlns:android= "Http://schemas.android.com/apk/res/android" >    <Translateandroid:duration= "+"Android:fromxdelta= "Ten"Android:fromydelta= "Ten"Android:toxdelta= "+"Android:toydelta= "+" /></Set>

 ④rotate (rotate animation)

Mainactivity.java Main code:

ImageView image == animationutils.loadanimation (this, r.anim.rotate); Image.startanimation ( loadanimation);

Rotate.xml

<?XML version= "1.0" encoding= "Utf-8"?><Setxmlns:android= "Http://schemas.android.com/apk/res/android" >    <Rotateandroid:duration= "+"android:fromdegrees= "0"Android:interpolator= "@android: Anim/accelerate_decelerate_interpolator"Android:pivotx= "50%"Android:pivoty= "50%"android:todegrees= "+360" /></Set>

(2) Layout Animation (placement animation)

Mainactivity.java Main code:

Intent intent=New Intent (mainactivity.  this, listactivity. class ); startactivity (intent);

Listactivity.java Code:

 Packagecom.example.caobotao.learnanimation;Importjava.util.ArrayList;Importjava.util.List;Importandroid.app.Activity;ImportAndroid.os.Bundle;Importandroid.view.animation.AnimationUtils;ImportAndroid.view.animation.LayoutAnimationController;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.ListView; Public classListactivityextendsactivity{PrivateListView ListView; @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub        Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.list_layout); ListView=(ListView) Findviewbyid (R.id.listview); List<string>list=NewArraylist<string>();  for(inti=0;i<20;i++) {List.add ("Example" +i); } arrayadapter<string>adapter=NewArrayadapter<string> ( This, Android.        R.layout.simple_list_item_1, list);        Listview.setadapter (adapter); Layoutanimationcontroller Lac=NewLayoutanimationcontroller (Animationutils.loadanimation ( This, r.anim.zoom_in));        Lac.setorder (Layoutanimationcontroller.order_normal);        Listview.setlayoutanimation (LAC);    Listview.startlayoutanimation (); }}

Zoom_in.xml

<?XML version= "1.0" encoding= "Utf-8"?><Setxmlns:android= "Http://schemas.android.com/apk/res/android"Android:interpolator= "@android: Anim/decelerate_interpolator" >    < Scaleandroid:duration= "+"Android:fromxscale= "0.1"Android:fromyscale= "0.1"Android:pivotx= "50%"Android:pivoty= "50%"Android:toxscale= "1.0"Android:toyscale= "1.0" />  <Alphaandroid:duration= "+"Android:fromalpha= "0"Android:toalpha= "1.0" /></Set>

(3) Frame Animation (frame animation)

Mainactivity.java Main code:

ImageView image == (animationdrawable) image.getdrawable (); Animationdrawable.start ();

Anim_list.xml

<?XML version= "1.0" encoding= "Utf-8"?><animation-listxmlns:android= "Http://schemas.android.com/apk/res/android" >    <Itemandroid:drawable= "@mipmap/one"android:duration= "$"/>    <Itemandroid:drawable= "@mipmap/two"android:duration= "$"/>    <Itemandroid:drawable= "@mipmap/three"android:duration= "$"/>    <Itemandroid:drawable= "@mipmap/four"android:duration= "$"/>    <Itemandroid:drawable= "@mipmap/five"android:duration= "$"/>    <Itemandroid:drawable= "@mipmap/six"android:duration= "$"/></animation-list>

Android Animation Animation Simple example

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.