使用向上/向下滑动动画显示和隐藏视图

使用向上/向下滑动动画显示和隐藏视图

我有一个LinearLayout我希望显示或隐藏的内容Animation,只要我改变其可见性,就会向上或向下推动布局。

我在那里看到了一些样品,但它们都不适合我的需要。

我为动画创建了两个xml文件,但是当我更改a的可见性时,我不知道如何启动它们LinearLayout


ABOUTYOU
浏览 845回答 3
3回答

慕码人8056858

使用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);         }     });

慕村9548890

最简单的解决方案:设置android:animateLayoutChanges="true"容器上的视图。将其置于某些上下文中:如果您具有如下所示的布局,则将自动为此容器中的视图的所有可见性更改进行动画处理。<LinearLayout&nbsp;android:id="@+id/container" &nbsp;&nbsp;&nbsp;&nbsp;android:layout_width="match_parent" &nbsp;&nbsp;&nbsp;&nbsp;android:layout_height="match_parent" &nbsp;&nbsp;&nbsp;&nbsp;android:animateLayoutChanges="true" &nbsp;&nbsp;&nbsp;&nbsp;> &nbsp;&nbsp;&nbsp;&nbsp;<Views_which_change_visibility></LinearLayout>您可以在Animating Layout Changes - Android Developer上找到有关此内容的更多详细信息
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android