Android入门第十二篇之Gallery评论收藏举

15949人阅读评论(43)收藏

本文来自http://blog.csdn.net/hellogv/,引用必须注明出处!

Android的Gallery控件是个很不错的看图控件,大大减轻了开发者对于看图功能的开发,而且效果也比较美观。本文介绍Gallery的用法,用反射机制来动态读取资源中的图片。

本文的效果图:

main.xml源码:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <Galleryandroid:id="@+id/gallery"android:layout_height="fill_parent"android:layout_width="fill_parent"></Gallery>
  8. </LinearLayout>

程序源码:

  1. packagecom.testImageView;
  2. importjava.lang.reflect.Field;
  3. importjava.util.ArrayList;
  4. importandroid.app.Activity;
  5. importandroid.content.Context;
  6. importandroid.graphics.Bitmap;
  7. importandroid.graphics.BitmapFactory;
  8. importandroid.os.Bundle;
  9. importandroid.view.View;
  10. importandroid.view.ViewGroup;
  11. importandroid.widget.AdapterView;
  12. importandroid.widget.BaseAdapter;
  13. importandroid.widget.Gallery;
  14. importandroid.widget.ImageView;
  15. importandroid.widget.AdapterView.OnItemClickListener;
  16. publicclasstestImageViewextendsActivity{
  17. privateGallerymGallery;
  18. @Override
  19. publicvoidonCreate(BundlesavedInstanceState){
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.main);
  22. mGallery=(Gallery)findViewById(R.id.gallery);
  23. try{
  24. mGallery.setAdapter(newImageAdapter(this));
  25. }catch(IllegalArgumentExceptione){
  26. //TODOAuto-generatedcatchblock
  27. e.printStackTrace();
  28. }catch(IllegalAccessExceptione){
  29. //TODOAuto-generatedcatchblock
  30. e.printStackTrace();
  31. }
  32. mGallery.setOnItemClickListener(newOnItemClickListener(){
  33. publicvoidonItemClick(AdapterViewparent,Viewv,intposition,longid){
  34. testImageView.this.setTitle(String.valueOf(position));
  35. }
  36. });
  37. }
  38. /*
  39. *classImageAdapterisusedtocontrolgallerysourceandoperation.
  40. */
  41. privateclassImageAdapterextendsBaseAdapter{
  42. privateContextmContext;
  43. privateArrayList<Integer>imgList=newArrayList<Integer>();
  44. privateArrayList<Object>imgSizes=newArrayList<Object>();
  45. publicImageAdapter(Contextc)throwsIllegalArgumentException,IllegalAccessException{
  46. mContext=c;
  47. //用反射机制来获取资源中的图片ID和尺寸
  48. Field[]fields=R.drawable.class.getDeclaredFields();
  49. for(Fieldfield:fields)
  50. {
  51. if(!"icon".equals(field.getName()))//除了icon之外的图片
  52. {
  53. intindex=field.getInt(R.drawable.class);
  54. //保存图片ID
  55. imgList.add(index);
  56. //保存图片大小
  57. intsize[]=newint[2];
  58. BitmapbmImg=BitmapFactory.decodeResource(getResources(),index);
  59. size[0]=bmImg.getWidth();size[1]=bmImg.getHeight();
  60. imgSizes.add(size);
  61. }
  62. }
  63. }
  64. @Override
  65. publicintgetCount(){
  66. //TODOAuto-generatedmethodstub
  67. returnimgList.size();
  68. }
  69. @Override
  70. publicObjectgetItem(intposition){
  71. //TODOAuto-generatedmethodstub
  72. returnposition;
  73. }
  74. @Override
  75. publiclonggetItemId(intposition){
  76. //TODOAuto-generatedmethodstub
  77. returnposition;
  78. }
  79. @Override
  80. publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
  81. //TODOAuto-generatedmethodstub
  82. ImageViewi=newImageView(mContext);
  83. //从imgList取得图片ID
  84. i.setImageResource(imgList.get(position).intValue());
  85. i.setScaleType(ImageView.ScaleType.FIT_XY);
  86. //从imgSizes取得图片大小
  87. intsize[]=newint[2];
  88. size=(int[])imgSizes.get(position);
  89. i.setLayoutParams(newGallery.LayoutParams(size[0],size[1]));
  90. returni;
  91. }
  92. };
  93. }

本文来自http://blog.csdn.net/hellogv/,引用必须注明出处!

Android的Gallery控件是个很不错的看图控件,大大减轻了开发者对于看图功能的开发,而且效果也比较美观。本文介绍Gallery的用法,用反射机制来动态读取资源中的图片。

本文的效果图:

main.xml源码:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <Galleryandroid:id="@+id/gallery"android:layout_height="fill_parent"android:layout_width="fill_parent"></Gallery>
  8. </LinearLayout>

程序源码:

  1. packagecom.testImageView;
  2. importjava.lang.reflect.Field;
  3. importjava.util.ArrayList;
  4. importandroid.app.Activity;
  5. importandroid.content.Context;
  6. importandroid.graphics.Bitmap;
  7. importandroid.graphics.BitmapFactory;
  8. importandroid.os.Bundle;
  9. importandroid.view.View;
  10. importandroid.view.ViewGroup;
  11. importandroid.widget.AdapterView;
  12. importandroid.widget.BaseAdapter;
  13. importandroid.widget.Gallery;
  14. importandroid.widget.ImageView;
  15. importandroid.widget.AdapterView.OnItemClickListener;
  16. publicclasstestImageViewextendsActivity{
  17. privateGallerymGallery;
  18. @Override
  19. publicvoidonCreate(BundlesavedInstanceState){
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.main);
  22. mGallery=(Gallery)findViewById(R.id.gallery);
  23. try{
  24. mGallery.setAdapter(newImageAdapter(this));
  25. }catch(IllegalArgumentExceptione){
  26. //TODOAuto-generatedcatchblock
  27. e.printStackTrace();
  28. }catch(IllegalAccessExceptione){
  29. //TODOAuto-generatedcatchblock
  30. e.printStackTrace();
  31. }
  32. mGallery.setOnItemClickListener(newOnItemClickListener(){
  33. publicvoidonItemClick(AdapterViewparent,Viewv,intposition,longid){
  34. testImageView.this.setTitle(String.valueOf(position));
  35. }
  36. });
  37. }
  38. /*
  39. *classImageAdapterisusedtocontrolgallerysourceandoperation.
  40. */
  41. privateclassImageAdapterextendsBaseAdapter{
  42. privateContextmContext;
  43. privateArrayList<Integer>imgList=newArrayList<Integer>();
  44. privateArrayList<Object>imgSizes=newArrayList<Object>();
  45. publicImageAdapter(Contextc)throwsIllegalArgumentException,IllegalAccessException{
  46. mContext=c;
  47. //用反射机制来获取资源中的图片ID和尺寸
  48. Field[]fields=R.drawable.class.getDeclaredFields();
  49. for(Fieldfield:fields)
  50. {
  51. if(!"icon".equals(field.getName()))//除了icon之外的图片
  52. {
  53. intindex=field.getInt(R.drawable.class);
  54. //保存图片ID
  55. imgList.add(index);
  56. //保存图片大小
  57. intsize[]=newint[2];
  58. BitmapbmImg=BitmapFactory.decodeResource(getResources(),index);
  59. size[0]=bmImg.getWidth();size[1]=bmImg.getHeight();
  60. imgSizes.add(size);
  61. }
  62. }
  63. }
  64. @Override
  65. publicintgetCount(){
  66. //TODOAuto-generatedmethodstub
  67. returnimgList.size();
  68. }
  69. @Override
  70. publicObjectgetItem(intposition){
  71. //TODOAuto-generatedmethodstub
  72. returnposition;
  73. }
  74. @Override
  75. publiclonggetItemId(intposition){
  76. //TODOAuto-generatedmethodstub
  77. returnposition;
  78. }
  79. @Override
  80. publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
  81. //TODOAuto-generatedmethodstub
  82. ImageViewi=newImageView(mContext);
  83. //从imgList取得图片ID
  84. i.setImageResource(imgList.get(position).intValue());
  85. i.setScaleType(ImageView.ScaleType.FIT_XY);
  86. //从imgSizes取得图片大小
  87. intsize[]=newint[2];
  88. size=(int[])imgSizes.get(position);
  89. i.setLayoutParams(newGallery.LayoutParams(size[0],size[1]));
  90. returni;
  91. }
  92. };
  93. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值