Created
February 19, 2016 02:31
-
-
Save cutiko/91ff8bc494db96676fd2 to your computer and use it in GitHub Desktop.
Falling leaf animation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:background="@color/colorPrimary" | |
tools:context="SomeActivity"> | |
<!--A simple layout is a full screen relative layout so we can put the image in the middle | |
I use a relative, cause I can not triggered the animation, thoose conditions are not includede | |
in the previous file, but you could check something and see if the animation start or just skip it--> | |
<!--and this is the leaf--> | |
<ImageView | |
android:id="@+id/loadingLeaf" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:src="@mipmap/leaf" | |
android:layout_centerInParent="true"/> | |
<!--You can download the leaf from here--> | |
<!--http://cutiko.cl/wp-content/uploads/2016/02/leaf.zip--> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This code is highly summarized | |
private int height; | |
private Timer timer; | |
private int leafFallDuration = 6000; | |
private int leafRotationDuration = 900; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.some_activity); | |
//We are going to use the height so we have to get it first | |
DisplayMetrics displaymetrics = new DisplayMetrics(); | |
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); | |
height = displaymetrics.heightPixels; | |
//this method is in charge to make the leaf fall from the top over and over again | |
leafFalling(); | |
//we call the first rotation, and the first will call the second and the secon will call the first, recursively, then leaf is always moving | |
firstLeafRotation(); | |
} | |
} | |
private void leafFalling() { | |
//The trick here is, the animation is triggered, so the leaf start to fall | |
//then, the timer is also set, to the exact same duration than the animation | |
//So, when the animation is over, the timer will run out, calling the animation again | |
//and setting the timer, recursivety | |
TranslateAnimation animation = new TranslateAnimation(0, 0, -height/2, 0); | |
animation.setDuration(leafFallDuration); | |
loadingLeaf.startAnimation(animation); | |
TimerTask reloadLeaf = new TimerTask() { | |
@Override | |
public void run() { | |
leafFalling(); | |
} | |
}; | |
timer.schedule(reloadLeaf, leafFallDuration); | |
} | |
void firstLeafRotation() { | |
//The trick here is the same than above, but insted of calling it self when the timer run out | |
//the second animation will be triggered, and the secon will cal the first | |
loadingLeaf.animate().rotationBy(75).setDuration(leafRotationDuration).setInterpolator(new LinearInterpolator()).start(); | |
TimerTask reloadLeaf = new TimerTask() { | |
@Override | |
public void run() { | |
secondLeafRotation(); | |
} | |
}; | |
timer.schedule(reloadLeaf, leafRotationDuration); | |
} | |
private void secondLeafRotation() { | |
loadingLeaf.animate().rotationBy(-75).setDuration(leafRotationDuration).setInterpolator(new LinearInterpolator()).start(); | |
//So when this timer is executed, the first rotation will be called, and will restart the movement | |
TimerTask reloadLeaf = new TimerTask() { | |
@Override | |
public void run() { | |
firstLeafRotation(); | |
} | |
}; | |
timer.schedule(reloadLeaf, leafRotationDuration); | |
} | |
//NOTE: if you want to stop the animation, just use a broadcast receiver listening to some other something running in background |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment