Android 我可以部分隐藏布局吗?

Android 我可以部分隐藏布局吗?,android,android-layout,android-animation,Android,Android Layout,Android Animation,由于我是微软绘画大师,我只需上传一张图片,自我描述我正在努力实现的目标 我已经搜索过了,但我不确定我要搜索什么。我发现了一种叫做动画的东西。我成功地从视图中旋转、淡入等元素(使用本教程) 但这对于我想要实现的目标来说有点有限,现在,我被卡住了,因为我不知道在android开发中这到底是怎么回事。我尝试了“滚动布局”之类的词,但没有得到更好的结果 你能给我一些提示吗 多谢各位 使用此应用程序,您可以看到一个实时示例: 真诚地 Sergi您可以通过在event onClick()上使用setvis

由于我是微软绘画大师,我只需上传一张图片,自我描述我正在努力实现的目标

我已经搜索过了,但我不确定我要搜索什么。我发现了一种叫做动画的东西。我成功地从视图中旋转、淡入等元素(使用本教程)

但这对于我想要实现的目标来说有点有限,现在,我被卡住了,因为我不知道在android开发中这到底是怎么回事。我尝试了“滚动布局”之类的词,但没有得到更好的结果

你能给我一些提示吗

多谢各位

使用此应用程序,您可以看到一个实时示例:

真诚地


Sergi

您可以通过在event onClick()上使用setvisibility功能手动执行此操作

用这个


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

如果你只是想要一个简单的出现/消失,没有任何幻想,这是很好的。如果你想给它设置动画,事情会变得更复杂一些,因为你需要使用负的边距来让它看起来变大变小,就像这样:

我们使用了与以前相同的
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;
    }
}
public SlideAnimation(视图,整数持续时间){
//将动画的持续时间设置为传入的int
设置持续时间(持续时间);
//将要设置动画的视图设置为我们传入的视图
viewToBeAnimated=视图;
//获取视图的边距参数,以便我们可以编辑它们
viewMarginParams=(MarginLayoutParams)view.getLayoutParams();
//如果视图可见,请在之后隐藏它。如果它已消失,请在开始之前显示它。
hideAfter=(view.getVisibility()==view.VISIBLE);
//首先,从我们已经设置的底部开始计算边距。
//你需要你的布局有一个负的空白,这样才能正常工作。
margintart=viewMarginParams.bottomMargin;
//决定我们是在扩张还是崩溃
如果(marginstat==0){
marginEnd=0-view.getHeight();
}
否则{
marginEnd=0;
}
//确保视图对动画可见
view.setVisibility(view.VISIBLE);
}
@凌驾
受保护的无效应用转换(浮点插值时间,转换t){
超级应用转换(插值时间,t);
如果(插值时间<1.0f){
//将新的底部边距设置为边距的开头
//加上中间位
viewMarginParams.bottomMargin=marginStart
+(int)((marginEnd-marginstat)*内插时间);
//请求布局,因为它发生了,所以我们可以看到它重画
viewToBeAnimated.requestLayout();
//在我们把剩下的事情搞砸之前,一定要把它做完
}否则,如果(!已完成){
viewMarginParams.bottomMargin=marginEnd;
viewToBeAnimated.requestLayout();
如果(隐藏后){
viewToBeAnimated.setVisibility(View.GONE);
}
alreadyFinished=真;
}
hideAfter=假;
}
}

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

2个相对值(上半部分+下半部分)在其他相对值(行)中应该做的事情…+PCCoder注释:)您试过了吗?@Selvin的基本意思是,在一个大视图组(表示列表视图项)中应该有两个视图组。在你的例子中,你所需要做的就是将第二个视图组的可见性设置为可见和消失之间。我想他也想要这个动画?是的,某种程度上是动画的,所以从可见到隐藏的“移动”是“缓慢”发生的,而不是瞬间发生的。
@Override
public void onClick(View v) {
    SlideAnimation slideAnim = new SlideAnimation(lyt_second, time);
    lyt_second.startAnimation(slideAnim);
}
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;
    }
}