Android gallery兑现图片的左右循环旋转源码分享(转)

Android gallery实现图片的左右循环旋转源码分享(转)

三步走:第一步初始化gallery时设置较大的初始化位置
Gallery gallery = ((Gallery) findViewById(R.id.myGallery1));
    gallery.setAdapter(new ImageAdapter(this));
    gallery.setSelection(200);
第二步:重写 BaseAdapter方法中的getCount时返回一个较大的值:
// 为了使资源循环使用
    public int getCount()
    {
      return Integer.MAX_VALUE;
    }
第三步:重写BaseAdapter时使用用position对集合大小取余的值,如下:
/* 取得目前欲显示的图像View,传入数组ID值使之读取与成像 */
    public View getView(int position, View convertView, ViewGroup parent)
    {
      /* 创建一个ImageView对象 */
      ImageView i = new ImageView(this.myContext);
      i.setPadding(10, 10, 10, 10);
      i.setAlpha(80);
      // i.setImageResource(this.myImageIds[position]);
      if(position<0){
        position =position+myImageIds.length;
      }
      i.setImageResource(this.myImageIds[position% myImageIds.length]);
      i.setScaleType(ImageView.ScaleType.FIT_XY);
      i.setBackgroundResource(mGalleryItemBackground);
      /* 设置这个ImageView对象的宽高,单位为dip */
      i.setLayoutParams(new Gallery.LayoutParams(85, 72));
      return i;
    }

以下是该类的完整代码:
   /* 依据距离*的位移量 利用getScale返回views的大小(0.0f to 1.0f) */

  1. package irdc.ex03_15;

  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.res.TypedArray;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.AdapterView;
  9. import android.widget.BaseAdapter;
  10. import android.widget.Gallery;
  11. import android.widget.ImageView;
  12. import android.widget.TextView;
  13. import android.widget.Toast;
  14. import android.widget.AdapterView.OnItemSelectedListener;

  15. public class EX03_15 extends Activity
  16. {
  17.   private TextView mTextView01;
  18.   @Override
  19.   public void onCreate(Bundle savedInstanceState)
  20.   {
  21.     super.onCreate(savedInstanceState);
  22.     setContentView(R.layout.main);

  23.     Gallery gallery = ((Gallery) findViewById(R.id.myGallery1));
  24.     gallery.setAdapter(new ImageAdapter(this));
  25.     gallery.setSelection(200);
  26.     

  27.     gallery.setOnItemSelectedListener(new OnItemSelectedListener()
  28.     {

  29.       public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
  30.           long arg3)
  31.       {
  32.         Toast.makeText(EX03_15.this, "当前位置:" + arg2, Toast.LENGTH_SHORT).show();
  33.       }

  34.       public void onNothingSelected(AdapterView<?> arg0)
  35.       {

  36.       }
  37.     });
  38.   }

  39.   public class ImageAdapter extends BaseAdapter
  40.   {
  41.     /* 类成员 myContext为Context父类 */
  42.     private Context myContext;
  43.     /*声明GalleryItemBackground*/
  44.     int mGalleryItemBackground;

  45.     /* 使用android.R.drawable里的图片作为图库来源,类型为整数数组 */
  46.     private int[] myImageIds =
  47.     { R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4,
  48.         R.drawable.a5, R.drawable.a27 };

  49.     /* 构造器只有一个参数,即要存储的Context */
  50.     public ImageAdapter(Context c)
  51.     {
  52.       myContext = c;

  53.       /*
  54.        * 使用在res/values/attrs.xml中的<declare-styleable>定义 的Gallery属性.
  55.        */
  56.       TypedArray a = obtainStyledAttributes(R.styleable.Gallery);

  57.       /* 取得Gallery属性的Index id */
  58.       mGalleryItemBackground = a.getResourceId(
  59.           R.styleable.Gallery_android_galleryItemBackground, 0);

  60.       /* 让对象的styleable属性能够反复使用 */
  61.       a.recycle();
  62.     }

  63.     /* 返回所有已定义的图片总数量 */
  64.     // public int getCount() { return this.myImageIds.length; }
  65.     // 为了使资源循环使用
  66.     public int getCount()
  67.     {
  68.       return Integer.MAX_VALUE;
  69.     }

  70.     /* 利用getItem方法,取得目前容器中图像的数组ID */
  71.     public Object getItem(int position)
  72.     {
  73.       return position;
  74.     }

  75.     public long getItemId(int position)
  76.     {
  77.       return position;
  78.     }

  79.     /* 取得目前欲显示的图像View,传入数组ID值使之读取与成像 */
  80.     public View getView(int position, View convertView, ViewGroup parent)
  81.     {
  82.       /* 创建一个ImageView对象 */
  83.       ImageView i = new ImageView(this.myContext);
  84.       i.setPadding(10, 10, 10, 10);
  85.       i.setAlpha(80);
  86.       // i.setImageResource(this.myImageIds[position]);
  87.       if(position<0){
  88.         position =position+myImageIds.length;
  89.       }
  90.       i.setImageResource(this.myImageIds[position% myImageIds.length]);
  91.       i.setScaleType(ImageView.ScaleType.FIT_XY);
  92.       i.setBackgroundResource(mGalleryItemBackground);
  93.       /* 设置这个ImageView对象的宽高,单位为dip */
  94.       i.setLayoutParams(new Gallery.LayoutParams(85, 72));
  95.       return i;
  96.     }

  97.     /* 依据距离*的位移量 利用getScale返回views的大小(0.0f to 1.0f) */
  98.     public float getScale(boolean focused, int offset)
  99.     {
  100.       return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
  101.     }
  102.   }
  103. }
转自 :http://tigerszdf.blog.163.com/blog/static/45955133201021934744134/