Chinaunix首页 | 论坛 | 博客
  • 博客访问: 51910
  • 博文数量: 18
  • 博客积分: 454
  • 博客等级: 下士
  • 技术积分: 165
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-08 22:40
文章分类
文章存档

2012年(18)

最近访客

分类: 嵌入式

2012-01-08 23:17:30

2.png
     在阅读微博的功能篇中,如果微博包含了图片就会在微博正文下面显示该张图片,但是这个图片只是张缩略图,这样就需要提供一个能放大缩小查看这张图片的功能,当点击正文中的缩略图的时候显示一个简单的图片浏览器功能,提供图片的放大、缩小、拖拽操作方便用户查看图片,同时也提供保存图片到手机的功能。本功能的UI比较简单就不单独分篇讲了,具体的实现效果如上图。

      新建ImageActivity.java作为图片浏览Activity,在res/layout下新建image.xml的Layout作为图片浏览的布局文件,image.xml布局代码很简单了就不详细解释了直接贴代码:


  1.   xmlns:=""
  2.   android:layout_width="wrap_content"
  3.   android:layout_height="wrap_content"
  4.   android:orientation="vertical">
  5.   
  6.   android:layout_width="fill_parent"
  7.   android:layout_height="41px"
  8.   android:background="@drawable/imagebar_bg">
  9.   
  10.   
  11.   android:layout_width="fill_parent"
  12.   android:layout_height="fill_parent"
  13.   android:layout_weight="2">
  14.   
  15.   android:id="@+id/returnBtn"
  16.   android:layout_width="63px"
  17.   android:layout_height="29px"
  18.   android:layout_centerInParent="true"
  19.   android:text="返回"
  20.   android:textColor="#ffffff"
  21.   android:background="@drawable/btn1_bg">
  22.   
  23.   
  24.   
  25.   
  26.   android:layout_width="fill_parent"
  27.   android:layout_height="fill_parent"
  28.   android:layout_weight="1">
  29.   
  30.   android:layout_width="wrap_content"
  31.   android:layout_height="wrap_content"
  32.   android:layout_centerInParent="true"
  33.   android:text="浏览图片"
  34.   android:textColor="#ffffff">
  35.   
  36.   
  37.   
  38.   
  39.   android:layout_width="fill_parent"
  40.   android:layout_height="fill_parent"
  41.   android:layout_weight="2">
  42.   
  43.   android:id="@+id/downBtn"
  44.   android:layout_width="60px"
  45.   android:layout_height="29px"
  46.   android:layout_centerInParent="true"
  47.   android:text="下载"
  48.   android:textColor="#ffffff"
  49.   android:background="@drawable/btn2_bg">
  50.   
  51.   
  52.   
  53.   
  54.   
  55.   android:layout_width="fill_parent"
  56.   android:layout_height="fill_parent">

  57.   
  58.     xmlns:android=""
  59.     android:id="@+id/pic"
  60.     android:layout_width="fill_parent"
  61.     android:layout_height="fill_parent">
  62.   
  63.   
  64.   
  65.   android:id="@+id/zoomCtrl"
  66.   android:layout_width="wrap_content"
  67.   android:layout_height="wrap_content"
  68.   android:layout_alignParentRight="true"
  69.   android:layout_alignParentBottom="true">
  70.   
  71.   
复制代码
上面的代码中用到了一个自定义控件MySinaWeiBo.ui.ImageZoomView,这个就是整个功能的核心部分,用来实现图片的放大、缩小、拖拽的一个图片显示控件,这个控件非我原创,是参考了Android one finger zoom tutorial 这篇博客写出来的,所以我在这里也不贴实现代码了,有兴趣的大家可以直接看看这个文章。      接下要做的就是用这个ImageZoomView来显示图片,在阅读微博内容的页面中当点击内容中的缩略图片的时候会把这个缩略图对应的原图的url传给当前的这个ImageActivity,那么在ImageActivity的onCreate方法中根据这个url获取图片并且设置给 ImageZoomView。在onCreate方法中代码如下:
  1. @Override
  2.     public void onCreate(Bundle savedInstanceState) {
  3.         。。。。。
  4.         Intent i=this.getIntent();
  5.         if(i!=null){
  6.             Bundle b=i.getExtras();
  7.             if(b!=null){
  8.                 if(b.containsKey("url")){
  9.                     String url = b.getString("url");
  10.                     mZoomView=(ImageZoomView)findViewById(R.id.pic);
  11.                     Drawable img= AsyncImageLoader.loadImageFromUrl(url);
  12.                     
  13.                
  14.                     image=drawableToBitmap(img);
  15.                     mZoomView.setImage(image);
  16.                     
  17.                     mZoomState = new ZoomState();
  18.                     mZoomView.setZoomState(mZoomState);
  19.                     mZoomListener = new SimpleZoomListener();
  20.                     mZoomListener.setZoomState(mZoomState);
  21.                     
  22.                     mZoomView.setOnTouchListener(mZoomListener);
  23.                     resetZoomState();
  24.                 }
  25.             }
  26.         }
  27. 。。。。。。
  28. }
复制代码
  1. private void resetZoomState() {
  2.         mZoomState.setPanX(0.5f);
  3.         mZoomState.setPanY(0.5f);
  4.         
  5.         final int mWidth = image.getWidth();
  6.         final int vWidth= mZoomView.getWidth();
  7.         Log.e("iw:",vWidth+"");
  8.         mZoomState.setZoom(1f);
  9.         mZoomState.notifyObservers();
  10.         
  11.     }
  12.    
  13.     public Bitmap drawableToBitmap(Drawable drawable) {
  14.         Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
  15.         return bitmap;
  16.     }
复制代码
接下来就是ZoomControls的放大缩小事件代码:
  1. ZoomControls zoomCtrl = (ZoomControls) findViewById(R.id.zoomCtrl);
  2.         zoomCtrl.setOnZoomInClickListener(new OnClickListener(){
  3.             @Override
  4.             public void onClick(View view) {
  5.                 float z= mZoomState.getZoom()+0.25f;
  6.                 mZoomState.setZoom(z);
  7.                 mZoomState.notifyObservers();
  8.             }
  9.             
  10.         });
  11.         zoomCtrl.setOnZoomOutClickListener(new OnClickListener(){

  12.             @Override
  13.             public void onClick(View v) {
  14.                 float z= mZoomState.getZoom()-0.25f;
  15.                 mZoomState.setZoom(z);
  16.                 mZoomState.notifyObservers();
  17.             }
  18.             
  19.         });
复制代码
这样一个简单的图片浏览器功能就完成了,支持放大缩小并且还能拖拽,基本上达到应用需求。
阅读(1394) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~