我可以部分隐藏布局吗?

时间:2013-03-13 14:52:33

标签: android android-layout android-animation

由于我是MS Paint的大师,我只会上传一张图片,自我描述我想要实现的目标。

enter image description here

我搜索过,但我不确定我该搜索什么。我找到了一种叫做动画的东西。我设法从视图中旋转,淡化等元素(使用这个很棒的教程http://www.vogella.com/articles/AndroidAnimation/article.html

但这对于我想要实现的目标来说有点受限,现在,我被困住了,因为我不知道在android开发中这是怎么称呼的。试过像“scrollup layouts”这样的词,但我没有得到任何更好的结果。

你能给我一些提示吗?

谢谢。

您可以使用此应用找到实时示例:https://play.google.com/store/apps/details?id=alexcrusher.just6weeks

此致

塞吉

2 个答案:

答案 0 :(得分:1)

您可以使用事件onClick()

上的setvisibility功能手动执行此操作

使用此

dynamically adding two views one below other

答案 1 :(得分:1)

使用类似这样的布局(如果您愿意,可以使用线性,相对或其他布局):

<LinearLayout
    android:id="@+id/lty_parent">
    <LinearLayout
        android:id="@+id/lyt_first" />
    <LinearLayout 
        android:id="@+id/lyt_second"/>
</LinearLayout>

然后在onClick方法中,无论您要使用什么来控制它,请在VisibilityVisible之间设置Gone.

public void buttonClickListener(){

    ((Button) findViewById(R.id.your_button))
        .setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {


        if (lyt_second.getVisibility() == View.GONE) {
            lyt_second.setVisibility(View.VISIBILE);
        } 
        else {
            lyt_second.setVisibility(View.GONE);            
        }
    });

如果你只想要一个没有花哨的简单出现/消失,这是好的。如果你想要为它设置动画,事情会变得有点复杂,因为你需要使用负边距来使它看起来像是增长和缩小,如下所示:

我们使用的是与之前相同的onClick方法,但这次点击它时会为隐藏/可见视图启动自定义SlideAnimation

@Override
public void onClick(View v) {
    SlideAnimation slideAnim = new SlideAnimation(lyt_second, time);
    lyt_second.startAnimation(slideAnim);
}

SlideAnimation的实现基于一般Animation类,我们扩展然后覆盖转换。

public SlideAnimation(View view, int duration) {

        //Set the duration of the animation to the int we passed in
        setDuration(duration);

        //Set the view to be animated to the view we passed in
        viewToBeAnimated = view;

        //Get the Margin Parameters for the view so we can edit them
        viewMarginParams = (MarginLayoutParams) view.getLayoutParams();

        //If the view is VISIBLE, hide it after. If it's GONE, show it before we start.
        hideAfter = (view.getVisibility() == View.VISIBLE);

        //First off, start the margin at the bottom margin we've already set. 
        //You need your layout to have a negative margin for this to work correctly.
        marginStart = viewMarginParams.bottomMargin;

        //Decide if we're expanding or collapsing
        if (marginStart == 0){
            marginEnd = 0 - view.getHeight();
        }
        else {
            marginEnd = 0;
        }

        //Make sure the view is visible for our animation
        view.setVisibility(View.VISIBLE);
    }

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

        if (interpolatedTime < 1.0f) {

            // Setting the new bottom margin to the start of the margin 
            // plus the inbetween bits
            viewMarginParams.bottomMargin = marginStart
                    + (int) ((marginEnd - marginStart) * interpolatedTime);

            // Request the layout as it happens so we can see it redrawing
            viewToBeAnimated.requestLayout();

        // Make sure we have finished before we mess about with the rest of it
        } else if (!alreadyFinished) {
            viewMarginParams.bottomMargin = marginEnd;
            viewToBeAnimated.requestLayout();

            if (hideAfter) {
                viewToBeAnimated.setVisibility(View.GONE);
            }
            alreadyFinished = true;
        }
            hideAfter = false;
    }
}

编辑:如果之前有人使用过此代码,并且发现如果在动画完成之前点击多次启动动画的按钮,那么从那时起它就会搞乱动画,使动画完成后始终隐藏视图。我错过了代码底部附近hideAfter布尔值的重置,现在添加了它。

相关问题