In some of my older posts I have already showed a lot of animations on ListView and GridViews. Search coderzheaven for ListView and GridView animations if you want to see the articles and posts.
This is also yet another post on animation. Here what I will do is create an animation in a GridView such that all cells will appear “RANDOM” with a fade animation.
So we will start
Click on the download link to download the code.
We will start with the animation XML first.
First create a folder named “anim” inside the “res” folder of your project.
Inside it create fade.xml and copy this code into it.
fade.xml
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="@android:integer/config_longAnimTime" />
Then create anotther xml file named “layout_random_fade.xml” and copy this code into it.
<?xml version="1.0" encoding="utf-8"?> <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" android:delay="0.5" android:animationOrder="random" android:animation="@anim/fade" />
This is the factor that creates the random animation.
android:animationOrder=”random”
And this one creates the fade animation.
android:animation=”@anim/fade”
Now we will write the activity that uses the gridview to create the animation.
package com.coderzheaven.gridviewrandomfadeanimation; import java.util.List; import android.app.Activity; import android.content.Intent; import android.content.pm.ResolveInfo; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); loadApps(); setContentView(R.layout.activity_main); GridView grid = (GridView) findViewById(R.id.grid); grid.setAdapter(new AppsAdapter()); } private List<ResolveInfo> mApps; private void loadApps() { Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); mApps = getPackageManager().queryIntentActivities(mainIntent, 0); } public class AppsAdapter extends BaseAdapter { public View getView(int position, View convertView, ViewGroup parent) { ImageView i = new ImageView(MainActivity.this); ResolveInfo info = mApps.get(position % mApps.size()); i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager())); i.setScaleType(ImageView.ScaleType.FIT_CENTER); final int w = (int) (36 * getResources().getDisplayMetrics().density + 0.5f); i.setLayoutParams(new GridView.LayoutParams(w, w)); return i; } public final int getCount() { return Math.min(32, mApps.size()); } public final Object getItem(int position) { return mApps.get(position % mApps.size()); } public final long getItemId(int position) { return position; } } }
Ok Now it’s done. Run the project and see the result.
You can download the complete java android sample code from here.
Pingback: Wave Scale Animation in GridViews in Android