在GridView上使用动画



 

 

 

准备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)) ;

  }

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值