安卓中除了可以为View添加View动画外还可以为ViewGroup的子元素添加出场动画.


例如给ListView中每个控件添加动画(只有第一次显示时才播放动画,滚动后不再播放动画)


布局中添加android:layoutAnimation="@anim/layout_anin


布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    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="com.example.xtest.MainActivity"    android:background="#fff" >    <ListView         android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/listview"        android:layoutAnimation="@anim/layout_anim"></ListView></RelativeLayout>

layout_anim.xml

<?xml version="1.0" encoding="utf-8"?><layoutAnimation    xmlns:android="http://schemas.android.com/apk/res/android"    android:delay=".1"    android:animationOrder="normal"    android:animation="@anim/item_anim"    />


item_anim.xml

<?xml version="1.0" encoding="utf-8"?><set    xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="3000"       android:interpolator="@android:anim/linear_interpolator"    android:shareInterpolator="true"      >    <translate         android:fromXDelta="-500"        android:toXDelta="0"        />    <alpha         android:fromAlpha="0.5"        android:toAlpha="1"/></set>

setContentView(R.layout.activity_main3);ListView listView=(ListView) findViewById(R.id.listview);listView.setAdapter(new BaseAdapter() {@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubTextView textView;if (convertView==null) {textView=new TextView(getApplicationContext());textView.setTextColor(Color.BLACK);textView.setPadding(20, 20, 20, 20);textView.setTextSize(25);textView.setBackgroundColor(Color.DKGRAY);}elsetextView=(TextView) convertView;textView.setText(""+position);return textView;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn 0;}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn null;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn 100;}});
只需按照上面这样就可以有动画效果了


当然也可以直接使用java代码来实现动画

Animation animation=AnimationUtils.loadAnimation(getApplicationContext(), R.anim.item_anim);LayoutAnimationController animationController=new LayoutAnimationController(animation);animationController.setDelay(0.5f);animationController.setOrder(LayoutAnimationController.ORDER_NORMAL);listView.setLayoutAnimation(animationController);

或者直接这样:

LayoutAnimationController animationController=AnimationUtils.loadLayoutAnimation(getApplicationContext(), R.anim.layout_anim);animationController.setDelay(0.5f);animationController.setOrder(LayoutAnimationController.ORDER_NORMAL);listView.setLayoutAnimation(animationController);



按照上面这种方式的话就不需要在布局文件中为ListView指定android:layoutAnimation="@anim/layout_anin了。

animationController.setDelay(0.5f) 设置下一个动画相对于上一个动画的延时为 0.5* (item_amin.xml中指定的时间)


再看一个例子:

final LinearLayout linearLayout=new LinearLayout(getApplicationContext());linearLayout.setOrientation(LinearLayout.VERTICAL);setContentView(linearLayout);final LayoutAnimationController animationController=AnimationUtils.loadLayoutAnimation(getApplicationContext(), R.anim.layout_anim);animationController.setDelay(0.5f);animationController.setOrder(LayoutAnimationController.ORDER_NORMAL);linearLayout.setLayoutAnimation(animationController);//下面这一个Button会执行从左向右移动的动画Button but=new Button(getApplicationContext());but.setText(""+1);linearLayout.addView(but);but.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Button but=new Button(getApplicationContext());but.setText(""+System.currentTimeMillis());linearLayout.addView(but);//把新创建的Button加入到linearLayout中//如果不调用下面这句话新添加控件时不会执行任何动画linearLayout.setLayoutAnimation(animationController);}});



还有一点需要注意,如果指定android:fillAfter="true" ,则动画结束后就一直停在那,不会自动恢复到一开始位置。

View动画只是对View的一个镜像进行移动,当View移动到新位置后,新位置不会响应事件,原位置一直可以响应事件。所以如果动画结束后设置View隐藏的话屏幕上依旧会显示控件




更多相关文章

  1. Android实现简单的城市列表功能
  2. 适合Material Dsign的新抽屉---Navigation View介绍
  3. 2017-06-09-LayoutTransition 容器布局动画
  4. Android(安卓)GreenDao最的基本配置与初始化
  5. Android(安卓)底部菜单栏(RadioGroup+Fragment)美化
  6. Android——RecyclerView——Recycler类全部源码翻译及注释
  7. Android(安卓)集成百度地图实现设备定位
  8. android studio ,gradle 导入项目 常见错误 错误提示:Error:(2, 0)
  9. PreferenceActivity 参数设置UI的使用

随机推荐

  1. android 文件系统分析
  2. Android(安卓)Wifi模块学习
  3. Google Android(安卓)SDK 2.1正式发布
  4. 系出名门Android(3) - 对话框(Dialog)和
  5. Android之BroadcastReceiver
  6. Android中SQLite应用详解
  7. 浅谈Android(安卓)动画,带你进入动画的世
  8. Android(安卓)默认Tab标签大小及间距修改
  9. 在Android上实现WLAN的一点理解
  10. Android调用WebService之服务端实现(一)