我的布局内有以下视图
<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);
这是动画文件的内容:
我如何制作此动画以推动页眉视图,而页脚视图却向上滑动?
希望我能说清楚。我想要的只是两个视图一起向上/向下移动。
如果我将动画应用于包装器视图,则它将从底部(而不是从当前位置)向上滑动。
如果您希望页眉与页脚动画一起推送,可以按以下步骤手动进行
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;
}
});
}
}
});
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句