Skip to content

Instantly share code, notes, and snippets.

@KushtrimPacaj
Created November 11, 2015 07:59
Show Gist options
  • Save KushtrimPacaj/c96dbc8fa493250976e9 to your computer and use it in GitHub Desktop.
Save KushtrimPacaj/c96dbc8fa493250976e9 to your computer and use it in GitHub Desktop.
Expanding animation.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical"
android:paddingBottom="@dimen/_6">
<LinearLayout
android:id="@+id/titleLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blue_text_color"
android:clickable="true"
android:orientation="horizontal">
<TextView
android:id="@+id/header_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="@dimen/_30"
android:paddingLeft="@dimen/_61"
android:paddingTop="@dimen/_30"
android:textColor="@color/white"
android:textSize="@dimen/f30"
/>
<ImageView
android:id="@+id/arrow"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingLeft="@dimen/_50"
android:paddingRight="@dimen/_50"
android:src="@drawable/arrow_white" />
</LinearLayout>
<LinearLayout
android:id="@+id/contentLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/_20"
android:visibility="gone">
</LinearLayout>
</LinearLayout>
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.Transformation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
/**
* Created by Kushtrim on 9/3/2015.
*/
public class ExpandableLayout extends FrameLayout {
public static final int ANIMATION_TIME = 500;
private final String headerTitle;
private int targetHeight;
@Bind(R.id.header_text)
TextView headerTV;
@Bind(R.id.arrow)
ImageView arrow;
@Bind(R.id.contentLayout)
LinearLayout contentLayout;
boolean folded;
public ExpandableLayout(Context context, final String headerTitle, boolean folded, View... rows) {
super(context);
LayoutInflater inflater = LayoutInflater.from(getContext());
LinearLayout expandableLayout = (LinearLayout) inflater.inflate(R.layout.expandable_layout, this, false);
addView(expandableLayout, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
this.headerTitle = headerTitle;
ButterKnife.bind(this);
headerTV.setText(headerTitle);
for (View row : rows) {
contentLayout.addView(row);
}
contentLayout.setPivotY(0);
this.folded = folded;
//get the width
contentLayout.setVisibility(VISIBLE);
contentLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// Ensure you call it only once :
Utils.removeOnGlobalLayoutListener(contentLayout, this);
targetHeight = contentLayout.getHeight();
if (ExpandableLayout.this.folded) {
//contentLayout.setScaleY(0f);
contentLayout.setVisibility(GONE);
} else {
// contentLayout.setScaleY(1f);
contentLayout.setVisibility(VISIBLE);
}
}
});
}
@OnClick(R.id.titleLayout)
public void expandableLayoutClicked() {
if (folded) {
arrow.animate().rotation(90).setDuration(ANIMATION_TIME).start();
expand(contentLayout);
folded = !folded;
} else {
arrow.animate().rotation(0).setDuration(ANIMATION_TIME).start();
collapse(contentLayout);
folded = !folded;
}
}
public void expand(final View v) {
if (v.getLayoutParams().height<=0)
v.getLayoutParams().height = 1;
v.setVisibility(View.VISIBLE);
AnimationSet set = new AnimationSet(false);
final int startHeight = v.getLayoutParams().height;
Log.d("startHeight",startHeight+"");
Animation a = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
v.getLayoutParams().height = (int) ((targetHeight -startHeight) * interpolatedTime + startHeight);
v.requestLayout();
}
@Override
public boolean willChangeBounds() {
return true;
}
};
set.addAnimation(a);
set.setDuration(500);
v.startAnimation(set);
}
public void collapse(final View v) {
final int startHeight = v.getLayoutParams().height;
AnimationSet set = new AnimationSet(false);
Animation a = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if (interpolatedTime == 1) {
v.setVisibility(View.GONE);
} else {
v.getLayoutParams().height = startHeight - (int) (startHeight * interpolatedTime);
v.requestLayout();
}
}
@Override
public boolean willChangeBounds() {
return true;
}
};
set.addAnimation(a);
set.setDuration(500);
v.startAnimation(set);
}
}
public class Utils {
// ... a bunch of other stuff :P
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static void removeOnGlobalLayoutListener(View v, ViewTreeObserver.OnGlobalLayoutListener listener) {
if (Build.VERSION.SDK_INT < 16) {
v.getViewTreeObserver().removeGlobalOnLayoutListener(listener);
} else {
v.getViewTreeObserver().removeOnGlobalLayoutListener(listener);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment