Android:animation Animation

Source: Internet
Author: User
Tags event listener

Animation is divided into two main categories: one is the gradient animation tweened animation, one is the frame animation Frame-by-frame animation. The gradient animation is to use a frame image to produce four effects: 1, alpha,2, rotate,3, scale,4, translates, can be implemented by code or XML file two ways. One-frame animation is the effect of a series of images moving through a quick play. There are also some animationset, Animationlistener, layoutanimationcontroller which are interspersed with examples.

Example one: Code implementation tweened Animation

① Create a new layout file: Four buttons plus one ImageView

<span style= "FONT-SIZE:14PX;" ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_        Parent "android:layout_height=" match_parent "> <linearlayout android:layout_width=" match_parent " android:layout_height= "match_parent" android:orientation= "vertical" > <imageview android:i D= "@+id/imageview" android:layout_width= "50DP" android:layout_height= "50DP" android:layou t_margintop= "100DP" android:src= "@drawable/arrow"/> </LinearLayout> <button android:i D= "@+id/button_alpha" android:layout_width= "match_parent" android:layout_height= "Wrap_content" Androi         D:layout_above= "@+id/button_rotate" android:text= "Alpha"/> <button android:id= "@+id/button_rotate" Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:layout_above= "@+id/ BUtton_scale "android:text=" rotate "/> <button android:id=" @+id/button_scale "Android:layout_        Width= "Match_parent" android:layout_height= "wrap_content" android:layout_above= "@+id/button_translate" android:text= "scale"/> <button android:id= "@+id/button_translate" android:layout_width= "Match_pa Rent "android:layout_height=" wrap_content "android:layout_alignparentbottom=" true "android:text=" Tran Slate "/></relativelayout></span>
②activity

<span style= "FONT-SIZE:14PX;" >package Com.example.f_animation_tween;import Android.os.bundle;import Android.support.v7.app.actionbaractivity;import Android.view.view;import Android.view.View.OnClickListener; Import Android.view.animation.alphaanimation;import Android.view.animation.animation;import Android.view.animation.animation.animationlistener;import Android.view.animation.animationset;import Android.view.animation.rotateanimation;import Android.view.animation.scaleanimation;import Android.view.animation.translateanimation;import Android.widget.button;import Android.widget.ImageView;public Class Mainactivity extends Actionbaractivity {private Button Button_alpha, Button_rotate, Button_scale, Button_trans; Private ImageView ImageView; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); button_alpha = (Button) Findviewbyid (R.id.button_alpha ); button_rotate = (Button) Findviewbyid (r.id.button_rotate); button_scale = (Button) Findviewbyid (r.id.button_scale); Button_trans = (Button) Findviewbyid (r.id.button_translate) ; ImageView = (ImageView) Findviewbyid (R.id.imageview); Button_alpha.setonclicklistener (new Buttonalphalisterner ()); Button_rotate.setonclicklistener (New Buttonrotatelisterner ()); Button_scale.setonclicklistener (new Buttonscalelisterner ()); Button_trans.setonclicklistener (new Buttontranslisterner ());} Private class Buttonalphalisterner implements Onclicklistener {@Overridepublic void OnClick (View arg0) {//TODO Auto-gene Rated method stub//creates a new alphaanimation, the constructor parameter represents a fade from 1 to 0 alphaanimation alphaanimation = new Alphaanimation (1, 0);// Fade duration is 2 seconds alphaanimation.setduration (2000);//Repeat two times, add once is three times alphaanimation.setrepeatcount (2);// Set a listener Alphaanimation.setanimationlistener (new Animationlistener () {@Overridepublic void Onanimationstart ( Animation arg0) {//TODO auto-generated method StubSystem.out.println ("--->start");} @Overridepublic void Onanimationrepeat (Animation arg0) {//TODO auto-gEnerated method StubSystem.out.println ("--->repeat");} @Overridepublic void Onanimationend (Animation arg0) {//TODO auto-generated method StubSystem.out.println ("--->end" );}});/ /Let the picture produce a fade animation imageview.startanimation (alphaanimation);//You can also have other controls animate button_alpha.startanimation (Alphaanimation);}} Private class Buttonrotatelisterner implements Onclicklistener {@Overridepublic void OnClick (View arg0) {//TODO Auto-gen Erated method stub//rotation animation, the center of the surrounding point is drawn by the constructor://The 34th parameter represents the x-axis relative to its own maximum, five or six parameter Y axis the same, equivalent to the image of the//lower right corner of the point, animation.relative_to_ Parent is relative to the parent control rotateanimation rotateanimation = new Rotateanimation (0, 360,animation.relative_to_self, 1, animation.relative_to_self,1); rotateanimation.setduration (+); imageview.startanimation (rotateAnimation);}} Private class Buttonscalelisterner implements Onclicklistener {@Overridepublic void OnClick (View arg0) {//TODO Auto-gene Rated method stub//transverse longitudinal is reduced to the original 0.1, around the point and above the same scaleanimation scaleanimation = new Scaleanimation (1, 0.1f, 1,0.1f, Animation.relative_to_self, 1,Animation.relative_to_self, 1); Scaleanimation.setduration (2000);//Let it freeze at the last Scaleanimation.setfillafter (true); Imageview.startanimation (scaleanimation);}} Private class Buttontranslisterner implements Onclicklistener {@Overridepublic void OnClick (View arg0) {//TODO Auto-gene Rated method Stubalphaanimation alphaanimation = new Alphaanimation (1, 0); Translateanimation translateanimation = new Translateanimation (animation.relative_to_parent, 0,Animation.RELATIVE_ To_parent, 1,animation.relative_to_self, 0, animation.relative_to_self,0); Animationset animationset = new Animationset (true);//You can add multiple animation effects through a single animationset, He is a subclass of animation animationset.addanimation (translateanimation); animationset.addanimation (alphaanimation); Animationset.setduration (+); imageview.startanimation (Animationset);}} </span>

Example two: Implementing a gradient animation in XML

① Create a new Anim folder under the Res folder, add a Alpha.xml file

<span style= "FONT-SIZE:14PX;" ><?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android "    android:interpolator=" @android: Anim/linear_interpolator ">    <alpha        android:duration=" 2000 "        android:fromalpha= "1.0"        android:toalpha= "0.0"/></set></span>
②activity Main code

<span style= "FONT-SIZE:14PX;" >private class Buttonalphalisterner implements Onclicklistener {@Overridepublic void OnClick (View arg0) {//TODO auto- Generated method Stubanimation alphaanimation = Animationutils.loadanimation (Mainactivity.this, R.anim.alpha); Imageview.startanimation (alphaanimation);}} </span>
for batch modification, the method of XML is faster, of course, you can add a few animation effects in the XML file, the specific parameters need to surf the Internet, here provides a framework.


Example three: frame-wise Animation

① a few pictures in the Drawable folder and create a new Anim.xml file

<span style= "FONT-SIZE:14PX;" ><?xml version= "1.0" encoding= "Utf-8"? ><animation-list xmlns:android= "http://schemas.android.com/apk/ Res/android "    android:oneshot=" false ">    <item        android:drawable=" @drawable/up "        android: Duration= ""/>    <item        android:drawable= "@drawable/right    " android:duration= " <item        android:drawable= "@drawable/down"        android:duration= "/>    <item        android: drawable= "@drawable/left"        android:duration= "/>a</animation-list></span>
②activity Main code

<span style= "FONT-SIZE:14PX;" >private class Buttonlistener implements Onclicklistener {@Overridepublic void OnClick (View arg0) {//TODO Auto-genera Ted Method Stubimageview.setbackgroundresource (R.drawable.anim); Animationdrawable animationdrawable = (animationdrawable) imageview.getbackground ();                 The Animationdrawable class is used to implement a frame-wise animation Isstart =!isstart;if (Isstart) Animationdrawable.start (); elseanimationdrawable.stop ();}} </span>

Example four: The use of Layoutanimationcontroller

A layout animation controller is used to animated a layout ' s, or a view group ' s, children. Each child uses the same animation and for every one of the them, the animation starts at a different time.

The ListView is also inherited ViewGroup, so take it as an example to write a demo.

① Layout file

<span style= "FONT-SIZE:14PX;" ><?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/ Res/android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >    <listview        android:id= "@+id/listview" android:layout_width= "Match_        Parent "        android:layout_height=" wrap_content ">    </ListView>    <button        android:id=" @+ Id/button "        android:layout_width=" match_parent "        android:layout_height=" Wrap_content "        android: text= "Test"/></linearlayout></span>

Create a new Anim folder under the ②res folder, create a new anim.xml and anim_layout.xml two files

<span style= "FONT-SIZE:14PX;" ><?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android ">    <alpha        android:duration=" android:fromalpha= "        0"        android:toalpha= "1"/></set ></span>
<span style= "FONT-SIZE:14PX;" ><?xml version= "1.0" encoding= "Utf-8"? ><layoutanimation xmlns:android= "http://schemas.android.com/apk /res/android "    android:animation=" @anim/anim "    android:animationorder=" random "    android:delay=" 200% "/ ></span>
the XML above sets the animation effect, execution order, and interval time

③activity

Package Com.example.f_animation_layoutcon;import Java.util.arraylist;import Java.util.list;import Android.os.bundle;import Android.support.v7.app.actionbaractivity;import Android.view.menu;import Android.view.menuitem;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.animation.animation;import Android.view.animation.animationutils;import Android.view.animation.layoutanimationcontroller;import Android.widget.adapter;import Android.widget.arrayadapter;import Android.widget.button;import Android.widget.listview;public class MainActivity Extends Actionbaractivity {private Button button;private ListView listview;private arrayadapter<string> adapter; Private list<string> List; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); button = (button) Findviewbyid (R.id.button); ListView = ( ListView) Findviewbyid (r.id.listview); list = new arraylist<string> (); List.add("Beijing"), List.add ("Shanghai"), List.add ("Guangzhou"), adapter = new Arrayadapter<string> (mainactivity.this,android. R.layout.simple_list_item_1, list); Button.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick ( View arg0) {//TODO auto-generated method Stubanimation animation = Animationutils.loadanimation (Mainactivity.this, R.anim.anim); Layoutanimationcontroller lac = new Layoutanimationcontroller (animation); Lac.setorder ( Layoutanimationcontroller.order_random); Lac.setdelay (1.0f); Listview.setlayoutanimation (LAC); Listview.setadapter (adapter);//You can also specify layoutanimation//in the ListView to add Properties Android:layoutanimation= "@anim /anim_layout "}});}}

Perform the effect, click button, the ListView item is displayed one by one instead of the full display.


Summary: 1, tween animation two ways to implement, XML is more useful for batch modification, event listener. 2, frame-by-frame animation. 3, Layoutanimationcontroller also have two ways to achieve.















Android:animation Animation

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.