6.3 Gallery和BaseAdapter容器

来源:互联网 发布:蒙牛puremilk知乎 编辑:程序博客网 时间:2024/06/04 18:48

6.3 Gallery和BaseAdapter容器

2011-12-30 14:37 徐娜子 电子工业出版社 我要评论(0) 字号:T | T

综合评级:

想读(0)  在读(0)  已读(0)   品书斋鉴(0)   已有0人发表书评

一键收藏,随时查看,分享好友!

《Android江湖》第6章人外有人,山外有山,在本章内容中讲解了Android控件的高级用法。首先讲解了对话框中使用进度条控件的基本知识和使用方法,然后依次介绍了Spinner、setDropDownViewResource、Gallery、AnalogClock和DigitalClock控件的基本用法,最后讲解了BaseAdapter容器的基本知识。本节为大家介绍Gallery和BaseAdapter容器。

AD:51CTO云计算架构师峰会 抢票进行中!

6.3 Gallery和BaseAdapter容器

练习3:演练Gallery和BaseAdapter联合使用的方法

源码路径:第6章\Gallery

在此尝试将数张PNG图片导入到Drawable中,并在onCreate时载入到Gallery Widget中,然后添加一个OnItemClick事件,以获取图片的ID编号来响应用户点击图片时的状态,完成Gallery的高级使用。本次演练的重点是如何设置Gallery图片的宽高以及放置图片Layout的大小,在此改写一个继承自BaseAdapter的ImageAdapter容器来存放图片,通过ImageView.setScaleType()方法来改变图片的显示,再通过setLayoutParams() 方法来改变Layout的宽高。

第1步:编写布局文件main.xml,添加一个Gallery和一个ImageView。

第2步:定义layout 外部resource 的xml文件,用来改变layout 的背景。具体代码如下所示:

  1. xml version="1.0" encoding="utf-8"?> 
  2. <resources> 
  3. <declare-styleable name="Gallery"> 
  4. <attr name="android:galleryItemBackground" /> 
  5. declare-styleable> 
  6.  
  7. resources> 

第3步:新建一个myImageAdapter类——Gallery的适配器,它继承于BaseAdapter类。具体代码如下所示:

  1. public class myImageAdapter extends BaseAdapter {  
  2. @Override  
  3. public int getCount() {  
  4. // TODO Auto-generated method stub  
  5. return 0;  
  6. }  
  7. @Override  
  8. public Object getItem(int position) {  
  9. // TODO Auto-generated method stub  
  10. return null;  
  11. }  
  12. @Override  
  13. public long getItemId(int position) {  
  14. // TODO Auto-generated method stub  
  15. return 0;  
  16. }  
  17. @Override  
  18. public View getView(int position, View convertView, ViewGroup parent) {  
  19. // TODO Auto-generated method stub  
  20. return null;  
  21. }  

第4步:修改mainActivity.java,添加Gallery相关操作。主要代码如下所示:

  1. public class Galleryjia extends Activity  
  2. {  
  3. /** Called when the activity is first created. */  
  4. @Override  
  5. public void onCreate(Bundle savedInstanceState)  
  6. {  
  7. super.onCreate(savedInstanceState);  
  8. setContentView(R.layout.main);  
  9. /*通过findViewById取得*/  
  10. Gallery g = (Gallery) findViewById(R.id.mygallery);  
  11. /* 添加一个ImageAdapter并设置给Gallery对象 */  
  12. g.setAdapter(new ImageAdapter(this));  
  13. /* 设置一个itemclickListener并Toast被点击图片的位置 */  
  14. g.setOnItemClickListener(new OnItemClickListener()  
  15. {  
  16. public void onItemClick  
  17. (AdapterView> parent, View v, int position, long id)  
  18. {  
  19. Toast.makeText  
  20. (Galleryjia.this, getString(R.string.my_gallery_text_pre)  
  21. + position+ getString(R.string.my_gallery_text_post),  
  22. Toast.LENGTH_SHORT).show();  
  23. }  
  24. });  
  25. }  
  26. /* 改写BaseAdapter自定义一ImageAdapter class */  
  27. public class ImageAdapter extends BaseAdapter  
  28. {  
  29. /*声明变量*/  
  30. int mGalleryItemBackground;  
  31. private Context mContext;  
  32. /*ImageAdapter的构造器*/  
  33. public ImageAdapter(Context c)  
  34. {  
  35. mContext = c;  
  36. /* 使用在res/values/attrs.xml中的<declare-styleable>定义  
  37. * 的Gallery属性*/  
  38. TypedArray a = obtainStyledAttributes(R.styleable.Gallery);  
  39. /*取得Gallery属性的Index id*/  
  40. mGalleryItemBackground = a.getResourceId  
  41. (R.styleable.Gallery_android_galleryItemBackground, 0);  
  42. /*让对象的styleable属性能够反复使用*/  
  43. a.recycle();  
  44. }  
  45. /* 覆盖的方法getCount,返回图片数目 */  
  46. public int getCount()  
  47. {  
  48. return myImageIds.length;  
  49. }  
  50. /* 覆盖的方法getItemId,返回图像的数组id */  
  51. public Object getItem(int position)  
  52. {  
  53. return position;  
  54. }  
  55. public long getItemId(int position)  
  56. {  
  57. return position;  
  58. }  
  59. /* 覆盖的方法getView,返回一View对象 */  
  60. public View getView  
  61. (int position, View convertView, ViewGroup parent)  
  62. {  
  63. /*产生ImageView对象*/  
  64. ImageView i = new ImageView(mContext);  
  65. /*设置图片给imageView对象*/  
  66. i.setImageResource(myImageIds[position]);  
  67. /*重新设置图片的宽高*/  
  68. i.setScaleType(ImageView.ScaleType.FIT_XY);  
  69. /*重新设置Layout的宽高*/  
  70. i.setLayoutParams(new Gallery.LayoutParams(136, 88));  
  71. /*设置Gallery背景图*/  
  72. i.setBackgroundResource(mGalleryItemBackground);  
  73. /*返回imageView对象*/  
  74. return i;  
  75. }  
  76. /*建构一Integer array并取得预加载Drawable的图片id*/  
  77. private Integer[] myImageIds =  
  78. {  
  79. R.drawable.photo1,  
  80. R.drawable.photo2,  
  81. R.drawable.photo3,  
  82. R.drawable.photo4,  
  83. R.drawable.photo5,  
  84. R.drawable.photo6,  
  85. };  
  86. }  

程序执行后的效果如图6-6所示。当选择一幅图片后,此图片会被放大显示,并显示标号。 (点击查看大图)图6-6 执行效果
原创粉丝点击