添加动画时隐藏]的LinearLayout" (View.GONE)和show的LinearLayout(View.VISIBLE) [英] Add animation when hide "LinearLayout" (View.GONE) and show LinearLayout (View.VISIBLE)

查看:229
本文介绍了添加动画时隐藏]的LinearLayout" (View.GONE)和show的LinearLayout(View.VISIBLE)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我的英语...我会尽量解释什么,我想做的事情。
我有一个项目。它可以在网址下载:

Sorry for my English... I will try to explain what I want to do. I have a project. It can be downloaded at the link:

正如你所看到的截图:

  • http://pixs.ru/showimage/Screenshot_9509352_15647059.png

按钮隐藏/显示乙布局隐藏和显示绿色的集装​​箱 - B布局。我想添加动画自上而下当容器B布局的显示。而从下往上动画当容器被隐藏。另外,我想蓝色的集装箱C,逐渐与容器B一起下跌。和瑞星顺利,​​容器B在一起。请帮我做到这一点。

The button "Hide/Show B layout" hides and shows the green container - "B layout". I want add animation top down when the container "B layout" is showing. And the animation from the bottom up when the container is hidden. Also, I want the blue container "C", gradually fell together with the container "B". And rising smoothly, together with the container "B". Please help me to do it.

下面复制我的code:

Below duplicate my code:

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);

          }
      } 
    });

}

}

推荐答案

我找到了一个解决问题的办法。全部课程和源$ C ​​$ C可以在这里下载:点击这里

I found a solution to the problem. Full lesson and source code can be downloaded here: click here

或者使用code如下:

Or use the code below:

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;
    }
}

这篇关于添加动画时隐藏]的LinearLayout&QUOT; (View.GONE)和show的LinearLayout(View.VISIBLE)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆