LayoutAnimationController
2015-11-25 09:55
573 查看
安卓中除了可以为View添加View动画外还可以为ViewGroup的子元素添加出场动画.
例如给ListView中每个控件添加动画(只有第一次显示时才播放动画,滚动后不再播放动画)
布局中添加android:layoutAnimation="@anim/layout_anin
布局文件
layout_anim.xml
item_anim.xml
当然也可以直接使用java代码来实现动画
或者直接这样:
按照上面这种方式的话就不需要在布局文件中为ListView指定android:layoutAnimation="@anim/layout_anin了。
animationController.setDelay(0.5f) 设置下一个动画相对于上一个动画的延时为 0.5* (item_amin.xml中指定的时间)
再看一个例子:
还有一点需要注意,如果指定android:fillAfter="true" ,则动画结束后就一直停在那,不会自动恢复到一开始位置。
View动画只是对View的一个镜像进行移动,当View移动到新位置后,新位置不会响应事件,原位置一直可以响应事件。所以如果动画结束后设置View隐藏的话屏幕上依旧会显示控件
例如给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() { @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub TextView 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); }else textView=(TextView) convertView; textView.setText(""+position); return textView; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public int getCount() { // TODO Auto-generated method stub return 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() { @Override public 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隐藏的话屏幕上依旧会显示控件
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories