Pages

About

Pages - Menu

Ads 468x60px

Social Icons

Main Menu

Featured Posts

Thursday, 19 June 2014

GridView RandomFade animation in Android

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 aGridView such that all cells will appear “RANDOM” with a fade animation.
GridView Random Animation
GridView Random 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
1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?>
       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.
?
1
2
3
4
5
<?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.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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.

1 comments:

  1. Very informative post.I am really impressed by this incredible precious site.

    ReplyDelete

Followers