使用向上/向下滑动动画显示和隐藏视图我有一个LinearLayout我希望显示或隐藏的内容Animation,只要我改变其可见性,就会向上或向下推动布局。我在那里看到了一些样品,但它们都不适合我的需要。我为动画创建了两个xml文件,但是当我更改a的可见性时,我不知道如何启动它们LinearLayout。
3 回答
![?](http://img1.sycdn.imooc.com/5458692c00014e9b02200220-100-100.jpg)
慕码人8056858
TA贡献1803条经验 获得超6个赞
使用Android 3.0(Honeycomb)中引入的新动画API,创建此类动画非常简单。
View
向下滑动一段距离:
view.animate().translationY(distance);
您可以稍后将View
其滑回原位,如下所示:
view.animate().translationY(0);
您还可以轻松组合多个动画。以下动画将View
向下滑动其高度并同时淡入其中:
// Prepare the View for the animationview.setVisibility(View.VISIBLE);view.setAlpha(0.0f);// Start the animationview.animate() .translationY(view.getHeight()) .alpha(1.0f) .setListener(null);
然后,您可以淡出View
后退并将其滑回原始位置。我们还设置了一个动画完成后AnimatorListener
我们可以设置View
背面的可见性GONE
:
view.animate() .translationY(0) .alpha(0.0f) .setListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); view.setVisibility(View.GONE); } });
![?](http://img1.sycdn.imooc.com/54584cfb0001308402200220-100-100.jpg)
慕村9548890
TA贡献1884条经验 获得超4个赞
最简单的解决方案:设置android:animateLayoutChanges="true"
容器上的视图。
将其置于某些上下文中:如果您具有如下所示的布局,则将自动为此容器中的视图的所有可见性更改进行动画处理。
<LinearLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:animateLayoutChanges="true" > <Views_which_change_visibility></LinearLayout>
您可以在Animating Layout Changes - Android Developer上找到有关此内容的更多详细信息
- 3 回答
- 0 关注
- 844 浏览
添加回答
举报
0/150
提交
取消