Slidedown and slideup layout with animation

前端 未结 5 2015
栀梦
栀梦 2020-11-29 21:21

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

help me with that, thnkss

相关标签:
5条回答
  • 2020-11-29 21:59

    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.

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

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

    Get SlideUp library

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

    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)

    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:

    Step 1:

    create any type of layout

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

    Step 2:

    Find that view in your activity/fragment

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

    Step 3:

    Create a SlideUp object and pass in your view

    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.

    0 讨论(0)
  • 2020-11-29 22:03

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

    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);
    }
    
    0 讨论(0)
  • 2020-11-29 22:12

    Above method is working, but here are more realistic slide up and slide down animations from the top of the screen.

    Just create these two animations under the anim folder

    slide_down.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

    <?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

    imageView.startAnimation(AnimationUtils.loadAnimation(getContext(),R.anim.slide_up));
    imageView.startAnimation(AnimationUtils.loadAnimation(getContext(),R.anim.slide_down));
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-29 22:20

    Create two animation xml under res/anim folder

    slide_down.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

    <?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); 
    
    0 讨论(0)
提交回复
热议问题