准备12张图片名字分别为:png_1、png_2、png_3、png_4、png_5、png_6、
png_7、png_8、png_9、png_10、png_11、png_12。
在main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000">
<GridView
android:id="@+id/myGridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:stretchMode="columnWidth"
android:layoutAnimation="@anim/layout_animation"/>
</LinearLayout>
在res/anim下新建anim_set.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="3000" />
<scale
android:fromXScale="1.0"
android:toXScale="0.0"
android:fromYScale="1.0"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="100"
android:repeatCount="3"
android:duration="3000" />
</set>
在res/anim下新建layout_animation.xml:
<?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/anim_set"/>
在ImageAdapter.java中:
package com.li.animation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context context = null;
private List<Integer> picRes = new ArrayList<Integer>() ;
public ImageAdapter(Context context) {
this.context = context;
this.initPic();
}
public int getCount() {
return this.picRes.size();
}
public Object getItem(int position) {
return this.picRes.get(position);
}
public long getItemId(int position) {
return this.picRes.get(position).intValue();
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView img = new ImageView(this.context);
img.setBackgroundColor(0xFF000000) ;
img.setImageResource(this.picRes.get(position));
img.setScaleType(ImageView.ScaleType.CENTER);
img.setLayoutParams(new GridView.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
img.setPadding(3, 3, 3, 3);
return img;
}
private void initPic(){ // 利用反射加载所有的图片
Field [] fields = R.drawable.class.getDeclaredFields() ;
for (int x = 0; x < fields.length; x++) {
if(fields[x].getName().startsWith("png_")){
try {
this.picRes.add(fields[x].getInt(R.drawable.class)) ;
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
在MyAnimationDemo.java中:
package com.li.animation;
import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;
public class MyAnimationDemo extends Activity {
private GridView myGridView = null ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.myGridView = (GridView) super.findViewById(R.id.myGridView) ;
this.myGridView.setAdapter(new ImageAdapter(this)) ;
}
}