Skip to content

Instantly share code, notes, and snippets.

@hatamiarash7
Last active January 18, 2018 13:33
Show Gist options
  • Save hatamiarash7/96b6e91e3d3c8297fa7201b57dec0baa to your computer and use it in GitHub Desktop.
Save hatamiarash7/96b6e91e3d3c8297fa7201b57dec0baa to your computer and use it in GitHub Desktop.
Android Animated Background
final ImageView backgroundOne = (ImageView) findViewById(R.id.background_one);
final ImageView backgroundTwo = (ImageView) findViewById(R.id.background_two);
final ValueAnimator animator = ValueAnimator.ofFloat(0.0f, 1.0f);
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setInterpolator(new LinearInterpolator());
animator.setDuration(10000L);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
final float progress = (float) animation.getAnimatedValue();
final float height = backgroundOne.getHeight();
final float translationY = height * progress;
backgroundOne.setTranslationY(translationY);
backgroundTwo.setTranslationY(translationY - height);
}
});
animator.start();
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/background_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/animate_background"/>
<ImageView
android:id="@+id/background_two"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/animate_background"/>
</FrameLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment