編輯: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();
}
});
至此,一個支援放大、縮小與拖曳的簡易圖片瀏覽器就完成了,基本符合應用需求。
ListView 中讓 Button 始終固定在底部的範例
在 Android 中實作底部佈局,往往會使用 RelativeLayout 並設定 android:layout_alignParentBottom=true,這樣很容
Android 進度條教學:Spinner 的宣告、切換與常用 API
使用者按下按鈕之後,如果畫面兩三秒沒反應,多半會以為 App 當機了。所以任何耗時的背後工作,都該配一個進度提示。Android 的 ProgressBar 有兩種常見外
Android 登入頁面設計:帳密輸入、錯誤提示與嘗試次數控制
登入頁是使用者對一支 App 的第一印象,也是安全防線的第一關。它要做的事看似簡單——收帳號、收密碼、比對——但細節決定成敗:密碼不能被旁人看光、錯誤要講得清楚、盲猜的次
Android 本地化:讓應用程式適配不同語言環境
Android 應用程式可以在許多不同地區的許多裝置上執行。為了讓應用程式更具互動性,它應該能處理文字、數字、檔案等內容,使其適配應用程式即將使用的語言環境。 本章將說明