how to animate a gridview by column wise in android

I am new to programming and I have a task to display a gridview with column wise animation. I tried like this.

My res/anim/animation.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="1500"
        android:fromYDelta="5"
        android:toYDelta="90%" />
</set>

part of my MainActivity.java

Animation anim = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.animation);
gridView.setAnimation(anim);
anim.start();

In the MainActivity.java please help me where i have to correct. By this I am getting on full gridview animation.

Upvotes: 2

Views: 2680

Answers (1)

Tony
Tony

Reputation: 2451

Try this in your Adapter's getView(),

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View gridItemView;
    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {

        gridItemView = inflater.inflate(R.layout.layout, null);

        // Grid item child views
        TextView textView = (TextView) gridItemView.findViewById(R.id.grid_text);
        ImageView imageView = (ImageView)gridItemView.findViewById(R.id.grid_image);
        textView.setText(web[position]);
        imageView.setImageResource(imageId[position]);

        // Move this initialization to constructor so that its not initalized again and again.
        Animation anim = AnimationUtils.loadAnimation(mContext,R.anim.item_anim);

        // By default all grid items will animate together and will look like the gridview is 
        // animating as a whole. So, experiment with incremental delays as below to get a 
        // wave effect. 
        anim.setStartOffset(position * 500);

        gridItemView.setAnimation(anim);
        anim.start();
    } else {
        gridItemView = (View) convertView;
    }

    return gridItemView;
}

UPDATE: If you want column-wise animation, try this,

anim.setStartOffset((position % numColumns) * 500);

where, numColumns is the number of columns. You could experiment with different values for setStartOffset() to get varying animation effects.

Upvotes: 3

Related Questions