Gallery

Gallery

       Gallery是一个水平的列表选择框,它允许用户通过拖动来查看上一个、下一个列表选项。
下面是控件Gallery的额外的属性:
       要使用一个Gallery非常的简单,只需要设置填充它内容的Adapter即可。从Adapter的体系上来看(可以看看:Android中的Adapter),显然使用BaseAdapter是最好的选择,当然SimpleAdapter也可以,不过,实现起来,没有BaseAdapter清晰和强大。所以这里的Best Practice,个人认为还是BaseAdapter。
       Gallery一般是用来显示图片的,当然也经常用来显示一些自定义布局和伪3D的效果。下面,通过一个简单的例子,来解释Gallery的用法。
一、首先要定义填充Gallery的适配器,这里,选择继承BaseAdapter,自定义自己的适配器
    
    
  1. public class GalleryAdapter extends BaseAdapter { 
  2.     private Context context; 
  3.     private Integer[] imagesId;//要显示的图片 
  4.     public GalleryAdapter(Context context) { 
  5.         this.context = context; 
  6.         imagesId=new Integer[]{R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d}; 
  7.     } 
  8.  
  9.     //返回要显示的图片的总数 
  10.     public int getCount() { 
  11.         return imagesId.length; 
  12.     } 
  13.  
  14.     //获得相关的数据项中的指定位置的数据集。这里我们可以指定为该位置的Bitmap 
  15.     public Object getItem(int position) { 
  16.         Bitmap bitmap=BitmapFactory.decodeResource(context.getResources(), imagesId[position]); 
  17.         return bitmap; 
  18.     } 
  19.  
  20.     //返回相关位置的item的id,这里返回和position一样的ID 
  21.     public long getItemId(int position) { 
  22.         return position; 
  23.     } 
  24.  
  25.     /** 
  26.      * Get a View that displays the data at the specified position in the data set. 
  27.      * You can either create a View manually or inflate it from an XML layout file.  
  28.      * 得到一个在指定位置显示指定数据的视图,你可以手动的创建一个或者从XML布局文件中装载一个。 
  29.      * 参数:position  
  30.      * 参数:convertView 如果可以的话,旧的视图可以被重用,不过用之前要检测它是否为null 
  31.      * 参数:parent 这个视图最后要依附的父视图 www.2cto.com  
  32.      */ 
  33.     public View getView(int position, View convertView, ViewGroup parent) { 
  34.         ImageView imageView=new ImageView(context); 
  35.         Bitmap bitmap=BitmapFactory.decodeResource(context.getResources(), imagesId[position]); 
  36.         imageView.setImageBitmap(bitmap); 
  37.         return imageView; 
  38.     } 
  39.      
  40. } 

二、选择我们的Gallery,这里使用系统的Gallery,当然,为了某些特殊的效果,也可以选择自定义自己的Gallery

三、先在布局文件中定义我们的Gallery,然后在Activity中使用
    
    
  1. <Gallery  
  2.     android:id="@+id/gallery" 
  3.     android:spacing="100dp" 
  4.     android:layout_width="wrap_content" 
  5.     android:layout_height="wrap_content"/> 
  6. public class ImageScanActivity extends Activity { 
  7.     @Override 
  8.     public void onCreate(Bundle savedInstanceState) { 
  9.         super.onCreate(savedInstanceState); 
  10.         setContentView(R.layout.main); 
  11.         Gallery gallery=(Gallery)findViewById(R.id.gallery); 
  12.         GalleryAdapter adapter=new GalleryAdapter(this); 
  13.         gallery.setAdapter(adapter); 
  14.     } 
  15. } 
上面的就是一个最简单的Gallery的使用,当然,有很多不足的地方,例如滑动的时候卡,不流畅。或者一下划过几张图片等等,这些问题放到后面慢慢解决。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值