android Animations 動畫效果(四)

來源:互聯網
上載者:User

1.LayoutAnimationController的使用方法
什麼是LayoutAnimationController
1.LayoutAnimationController用於為一個layout裡面的控制項,或者是一個

ViewGroup裡面的控制項設定動畫效果
2.每一個控制項都有相同的動畫效果
3.這些控制項的動畫效果在不同的時間顯示出來
4.LayoutAnimationController可以在xml檔案中設定,也可以在代碼中設定


2.ListView與Animations結合使用
在代碼中使用LayoutAnimationController
1.建立一個Animation對象:
可以通過裝載xml檔案,或者直接使用Animation的建構函式建立Animation對象
2使用如下代碼建立LayoutAnimationController對象:
LayoutAnimationController lac = new LayoutAnimationController

(animation);
3.設定控制項顯示順序:
lac.serOrder(LayoutAnimationController.ORDER_NORMAL)
4.為ListView設定LayoutAnimationController屬性:
listView.setLayoutAnimation(lac);



MainActivity.java

package com.yx.animations04;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.yx.animations04.R;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends ListActivity {

private Button button=null;
private ListView listView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

button = (Button) findViewById(R.id.buttonId);
button.setOnClickListener(new buttonListener());

listView = getListView();
}

private ListAdapter buildListAdapter(){
List> list = new ArrayList>();
HashMap m1 = new HashMap();
m1.put("name", "張三");
m1.put("gender","女");

HashMap m2 = new HashMap();
m2.put("name", "李四");
m2.put("gender","女");

HashMap m3 = new HashMap();
m3.put("name", "王五");
m3.put("gender","男");

list.add(m1);
list.add(m2);
list.add(m3);

SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this, list,
R.layout.item, new String[]{"name","gender"}, new int[]{R.id.name,R.id.gender});
return simpleAdapter;
}

private class buttonListener implements OnClickListener{
@Override
public void onClick(View v) {
listView.setAdapter(buildListAdapter());

//在代碼中實現,這裡不需要list_anim_layout.xml檔案
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.list_anim);
LayoutAnimationController lac = new LayoutAnimationController(animation);
lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
listView.setLayoutAnimation(lac);
}
}
}


list_anim_layout.xml


android:delay="0.5"
android:animationOrder="normal"
android:animation="@anim/list_anim"
>


list_anim.xml


android:interpolator="@android:anim/accelerate_interpolator">
android:toAlpha="0.0"
android:duration="2000">

activity_main.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >



android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
>

android:id="@+id/buttonId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="測試動畫效果"
/>


item.xml


android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

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

android:id="@+id/gender"
android:layout_width="wrap_content"
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.