Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slidedown and slideup layout with animation

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

like image 914
EliasM Avatar asked May 29 '14 04:05

EliasM


People also ask

How do you animate a view?

Create ImageView in the activity_main. xml along with buttons that will add animation to the view. Navigate to the app > res > layout > activity_main. xml.

How do I change an animation layout?

All you need to do is set an attribute in the layout to tell the Android system to animate these layout changes, and system-default animations are carried out for you. Tip: If you want to supply custom layout animations, create a LayoutTransition object and supply it to the layout with the setLayoutTransition() method.

What is translate animation in Android?

Translate Animation can change the visual appearance of an object, but they cannot change the objects themselves. That is, if you apply a translate animation to a view, it would move to a new position, but its click events would not get fired; the click events would still get fired at its previous position.


2 Answers

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);  
like image 119
Ando Masahashi Avatar answered Sep 19 '22 21:09

Ando Masahashi


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); } 
like image 20
neoteknic Avatar answered Sep 18 '22 21:09

neoteknic