If you are a fan of Google's android applications, you must haven't missed cards animation that Google Now brings in on Android 4.1, Jelly Bean.
Lets see how to create these smooth animations.
You can browse the source here : https://github.com/shardul/Android/tree/master/NowLayout
These animations will create alternate card animations.
This is where all the magic will happen. To create alternating animations we can definitely grab each child and start respective animation in activity but a better approach would be to create custom layout and handle animation there which is a better approach to take.
Now we have to make use of NowLayout in our activity.
And there you have it. Run the app and see sliding cards similar to Google Now app.
Make sure you share with your friends if you like what you created.
You can also learn about Creating Chrome Style Help Popups on Android.
Lets see how to create these smooth animations.
You can browse the source here : https://github.com/shardul/Android/tree/master/NowLayout
Step 1 : Animations and Styles
There are two animations of similar sliding transition, differing slightly for alternating cards (technically Views). One rotating in from left and other for right.
slide_up_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator" >
<translate
android:duration="@integer/config_slide_time"
android:fromYDelta="100%p"
android:toYDelta="0" />
<alpha
android:duration="@integer/config_slide_time"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
<rotate
android:duration="@integer/config_slide_time"
android:fromDegrees="25"
android:pivotX="0"
android:pivotY="0"
android:toDegrees="0" />
</set>
slide_up_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator" >
<translate
android:duration="@integer/config_slide_time"
android:fromYDelta="100%p"
android:toYDelta="0" />
<alpha
android:duration="@integer/config_slide_time"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
<rotate
android:duration="@integer/config_slide_time"
android:fromDegrees="-25"
android:pivotX="100%"
android:pivotY="0"
android:toDegrees="0" />
</set>
These animations will create alternate card animations.
NowLayout.java
package com.shardul.nowlayout;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
public class NowLayout extends LinearLayout implements OnGlobalLayoutListener {
public NowLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initLayoutObserver();
}
public NowLayout(Context context) {
super(context);
initLayoutObserver();
}
private void initLayoutObserver() {
// force vertical orientation and add observer
setOrientation(LinearLayout.VERTICAL);
getViewTreeObserver().addOnGlobalLayoutListener(this);
}
@Override
public void onGlobalLayout() {
getViewTreeObserver().removeGlobalOnLayoutListener(this);
final int heightPx =getContext().getResources().
getDisplayMetrics().heightPixels;
boolean inversed = false;
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
int[] location = new int[2];
child.getLocationOnScreen(location);
if (location[1] > heightPx) {
break;
}
if (!inversed) {
child.startAnimation(
AnimationUtils.loadAnimation(getContext(),
R.anim.slide_up_left));
} else {
child.startAnimation(
AnimationUtils.loadAnimation(getContext(),
R.anim.slide_up_right));
}
inversed = !inversed;
}
}
}
This is where all the magic will happen. To create alternating animations we can definitely grab each child and start respective animation in activity but a better approach would be to create custom layout and handle animation there which is a better approach to take.
Integrating
Now we have to make use of NowLayout in our activity.
main.xml
<ScrollView 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:fillViewport="true" >
<com.shardul.nowlayout.NowLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e3e3e3"
android:orientation="vertical" >
<TextView
android:id="@+id/txtView"
style="@style/nowCardStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
tools:context=".MainActivity" />
<TextView
android:id="@+id/txtView"
style="@style/nowCardStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
tools:context=".MainActivity" />
<TextView
android:id="@+id/txtView"
style="@style/nowCardStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
tools:context=".MainActivity" />
<TextView
android:id="@+id/txtView"
style="@style/nowCardStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
tools:context=".MainActivity" />
<TextView
android:id="@+id/txtView"
style="@style/nowCardStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
tools:context=".MainActivity" />
</com.shardul.nowlayout.NowLayout>
</ScrollView>
MainActivity.java
package com.shardul.nowlayout;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Make sure you share with your friends if you like what you created.
You can also learn about Creating Chrome Style Help Popups on Android.