android中的动画之布局动画
接下来我们来看看代码(我在这里展示的是给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 4 android:duration="300" 5 android:fromXScale="0.0" 6 android:fromYScale="0.0" 7 android:toYScale="1.0" 8 android:toXScale="1.0" 9 android:pivotX="50%" 10 android:pivotY="50%" 11 /> 12 13 </set>
布局文件代码
1.Mainactivity的xml代码
1 <LinearLayout xmlns: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 <Button 9 android:id="@+id/button_scale" 10 android:layout_width="fill_parent" 11 android:layout_height="wrap_content" 12 android:text="@string/button_stringScaleAnimation" /> 13 14 <LinearLayout 15 android:gravity="center" 16 android:layout_width="fill_parent" 17 android:layout_height="fill_parent" 18 android:orientation="vertical" > 19 20 <ImageView 21 android:id="@+id/imageview_scale" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:src="@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" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 <ListView 7 android:id="@+id/listview" 8 android:layout_width="fill_parent" 9 android: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{
13 private Button button = null;
14 protected void onCreate(Bundle savedInstanceState) {
15 super.onCreate(savedInstanceState);
16 setContentView(R.layout.activity_main);
17 button = (Button) findViewById(R.id.button_scale);
18 button.setOnClickListener(this);
19 }
20 public void onClick(View v) {
21 Intent intent = new Intent(MainActivity.this,MainActivity2.class);
22 startActivity(intent);
23 }
24 }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{
13 private ListView listview = null;
14 private ArrayAdapter<String> arrayadapter = null;
15 protected void onCreate(Bundle savedInstanceState) {
16 super.onCreate(savedInstanceState);
17 setContentView(R.layout.main2);
18 listview = (ListView) findViewById(R.id.listview);
19 String string[] = new String[20];
20 arrayadapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, get_array(string));
21 listview.setAdapter(arrayadapter);
22 //创建一个LayoutAnimationController的对象
23 LayoutAnimationController lac = new LayoutAnimationController(AnimationUtils.loadAnimation(this, R.anim.scale));
24 //设置每个子控件动画播放的顺序(中间计算了每个子控件的动画显示时间的)
25 /*
26 * 从安卓api中可以看出,有3个参数供选择
27 * ORDER_NORMAL--顺序显示
28 * ORDER_RANDOM--随机显示
29 * ORDER_REVERSE--倒叙显示
30 */
31 lac.setOrder(LayoutAnimationController.ORDER_RANDOM);
32 listview.setLayoutAnimation(lac);
33 }
34 private String [] get_array(String string[])
35 {
36 for(int i = 0; i < 20; i++)
37 {
38 string[i] = "小米" + (i + 1);
39 }
40 return string;
41 }
42 }转载自:https://www.cnblogs.com/Stay-Hungry-Stay-Foolish/p/5886904.html