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

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. gallery.setOnItemSelectedListener(new OnItemSelectedListener()
  27. {
  28. public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
  29. long arg3)
  30. {
  31. Toast.makeText(EX03_15.this, "当前位置:" + arg2, Toast.LENGTH_SHORT).show();
  32. }
  33. public void onNothingSelected(AdapterView<?> arg0)
  34. {
  35. }
  36. });
  37. }
  38. public class ImageAdapter extends BaseAdapter
  39. {
  40. /* 类成员 myContext为Context父类 */
  41. private Context myContext;
  42. /*声明GalleryItemBackground*/
  43. int mGalleryItemBackground;
  44. /* 使用android.R.drawable里的图片作为图库来源,类型为整数数组 */
  45. private int[] myImageIds =
  46. { R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4,
  47. R.drawable.a5, R.drawable.a27 };
  48. /* 构造器只有一个参数,即要存储的Context */
  49. public ImageAdapter(Context c)
  50. {
  51. myContext = c;
  52. /*
  53. * 使用在res/values/attrs.xml中的<declare-styleable>定义 的Gallery属性.
  54. */
  55. TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
  56. /* 取得Gallery属性的Index id */
  57. mGalleryItemBackground = a.getResourceId(
  58. R.styleable.Gallery_android_galleryItemBackground, 0);
  59. /* 让对象的styleable属性能够反复使用 */
  60. a.recycle();
  61. }
  62. /* 返回所有已定义的图片总数量 */
  63. // public int getCount() { return this.myImageIds.length; }
  64. // 为了使资源循环使用
  65. public int getCount()
  66. {
  67. return Integer.MAX_VALUE;
  68. }
  69. /* 利用getItem方法,取得目前容器中图像的数组ID */
  70. public Object getItem(int position)
  71. {
  72. return position;
  73. }
  74. public long getItemId(int position)
  75. {
  76. return position;
  77. }
  78. /* 取得目前欲显示的图像View,传入数组ID值使之读取与成像 */
  79. public View getView(int position, View convertView, ViewGroup parent)
  80. {
  81. /* 创建一个ImageView对象 */
  82. ImageView i = new ImageView(this.myContext);
  83. i.setPadding(10, 10, 10, 10);
  84. i.setAlpha(80);
  85. // i.setImageResource(this.myImageIds[position]);
  86. if(position<0){
  87. position =position+myImageIds.length;
  88. }
  89. i.setImageResource(this.myImageIds[position% myImageIds.length]);
  90. i.setScaleType(ImageView.ScaleType.FIT_XY);
  91. i.setBackgroundResource(mGalleryItemBackground);
  92. /* 设置这个ImageView对象的宽高,单位为dip */
  93. i.setLayoutParams(new Gallery.LayoutParams(85, 72));
  94. return i;
  95. }
  96. /* 依据距离中央的位移量 利用getScale返回views的大小(0.0f to 1.0f) */
  97. public float getScale(boolean focused, int offset)
  98. {
  99. return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
  100. }
  101. }
  102. }
复制代码

更多相关文章

  1. ProGuard Eclipse Android
  2. Android(安卓)Bluetooth 移植(上)
  3. Android的线程使用来更新UI----Thread、Handler、Looper、TimerT
  4. Fastboot使用详解
  5. Android(安卓)JNI和NDK学习(09)--JNI实例二 传递类对象
  6. 箭头函数的基础使用
  7. 类和 Json对象
  8. NPM 和webpack 的基础使用
  9. Python list sort方法的具体使用

随机推荐

  1. Android Malware Genome Project
  2. 9.0 添加自定义服务
  3. BlueStacks让 Android 应用跑在 Windows
  4. 【已解决】上传项目到binary 上面的时候
  5. Android(安卓)视频播放器 应用层设计 考
  6. Android 相机拍照获取图片并保存到指定位
  7. Android中SensorManager.getRotationMatr
  8. android中图片加载到内存的实例代码
  9. Android 平台的檔案讀寫方式
  10. 基于Platinum库的DMS实现(android)