安卓开发_浅谈Android动画(三)
一、LayoutAnimation布局动画
用于为一个layout里面的控件,或者是一个ViewGroup里面的控件设置动画效果
在res-anim文件下新建一个动画xml文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <set xmlns:android="http://schemas.android.com/apk/res/android" 3 android:interpolator="@android:anim/decelerate_interpolator" > 4 5 <scale 6 android:duration="1000" 7 android:fromXScale="0.1" 8 android:fromYScale="0.1" 9 android:pivotX="50%" 10 android:pivotY="50%" 11 android:toXScale="1.0" 12 android:toYScale="1.0" /> 13 <alpha 14 android:duration="1000" 15 android:fromAlpha="0" 16 android:toAlpha="1.0" /> 17 </set>
list为列表
//布局动画控制器
LayoutAnimationController lac = new LayoutAnimationController(AnimationUtils.loadAnimation(this,R.anim.zoom_in));
//动画方式
lac.setOrder(LayoutAnimationController.ORDER_RANDOM);
//加载布局动画
list.setLayoutAnimation(lac);
//开始动画
list.startLayoutAnimation();
//为列表视图中选中的项添加响应事件
效果图:
全部代码:
1 package other; 2 3 import com.example.allcode.R; 4 5 import android.app.Activity; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.view.animation.AnimationUtils; 9 import android.view.animation.LayoutAnimationController; 10 import android.widget.AdapterView; 11 import android.widget.AdapterView.OnItemClickListener; 12 import android.widget.ListView; 13 import android.widget.Toast; 14 15 public class Donghua_List extends Activity{ 16 private ListView list; 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 // TODO Auto-generated method stub 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.donghua_layout); 22 23 24 list = (ListView) findViewById(R.id.listView1); 25 //布局动画控制器 26 LayoutAnimationController lac = new LayoutAnimationController(AnimationUtils.loadAnimation(this,R.anim.zoom_in)); 27 //动画方式 28 lac.setOrder(LayoutAnimationController.ORDER_RANDOM); 29 //加载布局动画 30 list.setLayoutAnimation(lac); 31 //开始动画 32 list.startLayoutAnimation(); 33 //为列表视图中选中的项添加响应事件 34 list.setOnItemClickListener(new OnItemClickListener() { 35 36 @Override 37 public void onItemClick(AdapterView<?> parent, View arg1, int pos, 38 long id) { 39 // TODO Auto-generated method stub 40 String result = parent.getItemAtPosition(pos).toString(); //获取选择项的值 41 Toast.makeText(Donghua_List.this, "点击了 "+result, Toast.LENGTH_SHORT).show();//输出选中项消息 42 } 43 }); 44 } 45 46 47 48 }
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 7 <ListView 8 android:id="@+id/listView1" 9 android:entries="@array/list" 10 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" > 13 </ListView> 14 15 </LinearLayout>
动画汇总:
1 <?xml version="1.0" encoding="utf-8"?> 2 <set xmlns:android="http://schemas.android.com/apk/res/android" 3 android:interpolator="@android:anim/decelerate_interpolator" > 4 5 <scale 6 android:duration="1000" 7 android:fromXScale="0.1" 8 android:fromYScale="0.1" 9 android:pivotX="50%" 10 android:pivotY="50%" 11 android:toXScale="1.0" 12 android:toYScale="1.0" /> 13 <alpha 14 android:duration="1000" 15 android:fromAlpha="0" 16 android:toAlpha="1.0" /> 17 </set>
1 <?xml version="1.0" encoding="utf-8"?> 2 <set xmlns:android="http://schemas.android.com/apk/res/android"> 3 4 <translate android:fromXDelta="-100%p" 5 android:toXDelta="0" 6 android:duration="300"/> 7 <alpha android:fromAlpha="0.0" 8 android:toAlpha="1.0" 9 android:duration="300" /> 10 </set>
1 <?xml version="1.0" encoding="utf-8"?> 2 <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> 3 <translate android:fromYDelta="-100%" android:toXDelta="0" android:duration="300" /> 4 <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" /> 5 </set>
1 <?xml version="1.0" encoding="utf-8"?> 2 <set xmlns:android="http://schemas.android.com/apk/res/android" 3 android:interpolator="@android:anim/decelerate_interpolator"> 4 5 <translate android:fromYDelta="100%" android:toXDelta="0" android:duration="300" /> 6 <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" /> 7 </set>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。