Android layoutanimation的应用

来源:互联网 发布:淘宝联盟怎么发链接 编辑:程序博客网 时间:2024/04/30 08:16

layoutanimation作用于viewgroup,为viewgroup指定一个动画,这样当他的子元素出场时就具有这种动画效果。让我们来看看特殊的listview,他的每个item都已特殊的动画出现。

<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"    android:animation="@anim/anim_item"    android:animationOrder="reverse"    android:delay="0.5" />
delay子元素开始动画事件延迟

animationOrder表示出场次序 有normal 正常次序,random随机入场,reverse 倒叙入场,animation 具体入场动画
为子元素指定入场动画

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

为viewgroup指定layoutanimation属性,

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <ListView        android:id="@+id/listview"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layoutAnimation="@anim/anim_layout" >    </ListView></LinearLayout>
除了在xml内指定layoutanimation之外,还可以在代码里实现:

listview = (ListView) findViewById(R.id.listview);Animation animation = AnimationUtils.loadAnimation(ListLayOut.this,R.anim.anim_item);LayoutAnimationController controller = new LayoutAnimationController(animation);controller.setDelay(0.5f);controller.setOrder(LayoutAnimationController.ORDER_NORMAL);listview.setLayoutAnimation(controller);
2、activity的切换效果

activity的切换是有固定的动画的,但是我们也可以自己来定义,主要由overridependingtransition(int enteranimation , int exitanimation),这个方法必须在startactivity之后或者在finish之后调用才有效,他的参数含义如下:

enteranimation activity被打开时的动画资源id

exitanimation  activity被暂停时的动画资源id

public static void startActivity(Activity activity) {Intent intent = new Intent(activity, ListLayOut.class);activity.startActivity(intent);activity.overridePendingTransition(R.anim.anim_item, R.anim.anim_item2);}
当然也可以给fragment添加出入动画,但是只能添加view动画,不能田间属性动画。所以兼容性比较低。

0 0
原创粉丝点击