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

Android图片浏览器:在对话框上实现图片浏览

[日期:2011-10-23] 来源:Linux社区  作者:badboy1110 [字体:大 中 小]

加载图片的方法:

  1. public void loadImage() {  
  2.     if(imageChooseView == null) {  
  3.         LayoutInflater li = LayoutInflater.from(AddNew.this);  
  4.         imageChooseView = li.inflate(R.layout.imageswitch, null);  
  5.           
  6.         //通过渲染xml文件,得到一个视图(View),再拿到这个View里面的Gallery   
  7.         gallery = (Gallery)imageChooseView.findViewById(R.id.gallery);  
  8.         //为Gallery装载图片   
  9.         gallery.setAdapter(new ImageAdapter(this));  
  10.         gallery.setSelection(images.length/2);  
  11.         is = (ImageSwitcher)imageChooseView.findViewById(R.id.imageswitch);  
  12.         is.setFactory(this);  
  13.         is.setInAnimation(AnimationUtils.loadAnimation(this, Android.R.anim.fade_in));  
  14.         //卸载图片的动画效果   
  15.         is.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));  
  16.         gallery.setOnItemSelectedListener(new OnItemSelectedListener(){  
  17.   
  18.             @Override  
  19.             public void onItemSelected(AdapterView<?> arg0, View arg1,  
  20.                     int arg2, long arg3) {  
  21.                 //当前的头像位置为选中的位置   
  22.                 currentImagePosition = arg2;  
  23.                 //为ImageSwitcher设置图像   
  24.                 is.setImageResource(images[arg2 % images.length]);  
  25.                   
  26.             }  
  27.             @Override  
  28.             public void onNothingSelected(AdapterView<?> arg0) {  
  29.                   
  30.             }});  
  31.     }  
  32.       
  33. }  

重点,adapter的写法:

  1. class ImageAdapter extends BaseAdapter {  
  2.   
  3.         private Context context;  
  4.           
  5.         public ImageAdapter(Context context) {  
  6.             this.context = context;  
  7.         }  
  8.           
  9.         @Override  
  10.         public int getCount() {  
  11.             return Integer.MAX_VALUE;  
  12.         }  
  13.   
  14.         @Override  
  15.         public Object getItem(int position) {  
  16.             return position;  
  17.         }  
  18.   
  19.         @Override  
  20.         public long getItemId(int position) {  
  21.             return position;  
  22.         }  
  23.   
  24.           
  25.         /** 
  26.          * gallery从这个方法中拿到image 
  27.          */  
  28.         @Override  
  29.         public View getView(int position, View convertView, ViewGroup parent) {  
  30.             ImageView iv = new ImageView(context);  
  31.             iv.setImageResource(images[position%images.length]);  
  32.             iv.setAdjustViewBounds(true);  
  33.             iv.setLayoutParams(new Gallery.LayoutParams(80,80));  
  34.             iv.setPadding(15, 10, 15, 10);  
  35.             return iv;  
  36.               
  37.         }  
  38.           
  39.     }  
  40.   
  41.       
 
  1. //这个是重写的的方法,我给加上注释。我是百度了很久也没找到这个方法的解释。只要对着有道看的源码~~  TAT   
  2.       
  3.     @Override  
  4.     public View makeView() {  
  5.         ImageView view = new ImageView(this);  
  6.         view.setBackgroundColor(0xff000000);//设置背景边框颜色?   
  7.         view.setScaleType(ScaleType.FIT_CENTER);//设置 显示的类型  居中显示   
  8.         view.setLayoutParams(new ImageSwitcher.LayoutParams(90,90));//设置显示的图片大小   
  9.         return view;  
  10.     }  
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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