我正在尝试制作一个正确的下拉动画。下滑的视图应该在一个平稳的运动中将其下方的所有视图向下推,并且再次上滑时,所有视图都应该在一个平稳的运动中跟随它一起移动。
我尝试过的方法:
在代码中:
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
类。它提供了在将视图添加/删除到/从布局中进行动画处理的机制。 - Aleks G