編輯:Android 技術基礎
看圖程式的縮放按鈕該怎麼做?ZoomControls 把「放大」與「縮小」兩個圓形按鈕打包成一個元件,你只要接上自己的縮放邏輯就能用。它的運作原理不複雜:以 Matrix 對原始 Bitmap 做比例變換,再把結果交回 ImageView 顯示。
先把 foot.jpg 複製到 res/drawable-hdpi。

接著開啟 activity_main.xml,上方放 ImageView,下方放 ZoomControls:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/foot" />
<ZoomControls
android:id="@+id/zoomcontrol"
android:layout_gravity="bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
縮放的關鍵數字是兩個「累乘比例」:scaleWidth 與 scaleHeight。每次放大就乘上 1.25,縮小就乘上 0.8,這樣連續點擊才不會每次都跳回原始尺寸。
public class MainActivity extends Activity {
private ZoomControls zoomcontrols;
private ImageView img;
private int displayWidth;
private int displayHeight;
private float scaleWidth = 1;
private float scaleHeight = 1;
private Bitmap bitmap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 取得螢幕解析度,實務上可用來限制最大縮放倍率
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
displayWidth = dm.widthPixels;
displayHeight = dm.heightPixels;
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.foot);
img = (ImageView) findViewById(R.id.image);
zoomcontrols = (ZoomControls) findViewById(R.id.zoomcontrol);
zoomcontrols.setIsZoomInEnabled(true);
zoomcontrols.setIsZoomOutEnabled(true);
// 放大
zoomcontrols.setOnZoomInClickListener(new OnClickListener() {
public void onClick(View v) {
int bmpWidth = bitmap.getWidth();
int bmpHeight = bitmap.getHeight();
double scale = 1.25;
scaleWidth = (float) (scaleWidth * scale);
scaleHeight = (float) (scaleHeight * scale);
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizeBmp =
Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight, matrix, true);
img.setImageBitmap(resizeBmp);
}
});
// 縮小
zoomcontrols.setOnZoomOutClickListener(new OnClickListener() {
public void onClick(View v) {
int bmpWidth = bitmap.getWidth();
int bmpHeight = bitmap.getHeight();
double scale = 0.8;
scaleWidth = (float) (scaleWidth * scale);
scaleHeight = (float) (scaleHeight * scale);
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizeBmp =
Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight, matrix, true);
img.setImageBitmap(resizeBmp);
}
});
}
}
要注意的是,Bitmap.createBitmap() 每次都會配置一張新的記憶體圖。當使用者連續縮放很多次,或圖片本身解析度很高時,容易觸發 OutOfMemoryError。正式專案裡通常會加上倍率上下限判斷,或改用 ImageView 的 setScaleType() 搭配 scaleX / scaleY 屬性來縮放,讓系統 reuse 同一張 Bitmap。
hide() / show():隱藏或顯示縮放按鈕。hasFocus():判斷是否取得焦點。onTouchEvent(MotionEvent event):處理觸控螢幕的移動事件。setIsZoomInEnabled(boolean) / setIsZoomOutEnabled(boolean):是否允許放大、縮小,可搭配倍率判斷動態停用按鈕。setOnZoomInClickListener() / setOnZoomOutClickListener():註冊放大、縮小的事件監聽。setZoomSpeed(long speed):設定縮放速度。反覆點擊「+」與「-」,圖片便會以等比例放大或縮小。


第26章 Android OnKeyListener 鍵盤事件:銀行卡號四位一組即時回顯
鍵盤事件最常被拿來做兩件事:即時驗證輸入格式(例如 email 是否合法),以及過濾不該出現的字元。本章做一個更貼近生活的例子——輸入銀行帳號,每四位自動用「-」分隔,並
從零開始學 Android:Bitmap 圖像處理入門
用 BitmapFactory 載入圖片 Android 顯示圖片的方式很多,最基礎的做法之一,是用 BitmapFactory.decodeResource 從資源中取
ImageView 圖像視圖:src 與 background、scaleType 全面解析
本節引言: 這一節介紹 UI 基礎控件 ImageView(圖像視圖),顧名思義,就是用來顯示圖像的 View。官方 API:ImageView。本節內容包括: src
Android 呼叫 Web Service:用 ksoap2 實作手機號碼歸屬地查詢(從零開始學 Android)
天氣預報、手機號碼歸屬地查詢、甚至與 SQL Server 這類遠端資料庫互動,都可以透過 Web Service 完成。它的價值在於:客戶端不必懂資料庫連線,只要按 S