我的布局中有以下视图
<RelativeLayout
android:id="@+id/wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<LinearLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<!-- some child views here -->
</LinearLayout>
<LinearLayout
android:id="@+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/header"
android:visibility="gone">
<!-- some other child views here -->
</LinearLayout>
</RelativeLayout>
包装器RelativeLayout与其父级的底部对齐。并且不显示初始页脚视图(View.GONE)。当点击标题视图时,应该向用户显示页脚从屏幕底部向上滑动动画,当用户再次点击时,页脚应该向下滑动到底部。当我在页脚视图上启动向上滑动动画时,它不会与动画同时推动标题视图。如果我首先将页脚视图可见性首先设置为View.INVISIBLE然后开始动画,则标题视图会立即向上推(不与动画或页脚视图一起),页脚视图会附带动画。
以下是调用动画的代码
footer.setVisibility(View.INVISIBLE);
Animation animation = AnimationUtils.loadAnimation(activity, R.anim.slide_up_from_bottom);
animation.setDuration(animDuration);
footer.startAnimation(animation);
footer.postDelayed(new Runnable() {
@Override
public void run()
{
footer.setVisibility(View.VISIBLE);
}
}, animDuration);
这是动画文件内容:
当页脚视图向上滑动时,如何使此动画推送标题视图?
希望我清楚自己。我想要的是两个视图一起向上/向下移动。
如果我将动画应用于包装器视图,则它会从底部(而不是从当前位置)向上滑动。
答案 0 :(得分:4)
如果您希望标题与页脚动画一起推送,可以按照以下方式手动执行
public class MainActivity extends Activity {
private LinearLayout header,footer;
private RelativeLayout wrapper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
header = (LinearLayout) findViewById(R.id.header);
footer = (LinearLayout) findViewById(R.id.footer);
wrapper = (RelativeLayout) findViewById(R.id.wrapper);
header.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(footer.getVisibility() == View.VISIBLE){
int footerHeight = footer.getMeasuredHeight();
float initialWrapperY = wrapper.getY();
ObjectAnimator oa=ObjectAnimator.ofFloat(wrapper, "y", initialWrapperY,initialWrapperY+footerHeight);
oa.setDuration(400);
oa.start();
oa.addListener(new AnimatorListenerAdapter() {
public void onAnimationEnd(android.animation.Animator animation) {
footer.setVisibility(View.INVISIBLE);
};
});
}
else{
final float initialWrapperY = wrapper.getY();
footer.setVisibility(View.VISIBLE);
wrapper.setY(initialWrapperY);
ViewTreeObserver vto=wrapper.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
wrapper.getViewTreeObserver().removeOnPreDrawListener(this);
final int footerHeight = footer.getMeasuredHeight();
ObjectAnimator oa=ObjectAnimator.ofFloat(wrapper, "y", initialWrapperY,(initialWrapperY-footerHeight));
oa.setDuration(400);
oa.start();
return true;
}
});
}
}
});
}
}