你好,游客 登录 注册 搜索
背景:
阅读新闻

Android Gallery自定义风格

[日期:2012-06-21] 来源:Linux社区  作者:huweilong1030 [字体:大 中 小]
4、适配器代码如下:
  1. public class ImageAdapter extends BaseAdapter {  
  2.     int mGalleryItemBackGround;  
  3.     private Context mContext;  
  4.     private List<Drawable> list;  
  5.     public ImageAdapter(Context mContext,List<Drawable> li) {  
  6.         this.mContext = mContext;  
  7.         this.list = li;  
  8.         TypedArray a = mContext.obtainStyledAttributes(R.styleable.Gallery1);  
  9.         mGalleryItemBackGround = a.getResourceId(R.styleable.Gallery1_Android_galleryItemBackground, 0);  
  10.         a.recycle();  
  11.     }  
  12.   
  13.     public int getCount() {  
  14.         return list.size();  
  15.     }  
  16.   
  17.     public Object getItem(int position) {  
  18.         return position;  
  19.     }  
  20.   
  21.     public long getItemId(int position) {  
  22.         return position;  
  23.     }  
  24.   
  25.     public View getView(int position, View convertView, ViewGroup parent) {  
  26.         // 声明一个ImageView对象   
  27.         ImageView i = new ImageView(mContext);    
  28.         BitmapFactory.Options options = new BitmapFactory.Options();  
  29.         //设置图片的大小,宽高都为原来的二分之一,即图片为原来的四分之一    
  30.         options.inSampleSize = 2;   
  31.         i.setImageDrawable(list.get(position));  
  32.         //设置layout的宽高   
  33.         i.setLayoutParams(new Gallery.LayoutParams(200,200));//layout     
  34.         //重新设置图片的宽高   
  35.         i.setScaleType(ImageView.ScaleType.FIT_XY);//set scale type   
  36.         //设置图片的背景,即自定义的背景风格   
  37.         i.setBackgroundResource(mGalleryItemBackGround);  
  38.         return i;  
  39.     }  
  40.     class ViewHolder{  
  41.         ImageView image;  
  42.     }  
  43. }  
效果如下:
Android Gallery自定义风格
 
linux
相关资讯       Android开发入门 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款