【发布时间】:2012-02-12 12:53:17
【问题描述】:
我正在尝试制作正确的向下滑动动画。向下滑动的视图应将其下方的所有视图以一种平滑的方式向下推,当它向上滑动时,所有视图应以一种平滑的方式跟随。
我的尝试:
在代码中:
LinearLayout lin = (LinearLayout)findViewById(R.id.user_list_container);
setLayoutAnimSlidedownfromtop(lin, this);
lin.addView(getLayoutInflater().inflate(R.layout.user_panel,null),0);
还有:
public static void setLayoutAnimSlidedownfromtop(ViewGroup panel, Context ctx) {
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(100);
set.addAnimation(animation);
animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f
);
animation.setDuration(500);
set.addAnimation(animation);
LayoutAnimationController controller =
new LayoutAnimationController(set, 0.25f);
panel.setLayoutAnimation(controller);
}
我的user_panel.xml:
<?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="40dp"
android:orientation="vertical" >
<ImageView
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/icon" />
</LinearLayout>
主要 XML 的顶部:
<LinearLayout
android:id="@+id/user_list_container"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:id="@+id/container"
android:layout_below="@+id/user_list_container"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
上述方法的问题是,当我首先启动动画时,会创建视图的空白空间,然后视图会向下滑动。我希望它可以慢慢地向下推所有其他视图,而不是一气呵成。
【问题讨论】:
-
我认为您需要查看
LayoutTransition类。它提供了动画向/从布局添加/删除视图的机制。