Android控件开发之Gallery

来源:互联网 发布:淘宝网上开店的步骤 编辑:程序博客网 时间:2024/05/16 10:43

GalleryAndroid中的图片库控件。可以浏览自己的图片相册。

 效果图

 

 

 本程序main.xml源码

 

本程序ImageAdapter类源码

package com.sx.Gallery;

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. lt;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. lt;TextView    
  7.    android:layout_width="fill_parent"   
  8.    android:layout_height="wrap_content"   
  9.    android:text="@string/hello" />    
  10. lt;Gallery    
  11.    android:id="@+id/gallery"    
  12.    android:layout_width="fill_parent"    
  13.    android:layout_height="fill_parent"  
  14.    android:spacing="2px" />    
  15.    <!-- android:spacing="2px"用来设置图片之间的间距 -->   
  16.      
  17. lt;/LinearLayout>  




JAVA源码

[html] view plaincopy
  1. import android.content.Context;  
  2. import android.view.View;  
  3. import android.view.ViewGroup;  
  4. import android.widget.BaseAdapter;  
  5. import android.widget.Gallery;  
  6. import android.widget.ImageView;  
  7.   
  8. public class ImageAdapter extends BaseAdapter    
  9. {    
  10.          private Context context;    
  11.       
  12.          private int[] MyImageIDs =    
  13.          {     
  14.         R.drawable.img1,  
  15.         R.drawable.img2,  
  16.         R.drawable.img3,  
  17.         R.drawable.img4,  
  18.         R.drawable.img5,  
  19.         R.drawable.img6,  
  20.         R.drawable.img7,  
  21.         R.drawable.img8,  
  22.         R.drawable.img9,  
  23.         R.drawable.img10,  
  24.         R.drawable.img11,  
  25.         R.drawable.img12,  
  26.         R.drawable.img13,  
  27.         R.drawable.img14,  
  28.         R.drawable.img15,  
  29.         R.drawable.img16,  
  30.         R.drawable.img17,  
  31.         R.drawable.img18,  
  32.         R.drawable.img19,  
  33.         R.drawable.img20  
  34.          };    
  35.       
  36.     public ImageAdapter(Context context)    
  37.     {    
  38.         this.context = context;     
  39.     }    
  40.   
  41.     // 获取图片个数    
  42.     public int getCount()    
  43.     {      
  44.         return MyImageIDs.length;  
  45.         //return Integer.MAX_VALUE;  
  46.     }    
  47.     // 获取图片在库中的位置    
  48.     public Object getItem(int position)    
  49.     {     
  50.         return position;    
  51.     }    
  52.   
  53.     // 获取图片在库中的ID    
  54.     public long getItemId(int id)    
  55.     {     
  56.         return id;    
  57.     }    
  58.   
  59.     public View getView(int position, View convertView, ViewGroup parent)    
  60.     {    
  61.         ImageView imageview = new ImageView(this.context);    
  62.   
  63.         // 给ImageView设置资源     
  64.         imageview.setImageResource(this.MyImageIDs[position]);    
  65.         // 设置布局 图片120*120     
  66.         imageview.setLayoutParams(new Gallery.LayoutParams(120, 120));     
  67.         // 设置显示比例类型     
  68.         imageview.setScaleType(ImageView.ScaleType.FIT_XY);  
  69.   
  70.         //imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);     
  71.         return imageview;     
  72.     }    
  73. }    
  74.   
  75. //要想实现图片无限循环可拖动修改getCount()  函数改为返回值为return Integer.MAX_VALUE;  
  76. //imageview.setImageResource(this.MyImageIDs[position] % MyImageIDs.length.);  即可  
  77.   
  78.    
  79.   
  80. Activity源码  
  81.   
  82. package com.sx.Gallery;  
  83.   
  84. import android.app.Activity;  
  85. import android.os.Bundle;  
  86. import android.view.View;  
  87. import android.widget.AdapterView;  
  88. import android.widget.AdapterView.OnItemClickListener;  
  89. import android.widget.Gallery;  
  90. import android.widget.Toast;  
  91.   
  92. public class GalleryActivity extends Activity   
  93. {  
  94.     /** Called when the activity is first created. */  
  95.     @Override  
  96.     public void onCreate(Bundle savedInstanceState)  
  97.     {  
  98.         super.onCreate(savedInstanceState);  
  99.         setContentView(R.layout.main);  
  100.   
  101.         CreateGallery();  
  102.     }  
  103.       
  104.          private void CreateGallery()  
  105.          {  
  106.             //获得Gallery对象     
  107.                Gallery gellery=(Gallery)this.findViewById(R.id.gallery);   
  108.              
  109.              //添加ImageAdapter给Gallery对象 注意哦Gallery类并没有setAdapter这个方法 这个方法是从AbsSpinner类继承的    
  110.                gellery.setAdapter(new ImageAdapter(this));    
  111.     
  112.              //设置Gallery的背景图片     
  113.                //gellery.setBackgroundResource(R.drawable.bg0);     
  114.     
  115.              //设置Gallery的事件监听     
  116.               gellery.setOnItemClickListener(new GalleryItemListener());     
  117.     
  118.          }     
  119.          class GalleryItemListener implements OnItemClickListener  
  120.          {     
  121.              public void onItemClick(AdapterView<?> parent, View view, int position,  long id)  
  122.              {     
  123.                  Toast.makeText(GalleryActivity.this, "你选择了" + (position + 1) + " 号图片", Toast.LENGTH_SHORT).show();     
  124.              }     
  125.   
  126.          }  
  127. }  
0 0
原创粉丝点击