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

本文的效果图:

main.xml源码:

view plain copy to clipboard print ?
  1. <? xml version = "1.0" encoding = "utf-8" ?>
  2. < LinearLayout xmlns: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. < Gallery android:id = "@+id/gallery" android:layout_height = "fill_parent" android:layout_width = "fill_parent" > </ Gallery >
  8. </ LinearLayout >

程序源码:

view plain copy to clipboard print ?
  1. package com.testImageView;
  2. import java.lang.reflect.Field;
  3. import java.util.ArrayList;
  4. import android.app.Activity;
  5. import android.content.Context;
  6. import android.graphics.Bitmap;
  7. import android.graphics.BitmapFactory;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.AdapterView;
  12. import android.widget.BaseAdapter;
  13. import android.widget.Gallery;
  14. import android.widget.ImageView;
  15. import android.widget.AdapterView.OnItemClickListener;
  16. public class testImageView extends Activity{
  17. private GallerymGallery;
  18. @Override
  19. public void onCreate(BundlesavedInstanceState){
  20. super .onCreate(savedInstanceState);
  21. setContentView(R.layout.main);
  22. mGallery=(Gallery)findViewById(R.id.gallery);
  23. try {
  24. mGallery.setAdapter(new ImageAdapter( this ));
  25. }catch (IllegalArgumentExceptione){
  26. //TODOAuto-generatedcatchblock
  27. e.printStackTrace();
  28. }catch (IllegalAccessExceptione){
  29. //TODOAuto-generatedcatchblock
  30. e.printStackTrace();
  31. }
  32. mGallery.setOnItemClickListener(new OnItemClickListener(){
  33. public void onItemClick(AdapterViewparent,Viewv, int position, long id){
  34. testImageView.this .setTitle(String.valueOf(position));
  35. }
  36. });
  37. }
  38. /*
  39. *classImageAdapterisusedtocontrolgallerysourceandoperation.
  40. */
  41. private class ImageAdapter extends BaseAdapter{
  42. private ContextmContext;
  43. private ArrayList<Integer>imgList= new ArrayList<Integer>();
  44. private ArrayList<Object>imgSizes= new ArrayList<Object>();
  45. public ImageAdapter(Contextc) throws IllegalArgumentException,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. int index=field.getInt(R.drawable. class );
  54. //保存图片ID
  55. imgList.add(index);
  56. //保存图片大小
  57. int size[]= new int [ 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. public int getCount(){
  66. //TODOAuto-generatedmethodstub
  67. return imgList.size();
  68. }
  69. @Override
  70. public ObjectgetItem( int position){
  71. //TODOAuto-generatedmethodstub
  72. return position;
  73. }
  74. @Override
  75. public long getItemId( int position){
  76. //TODOAuto-generatedmethodstub
  77. return position;
  78. }
  79. @Override
  80. public ViewgetView( int position,ViewconvertView,ViewGroupparent){
  81. //TODOAuto-generatedmethodstub
  82. ImageViewi=new ImageView(mContext);
  83. //从imgList取得图片ID
  84. i.setImageResource(imgList.get(position).intValue());
  85. i.setScaleType(ImageView.ScaleType.FIT_XY);
  86. //从imgSizes取得图片大小
  87. int size[]= new int [ 2 ];
  88. size=(int [])imgSizes.get(position);
  89. i.setLayoutParams(new Gallery.LayoutParams(size[ 0 ],size[ 1 ]));
  90. return i;
  91. }
  92. };
  93. }

更多相关文章

  1. Android技术积累:图片缓存管理
  2. Android学习笔记(三)UI
  3. 更换android的初始化图片
  4. Android(安卓)开发注意事项
  5. 【转】【Android】获取手机中已安装apk文件信息(PackageInfo、Re
  6. Android使用ImageLoader异步加载网络图片(二)结合listview
  7. Android(安卓)关于嵌套listView时onItemClick事件不响应的解决办
  8. Android图片放大缩小实现方式(一)
  9. 如何实现一个图片加载框架

随机推荐

  1. Android HDMI( 三)
  2. Android 中文 API (102)—— CursorAdapter
  3. Nexus one (Android(安卓)2.1升级Android
  4. 系出名门Android(5) - 控件(View)
  5. Android网络应用接口
  6. Bitmap的加载和Cache
  7. Android 属性动画简介
  8. Android 创建android Splash界面以及避免
  9. android:theme="@android:style/Theme.Li
  10. Android View添加 Listener 小技巧示例