Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

動手做新浪微博客戶端(七):大圖瀏覽與儲存功能

編輯:Android 開發實例

在閱讀微博的功能中,若一則微博含有圖片,正文下方便會顯示一張縮圖。但縮圖畢竟太小,難以看清細節,因此我們需要一個能放大、縮小與拖曳的圖片瀏覽器,讓使用者方便檢視,同時也能把圖片儲存到手機裡。由於這個頁面的 UI 並不複雜,就併在此篇一併說明。

首先建立 ImageActivity.java 作為圖片瀏覽的 Activity,並在 res/layout 下新增 image.xml 作為版面配置。配置內容並不複雜,直接看程式碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="https://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="vertical">
  <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="41px"
  android:background="@drawable/imagebar_bg">
  <RelativeLayout
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="2">
  <Button
  android:id="@+id/returnBtn"
  android:layout_width="63px"
  android:layout_height="29px"
  android:layout_centerInParent="true"
  android:text="返回"
  android:textColor="#ffffff"
  android:background="@drawable/btn1_bg">
  </Button>
  </RelativeLayout>
  <RelativeLayout
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="1">
  <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerInParent="true"
  android:text="瀏覽圖片"
  android:textColor="#ffffff">
  </TextView>
  </RelativeLayout>
  <RelativeLayout
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="2">
  <Button
  android:id="@+id/downBtn"
  android:layout_width="60px"
  android:layout_height="29px"
  android:layout_centerInParent="true"
  android:text="下載"
  android:textColor="#ffffff"
  android:background="@drawable/btn2_bg">
  </Button>
  </RelativeLayout>
  </LinearLayout>
  <RelativeLayout
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <MySinaWeiBo.ui.ImageZoomView
    xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/pic"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
  </MySinaWeiBo.ui.ImageZoomView>
  <ZoomControls
  android:id="@+id/zoomCtrl"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentRight="true"
  android:layout_alignParentBottom="true">
  </ZoomControls>
  </RelativeLayout>
</LinearLayout>

上述程式碼用到了一個自訂控制項 MySinaWeiBo.ui.ImageZoomView,它是整個功能的核心,負責實作圖片的放大、縮小與拖曳。這個控制項並非我原創,而是參考「Android one finger zoom tutorial」這篇教學寫成,因此這裡就不再貼出實作程式碼,有興趣的讀者可以點進原文研究。

接下來,就是用 ImageZoomView 來顯示圖片。在閱讀微博的頁面中,當使用者點擊正文裡的縮圖時,會把該縮圖對應的原圖 URL 傳給 ImageActivity;於是在 onCreate 中,我們就能依這個 URL 抓取圖片並設定給 ImageZoomView:

@Override
    public void onCreate(Bundle savedInstanceState) {
    ......
    Intent i=this.getIntent();
    if(i!=null){
        Bundle b=i.getExtras();
        if(b!=null){
            if(b.containsKey("url")){
                String url = b.getString("url");
                mZoomView=(ImageZoomView)findViewById(R.id.pic);
                Drawable img= AsyncImageLoader.loadImageFromUrl(url);
                image=drawableToBitmap(img);
                mZoomView.setImage(image);
                mZoomState = new ZoomState();
                mZoomView.setZoomState(mZoomState);
                mZoomListener = new SimpleZoomListener();
                mZoomListener.setZoomState(mZoomState);
                mZoomView.setOnTouchListener(mZoomListener);
                resetZoomState();
            }
        }
    }
......
}
private void resetZoomState() {
    mZoomState.setPanX(0.5f);
    mZoomState.setPanY(0.5f);
    final int mWidth = image.getWidth();
    final int vWidth= mZoomView.getWidth();
    mZoomState.setZoom(1f);
    mZoomState.notifyObservers();
}

public Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
    return bitmap;
}

接著處理 ZoomControls 的放大、縮小事件:

ZoomControls zoomCtrl = (ZoomControls) findViewById(R.id.zoomCtrl);
    zoomCtrl.setOnZoomInClickListener(new OnClickListener(){
        @Override
        public void onClick(View view) {
            float z= mZoomState.getZoom()+0.25f;
            mZoomState.setZoom(z);
            mZoomState.notifyObservers();
        }
    });
    zoomCtrl.setOnZoomOutClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            float z= mZoomState.getZoom()-0.25f;
            mZoomState.setZoom(z);
            mZoomState.notifyObservers();
        }
    });

至此,一個支援放大、縮小與拖曳的簡易圖片瀏覽器就完成了,基本符合應用需求。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved