我可以部分地隐藏布局? [英] Can I partially hide a layout?

查看:177
本文介绍了我可以部分地隐藏布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我在MS画图高手,我只是上传图片selfdescripting我想要达到的。

As I've a master in MS Paint, I will just upload a picture selfdescripting what I'm trying to achieve.

我已经搜查,但我真的不知道我该怎么一直进行搜索。我发现一些所谓的动画。我设法旋转,淡入淡出等从视图元素(与这个伟大的教程的http:/ /www.vogella.com/articles/AndroidAnimation/article.html

I've searched, but I'm not really sure what do I've to search. I've found something called Animations. I managed to rotate, fade, etc an element from a View (with this great tutorial http://www.vogella.com/articles/AndroidAnimation/article.html)

但是,这是一个有点限制了我想要实现的,而现在,我坚持,因为我不知道这是怎么真的叫在android开发做。试图像scrollup布局,但我没有得到任何更好的结果。

But this is a bit limited for what I'm trying to achieve, and now, I'm stuck, because I don't know how is this really called in android development. Tried words like "scrollup layouts" but I didn't get any better results.

你能给我一些建议吗?

感谢您。

您可以看到一个活生生的例子,这个程序:的https:/ /play.google.com/store/apps/details?id=alexcrusher.just6weeks

You can see a live example, with this app: https://play.google.com/store/apps/details?id=alexcrusher.just6weeks

真诚的,

塞尔吉

推荐答案

(如果你想使用直线,亲属或其他布局)使用像这样的布局:

Use something like this as your layout (Use Linear, Relative or other layout if you wish):

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

然后在任何你想用它来控制它的的onClick 方法,将能见度可见了。

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

这是好的,如果你只是想要一个简单的显示/有没有什么花哨消失。事情得到,如果你想制作动画更有点复杂,因为你需要为了玩切缘阴性,使其出现增长和收缩,像这样:

Which is fine if you just want a simple appear/disappear with nothing fancy. Things get a little bit more complicated if you want to animate it, as you need to play around with negative margins in order to make it appear to grow and shrink, like so:

我们使用我们之前也做了同样的onClick 方法,但这次当我们点击它启动了一个自定义的 SlideAnimation 的隐藏/可见视图。

We use the same onClick method that we did before, but this time when we click it starts up a custom SlideAnimation for the hidden/visible view.

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

SlideAnimation 的实施是基于一般动画类,这是我们扩展,然后覆盖改造。

The implementation of the SlideAnimation is based on a general Animation class, which we extend and then Override the transformation.

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

编辑:如果有人使用过这个code之前,发现如果你点击启动该动画的更多按钮比前一次的动画完成了,它会搞砸动画从那时起,使其总是隐藏视图动画结束之后。我错过了近code底部的 hideAfter 布尔的复位,现在又增加了。

If anyone had used this code before and found that if you click on the button that starts the animation more than once before the animation was finished, it would mess up the animation from then on, causing it to always hide the view after the animation finished. I missed the reset of the hideAfter boolean near the bottom of the code, added it now.

这篇关于我可以部分地隐藏布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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