Android 带动画的下滑和上滑布局

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23925907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:37:44  来源:igfitidea点击:

Slidedown and slideup layout with animation

androidanimationslidedownslideup

提问by EliasM

how can I display a layout in the center with slideUp when I press the button, and press again to hide ... slideDown in ANDROID

当我按下按钮时,如何使用 slideUp 在中心显示布局,然后再次按下以隐藏......在 ANDROID 中的 slideDown

help me with that, thnkss

帮我解决这个问题,谢谢

回答by Ando Masahashi

Create two animation xml under res/anim folder

在 res/anim 文件夹下创建两个动画xml

slide_down.xml

幻灯片文件.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate
    android:duration="1000"
    android:fromYDelta="0"
    android:toYDelta="100%" />
</set>

slide_up.xml

幻灯片_up.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate
    android:duration="1000"
    android:fromYDelta="100%"
    android:toYDelta="0" />
</set>

Load animation Like bellow Code and start animation when you want According to your Requirement

加载动画如下代码并根据您的要求在需要时启动动画

//Load animation 
Animation slide_down = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.slide_down);

Animation slide_up = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.slide_up);

// Start animation
linear_layout.startAnimation(slide_down); 

回答by neoteknic

I use these easy functions, it work like jquery slideUp slideDown, use it in an helper class, just pass your view :

我使用这些简单的函数,它像 jquery slideUp slideDown 一样工作,在辅助类中使用它,只需传递您的视图:

public static void expand(final View v) {
    v.measure(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
    final int targetHeight = v.getMeasuredHeight();

    // Older versions of android (pre API 21) cancel animations for views with a height of 0.
    v.getLayoutParams().height = 1;
    v.setVisibility(View.VISIBLE);
    Animation a = new Animation()
    {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            v.getLayoutParams().height = interpolatedTime == 1
                    ? WindowManager.LayoutParams.WRAP_CONTENT
                    : (int)(targetHeight * interpolatedTime);
            v.requestLayout();
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    // 1dp/ms
    a.setDuration((int) (targetHeight / v.getContext().getResources().getDisplayMetrics().density));
    v.startAnimation(a);
}

public static void collapse(final View v) {
    final int initialHeight = v.getMeasuredHeight();

    Animation a = new Animation()
    {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            if(interpolatedTime == 1){
                v.setVisibility(View.GONE);
            }else{
                v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
                v.requestLayout();
            }
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    // 1dp/ms
    a.setDuration((int)(initialHeight / v.getContext().getResources().getDisplayMetrics().density));
    v.startAnimation(a);
}

回答by Md. Shofiulla

Above method working but this is more realistic slide up and down animation from top of the screen. You can try this why.

上述方法有效,但这是更逼真的从屏幕顶部向上和向下滑动的动画。你可以试试这个为什么。

Just create these two animation under anim folder

只需在anim文件夹下创建这两个动画

slide_down.xml

幻灯片文件.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="200"
        android:fromYDelta="-100%"
        android:toYDelta="0" />
</set> 

slide_up.xml

幻灯片_up.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="200"
        android:fromYDelta="0"
        android:toYDelta="-100%" />
</set>

Load animation in java class like this

像这样在java类中加载动画

imageView.startAnimation(AnimationUtils.loadAnimation(getContext(),R.anim.slide_up));
imageView.startAnimation(AnimationUtils.loadAnimation(getContext(),R.anim.slide_down));

回答by neoteknic

This doesn't work for me, I want to to like jquery slideUp / slideDown function, I tried this code, but it only move the content wich stay at the same place after animation end, the view should have a 0dp height at start of slideDown and the view height (with wrap_content) after the end of the animation.

这对我不起作用,我想喜欢 jquery slideUp / slideDown 功能,我试过这段代码,但它只移动动画结束后保持在同一位置的内容,视图在开始时应该有一个 0dp 高度slideDown 和动画结束后的视图高度(带有 wrap_content)。

回答by sayleep

I had a similar requirement in the app I am working on. And, I found a third-party library which does a slide-up, slide-down and slide-right in Android.

我在我正在开发的应用程序中也有类似的要求。而且,我找到了一个第三方库,它可以在 Android 中进行上滑、下滑和右滑。

Refer to the link for more details: https://github.com/mancj/SlideUp-Android

更多详情请参考链接:https: //github.com/mancj/SlideUp-Android

To set up the library(copied from the ReadMe portion of its Github page on request):

设置库(根据要求从其 Github 页面的自述部分复制):

Get SlideUp library

获取 SlideUp 库

Add the JitPack repository to your build file. Add it in your root build.gradle at the end of repositories:

将 JitPack 存储库添加到您的构建文件中。将它添加到存储库末尾的根 build.gradle 中:

allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
    maven { url "https://maven.google.com" } // or google() in AS 3.0
  }
}

Add the dependency (in the Module gradle)

添加依赖项(在 Module gradle 中)

dependencies {
    compile 'com.github.mancj:SlideUp-Android:2.2.1'
    compile 'ru.ztrap:RxSlideUp2:2.x.x' //optional, for reactive listeners based on RxJava-2
    compile 'ru.ztrap:RxSlideUp:1.x.x' //optional, for reactive listeners based on RxJava
}

To add the SlideUp into your project, follow these three simple steps:

要将 SlideUp 添加到您的项目中,请按照以下三个简单步骤操作:

Step 1:

第1步:

create any type of layout

创建任何类型的布局

<LinearLayout
  android:id="@+id/slideView"
  android:layout_width="match_parent"
  android:layout_height="match_parent"/>

Step 2:

第2步:

Find that view in your activity/fragment

在您的活动/片段中找到该视图

View slideView = findViewById(R.id.slideView);

Step 3:

第 3 步:

Create a SlideUp object and pass in your view

创建一个 SlideUp 对象并传入您的视图

slideUp = new SlideUpBuilder(slideView)
                .withStartState(SlideUp.State.HIDDEN)
                .withStartGravity(Gravity.BOTTOM)

                //.withSlideFromOtherView(anotherView)
                //.withGesturesEnabled()
                //.withHideSoftInputWhenDisplayed()
                //.withInterpolator()
                //.withAutoSlideDuration()
                //.withLoggingEnabled()
                //.withTouchableAreaPx()
                //.withTouchableAreaDp()
                //.withListeners()
                //.withSavedState()
                .build();

You may also refer to the sample project on the link. I found it quite useful.

您也可以参考链接上的示例项目。我发现它很有用。