实现安卓控件动画GridView

                    我们学了那么多动画,有没有想给控件实现动画呢,这里我们利用以前学过的GridView实现相册图片

缩略的动画效果。

首先我们anim_set文件.xml定义

实现平移缩放的xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
     android:fromXDelta="0.0"
     android:toXDelta="50%"
     android:fromYDelta="0.0"
     android:toYDelta="150%"
     android:duration="3000" />
     <scale
       android:fromXScale="1.0"
       android:toXScale="0.0"
       android:fromYScale="1.0"
       android:toYScale="0.0"
       android:repeatCount="3"
       android:pivotX="50%"
       android:pivotY="50%"
       android:startOffset="100"
       android:duration="3000" 
        
        />

</set>

然后设置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"/>

 接下来定义主布局函数main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   
    tools:context=".LayoutDemo" >

    <GridView
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layoutAnimation="@anim/layout_animation"
        android:stretchMode="columnWidth"
        android:numColumns="3" >
    </GridView>

</RelativeLayout>

接下来我们定义GridView适配器,我们继承BaseAdapter

public class ImageAdapter extends BaseAdapter{
    private Context c;
    private List<Integer> list=new ArrayList<Integer>();
	public ImageAdapter(Context c){
		this.c=c;
		this.initpic();
		
	}
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return list.get(arg0).intValue();
	}

	@Override
	public View getView(int position, View v, ViewGroup arg2) {
		ImageView image=new ImageView(this.c);
		image.setBackgroundColor(0xFF000000);
		image.setImageResource(list.get(position));
		image.setScaleType(ImageView.ScaleType.CENTER);
		image.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
		image.setPadding(3, 3, 3, 3);
		return image;

	}
	public void initpic(){
		Field[] fields=R.drawable.class.getDeclaredFields();
		for(int i=0;i<fields.length;i++){
			if(fields[i].getName().startsWith("pic")){
				try {
					this.list.add(fields[i].getInt(R.drawable.class));
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
			}
		}
		
		
	}

}

这里读取资源文件图片使用了java.lang.reflect反射机制,这是java中较为重要的章节,同学们可以自行查阅

Activity GridView绑定适配器

public class LayoutDemo extends Activity {

	GridView gridview;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_layout_demo);
		gridview=(GridView)super.findViewById(R.id.gridView);
		gridview.setAdapter(new ImageAdapter(this));
	}

	

}

实现效果如下


实现安卓控件动画GridView
 
实现安卓控件动画GridView
 
实现安卓控件动画GridView
 

下一章我们讲ListView动画实现,大家请持续关注哦

相关推荐