Skip to content

Instantly share code, notes, and snippets.

@leonsomed
Created March 26, 2016 23:26
Show Gist options
  • Select an option

  • Save leonsomed/1605bafb337a94a1d095 to your computer and use it in GitHub Desktop.

Select an option

Save leonsomed/1605bafb337a94a1d095 to your computer and use it in GitHub Desktop.
AnimatedListView Android
package com.leonziyo.imagegallery;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ListView;
/**
* Created by leonziyo on 3/26/16.
*/
public class AnimatedListView extends ListView {
private boolean alreadySetup = false;
public AnimatedListView(Context context) {
this(context, null);
}
public AnimatedListView(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.listViewStyle);
}
public AnimatedListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setDivider(null);
getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (alreadySetup)
return;
alreadySetup = true;
setUpAnimations();
}
});
}
private void setUpAnimations() {
final int childCount = getChildCount();
for(int i = 0; i < childCount; i++)
getChildAt(i).setVisibility(View.INVISIBLE);
final View lastChild = getChildAt(childCount - 1);
lastChild.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
lastChild.removeOnLayoutChangeListener(this);
for (int i = 0; i < childCount; i++)
postAnimation(getChildAt(i), (i+1) * 150);
}
});
}
private void postAnimation(final View element, int delay) {
element.postDelayed(new Runnable() {
@Override
public void run() {
animate(element);
}
}, delay);
}
private void animate(final View element) {
AnimationSet set = new AnimationSet(false);
TranslateAnimation translateAnimation = new TranslateAnimation(0f, 0f, 600f, 0f);
//animation.setInterpolator();
translateAnimation.setInterpolator(getContext(), android.R.anim.accelerate_decelerate_interpolator);
translateAnimation.setDuration(400);
translateAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
element.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationEnd(Animation animation) {
final ScaleAnimation scaleAnimation = new ScaleAnimation(
1f, 1.1f,
1f, 1.1f,
Animation.RELATIVE_TO_SELF, .5f,
Animation.RELATIVE_TO_SELF, .5f
);
scaleAnimation.setRepeatCount(1);
scaleAnimation.setDuration(80);
scaleAnimation.setRepeatMode(Animation.REVERSE);
element.startAnimation(scaleAnimation);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
AlphaAnimation alphaAnimation = new AlphaAnimation(0f, 1f);
alphaAnimation.setDuration(400);
set.addAnimation(translateAnimation);
set.addAnimation(alphaAnimation);
element.startAnimation(set);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment