您的位置:首页 > 移动开发 > Android开发

LayoutAnimationController

2015-11-25 09:55 519 查看
安卓中除了可以为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() {

@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隐藏的话屏幕上依旧会显示控件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息