如何在android中按列方式为gridview设置动画

时间:2016-07-11 09:14:15

标签: android gridview

我是编程新手,我的任务是显示带有列式动画的gridview。我试过这样的。

我的 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>

MainActivity.java

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

在MainActivity.java中,请帮助我,我必须纠正。通过这个我得到了完整的gridview动画。

1 个答案:

答案 0 :(得分:2)

在适配器的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;
}

更新: 如果你想要逐列动画,试试这个,

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

其中,numColumns是列数。您可以尝试使用setStartOffset()的不同值来获得不同的动画效果。