Gallery和ImageView使用

来源:互联网 发布:python 函数声明 编辑:程序博客网 时间:2024/05/14 09:58

Gallery可以显示一系列的图片,并且可以横向滑动。下面展示如何使用Gallery去显示一系列的图片。

1. 创建一个工程,Gallery。

2. main.xml中的代码。

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="Images of San Francisco" />      
  11.       
  12.     <Gallery  
  13.         android:id="@+id/gallery1"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content" />  
  16.   
  17.     <ImageView  
  18.         android:id="@+id/image1"  
  19.         android:layout_width="320dp"  
  20.         android:layout_height="250dp"  
  21.         android:scaleType="fitXY" />  
  22.   
  23. </LinearLayout>  
3. 在res/values文件夹下面新建一个文件,attrs.xml。

4. attrs.xml中的代码。

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <declare-styleable name="Gallery1">  
  4.         <attr name="android:galleryItemBackground" />  
  5.     </declare-styleable>  
  6. </resources>  

5. 准备一些图片。将这些图片放在res/drawable-mdpi下面。


6. GalleryActivity.java中的代码。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class GalleryActivity extends Activity {   
  2.     Integer[] imageIDs = {  
  3.             R.drawable.pic1,  
  4.             R.drawable.pic2,  
  5.             R.drawable.pic3,  
  6.             R.drawable.pic4,  
  7.             R.drawable.pic5,  
  8.             R.drawable.pic6,  
  9.             R.drawable.pic7  
  10.     };  
  11.   
  12.     /** Called when the activity is first created. */  
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.           
  18.         Gallery gallery = (Gallery) findViewById(R.id.gallery1);  
  19.   
  20.         gallery.setAdapter(new ImageAdapter(this));  
  21.         gallery.setOnItemClickListener(new OnItemClickListener()  
  22.         {  
  23.             public void onItemClick(AdapterView<?> parent, View v,  
  24.             int position, long id)  
  25.             {  
  26.                 Toast.makeText(getBaseContext(),  
  27.                         "pic" + (position + 1) + " selected",  
  28.                         Toast.LENGTH_SHORT).show();  
  29.                   
  30.                 ImageView imageView = (ImageView) findViewById(R.id.image1);  
  31.                 imageView.setImageResource(imageIDs[position]);  
  32.             }  
  33.         });  
  34.     }  
  35.       
  36.     public class ImageAdapter extends BaseAdapter  
  37.     {  
  38.         Context context;  
  39.         int itemBackground;  
  40.   
  41.         public ImageAdapter(Context c)  
  42.         {  
  43.             context = c;  
  44.             //---setting the style---  
  45.             TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);              
  46.               
  47.             itemBackground = a.getResourceId(  
  48.                 R.styleable.Gallery1_android_galleryItemBackground, 0);                  
  49.               
  50.             a.recycle();  
  51.         }  
  52.   
  53.         //---returns the number of images---  
  54.         public int getCount() {  
  55.             return imageIDs.length;  
  56.         }  
  57.   
  58.           
  59.         //---returns the item---  
  60.         public Object getItem(int position) {  
  61.             return position;  
  62.         }  
  63.   
  64.          //---returns the ID of an item---  
  65.         public long getItemId(int position) {  
  66.             return position;  
  67.         }        
  68.           
  69.         //---returns an ImageView view---  
  70.         public View getView(int position, View convertView, ViewGroup parent) {  
  71.             ImageView imageView;  
  72.             if (convertView == null) {  
  73.                 imageView = new ImageView(context);  
  74.                 imageView.setImageResource(imageIDs[position]);  
  75.                 imageView.setScaleType(ImageView.ScaleType.FIT_XY);  
  76.                 imageView.setLayoutParams(new Gallery.LayoutParams(150120));                    
  77.             } else {  
  78.                 imageView = (ImageView) convertView;  
  79.             }              
  80.             imageView.setBackgroundResource(itemBackground);  
  81.             return imageView;  
  82.         }  
  83.     }  
  84.   
  85. }  

7. 按F11在模拟器上面调试。会看见一系列的图片,这些图片可以左右滑动。当单击单个图片的时候,会弹出消息。


首先,我们在main.xml中添加Gallery和ImageView控件:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <Gallery  
  2.     android:id="@+id/gallery1"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content" />  
  5.   
  6. <ImageView  
  7.     android:id="@+id/image1"  
  8.     android:layout_width="320dp"  
  9.     android:layout_height="250dp"  
  10.     android:scaleType="fitXY" />  
前面已经提到过,Gallery用来显示一系列的图片,ImageView用来显示被选中的图片。

这些图片的id被保存在imageIDs数组中:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. Integer[] imageIDs = {  
  2.         R.drawable.pic1,  
  3.         R.drawable.pic2,  
  4.         R.drawable.pic3,  
  5.         R.drawable.pic4,  
  6.         R.drawable.pic5,  
  7.         R.drawable.pic6,  
  8.         R.drawable.pic7  
  9. };  
接下来创建BaseAdapter的子类:ImageAdapter,这样一来,我们就能把Gallery与图片资源绑定在一起了。这个适配器起到了桥梁的作用。

使用BaseAdapter的视图的还有:

  • ListView
  • GridView
  • Spinner
  • Gallery

BaseAdapter也有一些子类:

  • ListAdapter
  • ArrayAdapter
  • CursorAdapter
  • SpinnerAdapter

在ImageAdapter中我们主要实现以下的方法:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class ImageAdapter extends BaseAdapter  
  2.     {  
  3.         Context context;  
  4.         int itemBackground;  
  5.   
  6.         public ImageAdapter(Context c)  
  7.         {  
  8.             context = c;  
  9.             //---setting the style---  
  10.             TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);              
  11.               
  12.             itemBackground = a.getResourceId(  
  13.                 R.styleable.Gallery1_android_galleryItemBackground, 0);                  
  14.               
  15.             a.recycle();  
  16.         }  
  17.   
  18.         //---returns the number of images---  
  19.         public int getCount() {  
  20.             return imageIDs.length;  
  21.         }  
  22.   
  23.           
  24.         //---returns the item---  
  25.         public Object getItem(int position) {  
  26.             return position;  
  27.         }  
  28.   
  29.          //---returns the ID of an item---  
  30.         public long getItemId(int position) {  
  31.             return position;  
  32.         }        
  33.           
  34.         //---returns an ImageView view---  
  35.         public View getView(int position, View convertView, ViewGroup parent) {  
  36.             ImageView imageView;  
  37.             if (convertView == null) {  
  38.                 imageView = new ImageView(context);  
  39.                 imageView.setImageResource(imageIDs[position]);  
  40.                 imageView.setScaleType(ImageView.ScaleType.FIT_XY);  
  41.                 imageView.setLayoutParams(new Gallery.LayoutParams(150120));                    
  42.             } else {  
  43.                 imageView = (ImageView) convertView;  
  44.             }              
  45.             imageView.setBackgroundResource(itemBackground);  
  46.             return imageView;  
  47.         }  
  48.     }  
0 0
原创粉丝点击