Android Animate Drop Down/Up View Proper

Android animate drop down/up view proper

So I ended up doing it myself with some help from this answer.
If it had been Android 3.0, I could have used property animation, but it's not so I had to do that myself.

Here is what I ended up with:

import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;

/**
* Class for handling collapse and expand animations.
* @author Esben Gaarsmand
*
*/
public class ExpandCollapseAnimation extends Animation {
private View mAnimatedView;
private int mEndHeight;
private int mType;

/**
* Initializes expand collapse animation, has two types, collapse (1) and expand (0).
* @param view The view to animate
* @param duration
* @param type The type of animation: 0 will expand from gone and 0 size to visible and layout size defined in XML.
* 1 will collapse view and set to gone
*/
public ExpandCollapseAnimation(View view, int duration, int type) {
setDuration(duration);
mAnimatedView = view;
mEndHeight = mAnimatedView.getLayoutParams().height;
mType = type;
if(mType == 0) {
mAnimatedView.getLayoutParams().height = 0;
mAnimatedView.setVisibility(View.VISIBLE);
}
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
if (interpolatedTime < 1.0f) {
if(mType == 0) {
mAnimatedView.getLayoutParams().height = (int) (mEndHeight * interpolatedTime);
} else {
mAnimatedView.getLayoutParams().height = mEndHeight - (int) (mEndHeight * interpolatedTime);
}
mAnimatedView.requestLayout();
} else {
if(mType == 0) {
mAnimatedView.getLayoutParams().height = mEndHeight;
mAnimatedView.requestLayout();
} else {
mAnimatedView.getLayoutParams().height = 0;
mAnimatedView.setVisibility(View.GONE);
mAnimatedView.requestLayout();
mAnimatedView.getLayoutParams().height = mEndHeight;
}
}
}
}

Example ussage:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AnimationTestActivity extends Activity {
private boolean mActive = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final Button animatedButton = (Button) findViewById(R.id.animatedButton);

Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
ExpandCollapseAnimation animation = null;
if(mActive) {
animation = new ExpandCollapseAnimation(animatedButton, 1000, 1);
mActive = false;
} else {
animation = new ExpandCollapseAnimation(animatedButton, 1000, 0);
mActive = true;
}
animatedButton.startAnimation(animation);
}
});
}
}

XML:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/animatedButton"
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="@string/hello"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"/>
</LinearLayout>

Edit

Measure wrap_content height:

So in order to get this to work for wrap_content, I measured the height of the view before I start the animation and then use this measured height as the actual height. Below is the code for measuring the height of the view and set this as the new height (I assume the view uses screen width, change according to your own needs):

/**
* This method can be used to calculate the height and set it for views with wrap_content as height.
* This should be done before ExpandCollapseAnimation is created.
* @param activity
* @param view
*/
public static void setHeightForWrapContent(Activity activity, View view) {
DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

int screenWidth = metrics.widthPixels;

int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(screenWidth, MeasureSpec.EXACTLY);

view.measure(widthMeasureSpec, heightMeasureSpec);
int height = view.getMeasuredHeight();
view.getLayoutParams().height = height;
}

Show and hide a View with a slide up/down animation

With the new animation API that was introduced in Android 3.0 (Honeycomb) it is very simple to create such animations.

Sliding a View down by a distance:

view.animate().translationY(distance);

You can later slide the View back to its original position like this:

view.animate().translationY(0);

You can also easily combine multiple animations. The following animation will slide a View down by its height and fade it in at the same time:

// Prepare the View for the animation
view.setVisibility(View.VISIBLE);
view.setAlpha(0.0f);

// Start the animation
view.animate()
.translationY(view.getHeight())
.alpha(1.0f)
.setListener(null);

You can then fade the View back out and slide it back to its original position. We also set an AnimatorListener so we can set the visibility of the View back to GONE once the animation is finished:

view.animate()
.translationY(0)
.alpha(0.0f)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
view.setVisibility(View.GONE);
}
});

Slidedown and slideup layout with animation

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);

Sliding UP and DOWN Animation for my Layout

Use startAnimation() instead of setAnimation().

Try this:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

first.startAnimation(slide_up);
second.startAnimation(slide_down);
}
});

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="0%"
android:toYDelta="-200%" />
</set>

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="200%" />
</set>

Here is a good tutorial about different types of animation.

Hope this will help~

Up and Down animation on Android view

What I would do is animate views like you did before,with a kind of an infinite loop inside onAnimationEnd :

 //in onPreExecute do levitate(ivSplashLogo, 300, true)
//in onPostExecute do levitate(ivSplashLogo, 300, false)

public void levitate (final View movableView,final float Y,boolean animated){
if(animated) {
final long yourDuration = 200;
final TimeInterpolator yourInterpolator = new DecelerateInterpolator();
movableView.animate().
translationYBy(Y).
setDuration(yourDuration).
setInterpolator(yourInterpolator).
setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
levitate(movableView, -Y, true);
}
});
}
}

I haven't tried this yet but you could give it a go and tell me

How to show dropdown Animation for a list(RecyclerView) under Edittext?

Try this, I hope this will help you.

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/green">

<fragment
android:id="@+id/google_map_fragment"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="15dp">
</android.support.v7.widget.RecyclerView>

</LinearLayout>

</RelativeLayout>


Related Topics



Leave a reply



Submit