Layout animations, as the name implies, are typically used to display the view on a layout, adding animations to the display of the view groups.
Usually we use the Layoutanimationcontroller object to add an animation to the view by first creating a Layoutanimationcontroller object and then loading the object with the corresponding view.
Let's take a look at the code (what I'm showing here is adding animations to the ListView)
Code under the Anim file
XML code (This is mostly used to animate every view of a ListView)
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Setxmlns:android= "Http://schemas.android.com/apk/res/android">3 < Scale4 android:duration= "+"5 Android:fromxscale= "0.0"6 Android:fromyscale= "0.0"7 Android:toyscale= "1.0"8 Android:toxscale= "1.0"9 Android:pivotx= "50%"Ten Android:pivoty= "50%" One /> A - </Set>
Layout file Code
XML code for 1.Mainactivity
1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "vertical"6 Tools:context= "Com.example.Tween_Animation.Alpha_MainActivity" >7 8 <Button9 Android:id= "@+id/button_scale"Ten Android:layout_width= "Fill_parent" One Android:layout_height= "Wrap_content" A Android:text= "@string/button_stringscaleanimation" /> - - <LinearLayout the android:gravity= "Center" - Android:layout_width= "Fill_parent" - Android:layout_height= "Fill_parent" - android:orientation= "vertical" > + - <ImageView + Android:id= "@+id/imageview_scale" A Android:layout_width= "Wrap_content" at Android:layout_height= "Wrap_content" - android:src= "@drawable/ic_launcher" /> - </LinearLayout> - - </LinearLayout>
XML code for 2.mainactivity2
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "vertical" >6 <ListView7 Android:id= "@+id/listview"8 Android:layout_width= "Fill_parent"9 Android:layout_height= "Wrap_content"Ten /> One A </LinearLayout>
Activity Code
1.MainActivity Code
1 Packagecom.example.Layout_Animation;2 3 ImportCOM.EXAMPLE.ANDROIDANIMATION.R;4 5 Importandroid.app.Activity;6 Importandroid.content.Intent;7 ImportAndroid.os.Bundle;8 ImportAndroid.view.View;9 ImportAndroid.view.View.OnClickListener;Ten ImportAndroid.widget.Button; One A Public classMainactivityextendsActivityImplementsonclicklistener{ - PrivateButton Button =NULL; - protected voidonCreate (Bundle savedinstancestate) { the Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); -button =(Button) Findviewbyid (R.id.button_scale); -Button.setonclicklistener ( This); + } - Public voidOnClick (View v) { +Intent Intent =NewIntent (mainactivity. This, MainActivity2.class); A startactivity (intent); at } -}
2.mainactivity2 's Code
1 Packagecom.example.Layout_Animation;2 3 ImportCOM.EXAMPLE.ANDROIDANIMATION.R;4 5 Importandroid.app.Activity;6 ImportAndroid.os.Bundle;7 Importandroid.view.animation.AnimationUtils;8 ImportAndroid.view.animation.LayoutAnimationController;9 ImportAndroid.widget.ArrayAdapter;Ten ImportAndroid.widget.ListView; One A Public classMainActivity2extendsactivity{ - PrivateListView ListView =NULL; - PrivateArrayadapter<string> Arrayadapter =NULL; the protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.main2); -ListView =(ListView) Findviewbyid (R.id.listview); +String string[] =NewString[20]; -Arrayadapter =NewArrayadapter<string> ( This, Android. R.layout.simple_expandable_list_item_1, Get_array (String)); + Listview.setadapter (arrayadapter); A //to create an Layoutanimationcontroller object atLayoutanimationcontroller Lac =NewLayoutanimationcontroller (Animationutils.loadanimation ( This, R.anim.scale)); - //sets the order in which each child control animates (the animation display time of each child control is calculated in the middle) - /* - * As can be seen from the Android API, there are 3 parameters to choose from - * order_normal--Sequential display - * order_random--Random display in * order_reverse--Flashback display - */ to Lac.setorder (layoutanimationcontroller.order_random); + Listview.setlayoutanimation (LAC); - } the Privatestring [] Get_array (String string[]) * { $ for(inti = 0; I < 20; i++)Panax Notoginseng { -String[i] = "Millet" + (i + 1); the } + returnstring; A } the}
Animation layout animations in Android