android - 我可以部分隐藏布局吗?

标签 android android-layout android-animation

由于我精通 MS Paint,因此我只会上传一张图片来 self 描述我想要实现的目标。

enter image description here

我已经搜索过,但我不太确定要搜索什么。我发现了一种叫做动画的东西。我成功地从 View 中旋转、淡入淡出等元素(通过这个很棒的教程 http://www.vogella.com/articles/AndroidAnimation/article.html )

但这对于我想要实现的目标来说有点有限,现在我陷入了困境,因为我不知道这在 Android 开发中是如何真正被调用的。尝试过“滚动布局”之类的词,但我没有得到任何更好的结果。

你能给我一些建议吗?

谢谢。

您可以使用此应用程序查看一个实例:https://play.google.com/store/apps/details?id=alexcrusher.just6weeks

真诚的,

塞尔吉

最佳答案

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

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

然后在 onClick方法无论你想用什么来控制它,设置 Visibility之间VisibleGone.

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用于隐藏/可见 View 。

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

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

关于android - 我可以部分隐藏布局吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15388661/

相关文章:

java - 在布局中插入多个项目

android - Android 的功能发现动画

android - Android中缩放、旋转、平移后如何获取View相对于Parent的位置?

java - 如何在 Android 中获取 IST 时区

android - Osmdroid,如何创建一个保持在 map 中心的静态叠加项

Android 软件键盘触发快速搜索栏

android - 如何设置android布局以支持所有屏幕尺寸?

java - ViewFlipper 中动画的 OutOfMemory

android - 为什么 ImageView alpha 动画表现异常?

android - 选择应用程序以编程方式打开文件