android中的动画之布局动画

黄沙百战穿金甲,不破楼兰终不还。这篇文章主要讲述android中的动画之布局动画相关的知识,希望能为你提供帮助。
布局动画,顾名思义,通常用来布局上的显示view,为view groups的显示添加动画。
通常我们使用LayoutAnimationController的对象来为view添加一个动画,具体的操作是:先创建一个LayoutAnimationController的对象,然后用相应的view来加载该对象。
接下来我们来看看代码(我在这里展示的是给listview添加动画)
anim文件下的代码
xml代码(这个最主要是用来给listview的每一个view添加动画)

1 < ?xml version="1.0" encoding="utf-8"?> 2 < set xmlns:android="http://schemas.android.com/apk/res/android"> 3< scale 4android:duration="300" 5android:fromXScale="0.0" 6android:fromYScale="0.0" 7android:toYScale="1.0" 8android:toXScale="1.0" 9android:pivotX="50%" 10android:pivotY="50%" 11/> 12 13 < /set>

布局文件代码
1.Mainactivity的xml代码
1< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2xmlns:tools="http://schemas.android.com/tools" 3android:layout_width="match_parent" 4android:layout_height="match_parent" 5android:orientation="vertical" 6tools:context="com.example.Tween_Animation.Alpha_MainActivity" > 7 8< Button 9android:id="@+id/button_scale" 10android:layout_width="fill_parent" 11android:layout_height="wrap_content" 12android:text="@string/button_stringScaleAnimation" /> 13 14< LinearLayout 15android:gravity="center" 16android:layout_width="fill_parent" 17android:layout_height="fill_parent" 18android:orientation="vertical" > 19 20< ImageView 21android:id="@+id/imageview_scale" 22android:layout_width="wrap_content" 23android:layout_height="wrap_content" 24android:src="https://www.songbingjia.com/android/@drawable/ic_launcher" /> 25< /LinearLayout> 26 27 < /LinearLayout>

2.MainActivity2的xml代码
1 < ?xml version="1.0" encoding="utf-8"?> 2 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3android:layout_width="match_parent" 4android:layout_height="match_parent" 5android:orientation="vertical" > 6< ListView 7android:id="@+id/listview" 8android:layout_width="fill_parent" 9android:layout_height="wrap_content" 10/> 11 12 < /LinearLayout>

Activity代码
1.MainActivity代码
1 package com.example.Layout_Animation; 2 3 import com.example.androidanimation.R; 4 5 import android.app.Activity; 6 import android.content.Intent; 7 import android.os.Bundle; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.widget.Button; 11 12 public class MainActivity extends Activity implements OnClickListener{ 13private Button button = null; 14protected void onCreate(Bundle savedInstanceState) { 15super.onCreate(savedInstanceState); 16setContentView(R.layout.activity_main); 17button = (Button) findViewById(R.id.button_scale); 18button.setOnClickListener(this); 19} 20public void onClick(View v) { 21Intent intent = new Intent(MainActivity.this,MainActivity2.class); 22startActivity(intent); 23} 24 }

【android中的动画之布局动画】2.MainActivity2的代码
1 package com.example.Layout_Animation; 2 3 import com.example.androidanimation.R; 4 5 import android.app.Activity; 6 import android.os.Bundle; 7 import android.view.animation.AnimationUtils; 8 import android.view.animation.LayoutAnimationController; 9 import android.widget.ArrayAdapter; 10 import android.widget.ListView; 11 12 public class MainActivity2 extends Activity{ 13private ListView listview = null; 14private ArrayAdapter< String> arrayadapter = null; 15protected void onCreate(Bundle savedInstanceState) { 16super.onCreate(savedInstanceState); 17setContentView(R.layout.main2); 18listview = (ListView) findViewById(R.id.listview); 19String string[] = new String[20]; 20arrayadapter = new ArrayAdapter< String> (this, android.R.layout.simple_expandable_list_item_1, get_array(string)); 21listview.setAdapter(arrayadapter); 22//创建一个LayoutAnimationController的对象 23LayoutAnimationController lac = new LayoutAnimationController(AnimationUtils.loadAnimation(this, R.anim.scale)); 24//设置每个子控件动画播放的顺序(中间计算了每个子控件的动画显示时间的) 25/* 26* 从安卓api中可以看出,有3个参数供选择 27* ORDER_NORMAL--顺序显示 28* ORDER_RANDOM--随机显示 29* ORDER_REVERSE--倒叙显示 30*/ 31lac.setOrder(LayoutAnimationController.ORDER_RANDOM); 32listview.setLayoutAnimation(lac); 33} 34private String [] get_array(String string[]) 35{ 36for(int i = 0; i < 20; i++) 37{ 38string[i] = "小米" + (i + 1); 39} 40return string; 41} 42 }

 

    推荐阅读