android開發之Animations的使用(四)

來源:互聯網
上載者:User

android開發之Animations的使用(四)
android開發之Animations的使用(四)本博文主要講述的是,animation在layout中的使用。本文是用ListView控制項為例子實現在layout中的使用有兩種方法,第一是直接使用xml檔案中的layoutAnimation標籤第二是使用代碼實現,使用layoutAnimationController對象完成,詳細代碼如下: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;

/*
*本類主要實現的是Animation的動畫效果在layout控制項中的使用
*本例將anim_list動畫效果應用於listview 控制項中
*/
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

//使用布局檔案完成對Layout控制項的動畫效果的設定
//查看layout_anim_list.xml檔案

//listView.setAdapter(buildListAdapter());

//使用xml檔案實現,需要在相應的layout控制項中添加如下語句:

android:layoutAnimation="@anim/layout_anim_list"






//使用代碼實現layout控制項的動畫效果設定
listView.setAdapter(buildListAdapter());
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.anim_list);
//建立LayoutAnimationController對象,且設定延時為0.5s
LayoutAnimationController lac = new LayoutAnimationController(animation, 0.5f);
//設定顯示的順序是normal
lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
listView.setLayoutAnimation(lac);

}

}


// 建立一個listAdapter對象
private ListAdapter buildListAdapter(){
ArrayList> list = new ArrayList>();
HashMap map1 = new HashMap();
HashMap map2 = new HashMap();
HashMap map3 = new HashMap();

map1.put("user", "張三");
map1.put("sex", "男");
map2.put("user", "李四");
map2.put("sex", "女");
map3.put("user", "王五");
map3.put("sex", "男");

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;
}


}


動畫效果布局檔案anim_list.xml:


android:interpolator="@android:anim/accelerate_interpolator" >

android:duration="2000"
android:fromAlpha="0.0"
android:toAlpha="1.0" />


layout控制項實現動畫效果的布局檔案layout_anim_list.xml:


xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="0.5"
android:animationOrder="normal"
android:animation="@anim/anim_list">



main.xml:

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" >


android:id="@+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

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"
/>


android:id="@+id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/myList"
android:text="測試" />


listview條目的布局檔案item.xml:


android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
android:id="@+id/user"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

android:id="@+id/sex"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.