LayoutTransition:展开视图旁边的Animate View

时间:2012-10-26 08:02:30

标签: android android-animation

我已经将我的问题重新创建为一个非常简单的表示。我有3个TextViews。其中2个是单独LinearLayout,第三个与LinearLayout处于同一级别。 我正在切换test1test2的可见度,我希望看到它们消失(这有效)。此外,我希望test3能够滑入他的新位置(取代test1test2)。我无法做到这一点。 test3只是抓住它的新观点。

我怎么能做到这一点?

我的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:animateLayoutChanges="true"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click" />

    <LinearLayout android:animateLayoutChanges="true"
        android:id="@+id/child1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/test1"
            android:layout_width="match_parent" android:visibility="gone"
            android:layout_height="wrap_content"
            android:text="TEST1" />

        <TextView
            android:id="@+id/test2"
            android:layout_width="match_parent" android:visibility="gone"
            android:layout_height="wrap_content"
            android:text="TEST2" />
    </LinearLayout>

    <TextView
        android:id="@+id/test3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TEST3" />

</LinearLayout>

在我的活动中:

public class LayoutAnimations extends Activity {
    private boolean toggle = true;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_animations);

        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (toggle) {
                    findViewById(R.id.test1).setVisibility(View.VISIBLE);
                    findViewById(R.id.test2).setVisibility(View.VISIBLE);
                } else {
                    findViewById(R.id.test1).setVisibility(View.GONE);
                    findViewById(R.id.test2).setVisibility(View.GONE);
                }
                toggle = !toggle;
            }
        });

    }

}

编辑:我实际上在TextViewtest1旁边有另一个test2,它应该始终可见,所以我不能隐藏{ {1}}本身。

4 个答案:

答案 0 :(得分:6)

我用其他问题解决了这个问题。 See this answer

引用:

我认为最简单的方法是扩展Animation类并覆盖applyTransformation()以更改视图的高度,如下所示:

import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.LinearLayout;

public class MyCustomAnimation extends Animation {

    public final static int COLLAPSE = 1;
    public final static int EXPAND = 0;

    private View mView;
    private int mEndHeight;
    private int mType;
    private LinearLayout.LayoutParams mLayoutParams;

    public MyCustomAnimation(View view, int duration, int type) {

        setDuration(duration);
        mView = view;
        mEndHeight = mView.getHeight();
        mLayoutParams = ((LinearLayout.LayoutParams) view.getLayoutParams());
        mType = type;
        if(mType == EXPAND) {
            mLayoutParams.height = 0;
        } else {
            mLayoutParams.height = LayoutParams.WRAP_CONTENT;
        }
        view.setVisibility(View.VISIBLE);
    }

    public int getHeight(){
        return mView.getHeight();
    }

    public void setHeight(int height){
        mEndHeight = height;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {

        super.applyTransformation(interpolatedTime, t);
        if (interpolatedTime < 1.0f) {
            if(mType == EXPAND) {
                mLayoutParams.height =  (int)(mEndHeight * interpolatedTime);
            } else {
                mLayoutParams.height = (int) (mEndHeight * (1 - interpolatedTime));
            }
            mView.requestLayout();
        } else {
            if(mType == EXPAND) {
                mLayoutParams.height = LayoutParams.WRAP_CONTENT;
                mView.requestLayout();
            }else{
                mView.setVisibility(View.GONE);
            }
        }
    }
}

要使用它,请按以下方式设置onclick()

int height;

@Override
public void onClick(View v) {
    if(view2.getVisibility() == View.VISIBLE){
        MyCustomAnimation a = new MyCustomAnimation(view2, 1000, MyAnimation.COLLAPSE);
        height = a.getHeight();
        view2.startAnimation(a);
    }else{
        MyCustomAnimation a = new MyCustomAnimation(view2, 1000, MyAnimation.EXPAND);
        a.setHeight(height);
        view2.startAnimation(a);
    }
}

问候。

答案 1 :(得分:3)

我想在我的应用中发生同样的事情。为此:

  1. 在Res / anim中创建合适的动画。我从左侧的动画中使用了幻灯片。如果您对此不满意,可以谷歌其他。

    <强> slide_out_left.xml

    <translate
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromXDelta="0"
        android:toXDelta="-50%p" />
    
    <alpha
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />
    

  2. 将上面定义的动画附加到您想要制作动画的视图中(在您的情况下,包含text1和text2的child1)

    Animation outAnimation;
    LinearLayout a1 = (LinearLayout)findViewById(R.id.child1); 
    
    outAnimation = (Animation) AnimationUtils.loadAnimation (getApplicationContext(), R.anim.slide_out_left);
    
    a1.setAnimation(outAnimation);
    outAnimation.setAnimationListener(new  AnimationListener() {
    
        @Override
        public void onAnimationEnd(Animation arg0) {
        a1.setVisibility(View.GONE);                            
        }
        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub                          
        }
    
        @Override
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub
        }                 
    });
    a1.startAnimation(outAnimation);
    
  3. 注意我用child1而不是text3附加了动画,因为当child1慢慢滑出时会自动给出text3滑入的错觉。

答案 2 :(得分:1)

使用此动画XML slide_in_left.XML

<translate
    android:duration="500"
    android:fromXDelta="-100%"
    android:fromYDelta="0%"
    android:toXDelta="0%"
    android:toYDelta="0%" />
</set>

和slid_in_right.XML

<translate
    android:duration="500"
    android:fromXDelta="100%"
    android:fromYDelta="0%"
    android:toXDelta="0%"
    android:toYDelta="0%" />
</set>

使用此动画 动画slideinleft,slideinright;

并启动此动画

public void AnimationInitialization() {

    slideinleft= AnimationUtils
            .loadAnimation(this, R.anim.slide_in_left);


    slideinright= AnimationUtils.loadAnimation(this,
            R.anim.slide_in_right);

}

并将此2函数称为更改visibilty

public void showMenu() {

    linearlayout_first.clearAnimation();

    linearlayout_first.startAnimation(slideinleft);

    linearlayout_first.setVisibility(View.VISIBLE);

}

public void hideMenu() {

    linearlayout_second.clearAnimation();
    linearlayout_second.startAnimation(slideinright);


    linearlayout_second.setVisibility(View.GONE);
}

您可以根据需要更改布局。 也改变了动画XML的Xdelta和Ydelta ..

答案 3 :(得分:1)

文档有点令人困惑。对于android:animateLayoutChanges,它说

  

...当此标志设置为true时,将在ViewGroup容器上设置默认的LayoutTransition对象,并在发生这些布局更改时运行默认动画。

虽然ViewGroup类的setLayoutTransition方法的文档说:

  

默认情况下,转换对象为null(因此布局更改不是   动画)。

您应该尝试在布局上设置LayoutTransition

以下是一个示例.. http://www.java2s.com/Code/Android/UI/UseLayoutTransitiontoautomatetransitionanimationsasitemsarehiddenorshowninacontainer.htm