隐藏"LinearLayout"(View.GONE)和显示LinearLayout(View.VISIBLE)时添加动画

Add animation when hide "LinearLayout" (View.GONE) and show LinearLayout (View.VISIBLE)

对不起我的英语...我会尽力解释我想做什么。 我有一个项目。可以在link:

下载

如截图所示:

按钮 "Hide/Show B layout" 隐藏和显示绿色容器 - "B layout"。我想在显示容器 "B layout" 时自上而下添加动画。以及隐藏容器时自下而上的动画。还有,我想要蓝色容器"C",和容器"B"一起慢慢掉下来。并顺利上升,连同集装箱"B"。请帮我做一下。

下面复制我的代码:

MainActivity

public class MainActivity extends Activity {
View Layout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Layout = findViewById(R.id.bLayout);

    final  View  button2  =  findViewById (R.id.button); 
    button2.setOnClickListener(new  View.OnClickListener(){

      @Override 
      public void onClick (View v){ 

          if ((Layout.getVisibility() == View.VISIBLE))
          {  

              Layout.setVisibility(View.GONE);
          }
          else
          {

            // Animation animFadeIn = AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_in_left);
            // Layout.setAnimation(animFadeIn);
             Layout.setVisibility(View.VISIBLE);

          }
      } 
    });

}

}

我认为您可以通过使用 AlphaAnimation class. Here 是一个类似的问题和答案来轻松做到这一点。

所以我认为您正在寻找的是如何将翻译动画连接到 show/hide 视图,而不是立即设置可见性。

看看Show and hide a View with a slide up/down animation。不仅仅是设置可见性,首先你 运行 转换动画,然后添加挂钩以在完成后显示或隐藏视图。

如果您发现底部视图没有移动,请跳转到您的 Android 开发人员选项并启用 "Show layout bounds" 然后您可以看到视图的边框(动画不会移动实际视图界限)。要使底部视图也具有动画效果,还需要一个平移动画。

我找到了解决问题的方法。完整课程和源代码可以在这里下载:click here

或使用以下代码:

activity_main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/header"
    android:layout_width="match_parent"
    android:layout_height="64dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="16dp"
    android:background="#FCF"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/color"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/rectangle"
        android:fontFamily="sans-serif-light"
        android:gravity="center_vertical"
        android:text=""
        android:textAlignment="center"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/clickme"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="4dp"
        android:fontFamily="sans-serif-light"
        android:gravity="center_vertical"
        android:text="click_here"
        android:textStyle="bold" />
</LinearLayout>

<LinearLayout
    android:id="@+id/expandable"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:background="#FFF"
    android:orientation="vertical"
    android:paddingLeft="4dp" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text2" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Слуга: text3" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text4" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text5" />
</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {

    LinearLayout mLinearLayout;
    LinearLayout mLinearLayoutHeader;
    ValueAnimator mAnimator;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setTitle("title");

        mLinearLayout = (LinearLayout) findViewById(R.id.expandable);
        // mLinearLayout.setVisibility(View.GONE);
        mLinearLayoutHeader = (LinearLayout) findViewById(R.id.header);

        // Add onPreDrawListener
        mLinearLayout.getViewTreeObserver().addOnPreDrawListener(
                new ViewTreeObserver.OnPreDrawListener() {

                    @Override
                    public boolean onPreDraw() {
                        mLinearLayout.getViewTreeObserver()
                                .removeOnPreDrawListener(this);
                        mLinearLayout.setVisibility(View.GONE);

                        final int widthSpec =     View.MeasureSpec.makeMeasureSpec(
                                0, View.MeasureSpec.UNSPECIFIED);
                        final int heightSpec = View.MeasureSpec
                                .makeMeasureSpec(0,
                                        View.MeasureSpec.UNSPECIFIED);
                        mLinearLayout.measure(widthSpec, heightSpec);

                        mAnimator = slideAnimator(0,
                                mLinearLayout.getMeasuredHeight());
                        return true;
                    }
                });

        mLinearLayoutHeader.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mLinearLayout.getVisibility() == View.GONE) {
                    expand();
                } else {
                    collapse();
                }
            }
        });
    }

    private void expand() {
        // set Visible
        mLinearLayout.setVisibility(View.VISIBLE);

        mAnimator.start();
    }

    private void collapse() {
        int finalHeight = mLinearLayout.getHeight();

        ValueAnimator mAnimator = slideAnimator(finalHeight, 0);

        mAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationEnd(Animator animator) {
                // Height=0, but it set visibility to GONE
                mLinearLayout.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationStart(Animator animator) {
            }

            @Override
            public void onAnimationCancel(Animator animator) {
            }

            @Override
            public void onAnimationRepeat(Animator animator) {
            }
        });
        mAnimator.start();
    }

    private ValueAnimator slideAnimator(int start, int end) {

        ValueAnimator animator = ValueAnimator.ofInt(start, end);

        animator.addUpdateListener(new     ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                // Update Height
                int value = (Integer) valueAnimator.getAnimatedValue();

                ViewGroup.LayoutParams layoutParams = mLinearLayout
                        .getLayoutParams();
                layoutParams.height = value;
                mLinearLayout.setLayoutParams(layoutParams);
            }
        });
        return animator;
    }
}

我遇到了类似的问题,这是我能找到的最简单的解决方案:

setVisibility(View.GONE) on the view you wish to hide.

并确保在 xml 布局中,父级具有以下属性:

android:animateLayoutChanges="true"