动画视图滑出另一个视图,将视图推到下面

时间:2012-12-14 14:35:44

标签: android android-animation

我有一个按钮列表。当我按下按钮时,视图应该向下滑动按钮,如下所示:

开始:
enter image description here

中途:
enter image description here

结束:
enter image description here

我该怎么做?应该滑出的视图比按钮大,因此首先隐藏按钮后面的View然后向下滑动会导致视图在按钮上方可见。这不应该发生。

关于如何处理此问题的任何想法或示例?

4 个答案:

答案 0 :(得分:73)

我认为最简单的方法是扩展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, MyCustomAnimation.COLLAPSE);
        height = a.getHeight();
        view2.startAnimation(a);
    }else{
        MyCustomAnimation a = new MyCustomAnimation(view2, 1000, MyCustomAnimation.EXPAND);
        a.setHeight(height);
        view2.startAnimation(a);
    }
}

问候。

答案 1 :(得分:4)

以下是手工制作动画的简单示例,它提供了您想要的效果。它适用于测试应用程序,但我不确定是否存在错误:

public class MainActivity extends Activity implements OnClickListener {
private Timer timer;
private TimerTask animationTask;
private View view1;
private View view2;
boolean animating;
boolean increasing = true;
int initHeight = -1;
private LayoutParams params;

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

    view1 = findViewById(R.id.view1);// clickable view
    view1.setOnClickListener(this); 

    view2 = findViewById(R.id.view2);// animated view
    params = view2.getLayoutParams();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    timer.cancel();
}

@Override
public void onClick(View v) {
    Toast.makeText(this, "start animating...", Toast.LENGTH_SHORT).show();

    animationTask = new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (animationFinished()) {
                        animating = false;
                        cancel();//canceling animating task
                        return;
                    }
                    params.height += increasing ? 1 : -1;
                    view2.setLayoutParams(params);
                }
            });
        }

        private boolean animationFinished() {
            int viewHeight = view2.getHeight();
            if (increasing && viewHeight >= initHeight) {
                return true;
            }
            if (!increasing && viewHeight <= 0) {
                return true;
            }
            return false;
        }
    };

    //if we already animating - we just change direction of animation
    increasing = !increasing;
    if (!animating) {
        animating = true;
        int height = view2.getHeight();

        params.height = height;
        view2.setLayoutParams(params);//change param "height" from "wrap_conent" to real height

        if (initHeight < 0) {//height of view - we setup it only once
            initHeight = height;
        }
        timer.schedule(animationTask, 0, 10);//changing repeat time here will fasten or slow down animation
    }
}
}

答案 2 :(得分:1)

我会那样做。首先是整个可折叠面板组件的布局:(伪xml)

RelativeLayout (id=panel, clip)
    LinearLayout (id=content, alignParentBottom=true)
    LinearLayout (id=handle, above=content)

这应确保内容始终低于句柄。

然后当你需要崩溃时:

  • 将内容的上边距从0设置为-content.height
  • 将面板的高度从当前设置为当前content.height

答案 3 :(得分:1)

只需将android:animateLayoutChanges传递给包含所有视图的LinearLayout,即可获得所需的结果。