Use of Android development animations (iv)

Source: Internet
Author: User

Use of Android development animations (iv) This blog is mainly about the use of animation in layout. This article is using the ListView control as an example There are two ways to implement the use of layout the first is to use the layoutanimation tag directly in the XML fileThe second is to use the code implementation, using the Layoutanimationcontroller object to complete,The detailed code is as follows: Mainactivity.java:
Package Com.example.animationtest4; Import java.util.ArrayList;
Import Java.util.HashMap;
Import Android.os.Bundle;
Import android.app.Activity;
Import Android.view.Menu;
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.Button;
Import Android.widget.ListAdapter;
Import Android.widget.ListView;
Import Android.widget.SimpleAdapter;

/*
* The main implementation of this class is the use of animation animation effects in the layout control
* This example applies the Anim_list animation effect to the ListView control
*/
public class Mainactivity extends Activity {


Private Button Testbutton = null;
Private ListView ListView = null;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Testbutton = (Button) Findviewbyid (R.id.mybutton);
ListView = (ListView) Findviewbyid (R.id.mylist);

Testbutton.setonclicklistener (New Buttonlistener ());
}

Class Buttonlistener implements onclicklistener{


@Override
public void OnClick (View v) {
TODO auto-generated Method Stub

Use a layout file to complete the settings for animating the layout control
View Layout_anim_list.xml File

Listview.setadapter (Buildlistadapter ());

With the XML file implementation, you need to add the following statement to the appropriate layout control:

android:layoutanimation= "@anim/layout_anim_list"






Animating the layout control using code
Listview.setadapter (Buildlistadapter ());
Animation Animation = animationutils.loadanimation (Mainactivity.this, r.anim.anim_list);
Create Layoutanimationcontroller object with set delay of 0.5s
Layoutanimationcontroller lac = new Layoutanimationcontroller (animation, 0.5f);
The order in which the settings are displayed is normal
Lac.setorder (Layoutanimationcontroller.order_normal);
Listview.setlayoutanimation (LAC);

}

}


Create a ListAdapter object
Private ListAdapter Buildlistadapter () {
arraylisthashmap<string, string> map1 = new hashmap<string, string> ();
hashmap<string, string> map2 = new hashmap<string, string> ();
hashmap<string, string> map3 = new hashmap<string, string> ();

Map1.put ("User", "Zhang San");
Map1.put ("Sex", "male");
Map2.put ("User", "John Doe");
Map2.put ("Sex", "female");
Map3.put ("User", "Harry");
Map3.put ("Sex", "male");

List.add (MAP1);
List.add (MAP2);
List.add (MAP3);

Simpleadapter adapter = new Simpleadapter (mainactivity.this, List, R.layout.item, new string[]{"user", "Sex"}, new int[]{ R.id.user,r.id.sex});

return adapter;
}


@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}


}


Animation effects layout file Anim_list.xml:

<?xml version= "1.0" encoding= "Utf-8"?>
<set xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:interpolator= "@android: Anim/accelerate_interpolator" >

<alpha
Android:duration= "2000"
Android:fromalpha= "0.0"
Android:toalpha= "1.0"/>
</set>


Layout control for animating Layout_anim_list.xml:

<?xml version= "1.0" encoding= "Utf-8"?>
<layoutanimation
Xmlns:android= "Http://schemas.android.com/apk/res/android"
android:delay= "0.5"
Android:animationorder= "Normal"
android:animation= "@anim/anim_list" >

</layoutAnimation>


Main.xml:

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
Tools:context= ". Mainactivity ">


<textview
Android:id= "@+id/mytext"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/hello_world"/>

<listview
Android:id= "@+id/mylist"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_below= "@id/mytext"
android:layoutanimation= "@anim/layout_anim_list"
/>


<button
Android:id= "@+id/mybutton"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_below= "@id/mylist"
android:text= "Test"/>
</RelativeLayout>


The layout file for the ListView entry Item.xml:

<?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:orientation= "Horizontal"
    <TEXTVIEW&NBSP,
        android:id= "@+id/user"
        Android: Layout_width= "Fill_parent"
        android:layout_height= "wrap_content"       &NBSP
       />
    
    <textview 
        android:id= "@+id/sex"
        android:layout_width= "Fill_parent"
        android:layout_height= "wrap_content"         
    & nbsp  />

</linearlayout>



Use of Android development animations (iv)

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.