編輯:關於 Android 開發
本文示範如何製作一個圖片檢視程式,透過 Matrix 矩陣縮放,並運用實體音量鍵來放大與縮小圖片,同時示範 ScrollView 讓放大後的圖片可以捲動瀏覽。
package snowfox.android;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView v1;
private int widthOrg, heightOrg, w;
float scale = 1;
float scaleWidth, scaleHeight;
Bitmap OrgMap, newMap;
BitmapDrawable bmd;
Matrix matrix = new Matrix();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
v1 = (ImageView)findViewById(R.id.image01);
OrgMap = BitmapFactory.decodeResource(getResources(), R.drawable.test);
widthOrg = OrgMap.getWidth();
heightOrg = OrgMap.getHeight();
// 確定圖片初始縮放比例,一般以適應螢幕寬度為準
w = 320;
scale = (float)w/widthOrg;
matrix = new Matrix();
matrix.postScale(scale, scale);
newMap = Bitmap.createBitmap(OrgMap, 0, 0, widthOrg, heightOrg, matrix, true);
bmd = new BitmapDrawable(newMap);
v1.setImageDrawable(bmd);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// 放大圖片
if(keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
// 若大於 2 倍則停止放大,小於 1200 是為了防止圖片過大造成記憶體洩漏
if(scale < 2.0 && w < 1200) {
scale += 0.1;
matrix.reset(); // 重置矩陣
matrix.postScale(scale, scale); // 設定矩陣屬性
newMap = Bitmap.createBitmap(OrgMap, 0, 0, widthOrg, heightOrg, matrix, true);
bmd = new BitmapDrawable(newMap);
v1.setImageDrawable(bmd);
w = newMap.getWidth();
int h = newMap.getHeight();
Log.i("ta==========", w + " " + h);
}
return true;
}
// 縮小圖片
if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
// 若小於 0.1 倍則停止縮小
if(scale > 0.1) {
scale -= 0.05;
matrix.reset();
matrix.postScale(scale, scale);
newMap = Bitmap.createBitmap(OrgMap, 0, 0, widthOrg, heightOrg, matrix, true);
bmd = new BitmapDrawable(newMap);
v1.setImageDrawable(bmd);
w = newMap.getWidth();
int h = newMap.getHeight();
Log.i("ta==========", w + " " + h);
}
return true;
}
else
// 其他情況才呼叫 super,以屏蔽音量鍵原功能,同時保留其他按鍵功能
return super.onKeyDown(keyCode, event);
}
}
使用 ScrollView 搭配 HorizontalScrollView,讓放大後的圖片可以雙向捲動:
<ScrollView xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:fillViewport="true">
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<LinearLayout
android:gravity="center"
android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageView
android:id="@+id/image01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"/>
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
透過 Matrix 的 postScale 進行縮放,並以 createBitmap 重新繪製,即可實作流暢的圖片縮放體驗;同時以 w < 1200 的條件限制,避免圖片過大造成記憶體問題。
Android 圖片合成實作:Canvas 疊圖與 Xfermode 混合模式
用 Canvas 把兩張圖疊成一張 本文示範如何在 Android 中原生完成圖片合成。核心思路只有一句話:先建立一張「可寫入」的 Bitmap 當作畫布,再用 Canv
《Android源碼設計模式解析與實戰》讀書筆記(十九):組合模式
讀書筆記(十九):組合模式 第十九章、組合模式 組合模式又稱「部分—整體」模式,是結構型設計模式中的一員,擅長處理具有層次結構的對象集合。 1. 定義 組合模式將對象組合
Android 執行緒詳解:主執行緒、Handler、Message Queue 與 AsyncTask
Android 執行緒詳解:主執行緒、Handler、Message Queue 與 AsyncTask 執行緒本質上是一串連續指令。Android App 啟動後,會有
Android 布局管理器入門:LinearLayout、RelativeLayout、GridLayout、FrameLayout 與 AbsoluteLayout
Android 布局管理器入門:LinearLayout、RelativeLayout、GridLayout、FrameLayout 與 AbsoluteLayout A