为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

ABOUTYOU 2019-07-27 15:13:51
使用向上/向下滑动动画显示和隐藏视图我有一个LinearLayout我希望显示或隐藏的内容Animation,只要我改变其可见性,就会向上或向下推动布局。我在那里看到了一些样品,但它们都不适合我的需要。我为动画创建了两个xml文件,但是当我更改a的可见性时,我不知道如何启动它们LinearLayout。
查看完整描述

3 回答

?
慕码人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);
        }
    });


查看完整回答
反对 回复 2019-07-27
?
慕村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上找到有关此内容的更多详细信息


查看完整回答
反对 回复 2019-07-27
  • 3 回答
  • 0 关注
  • 844 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信