顯示具有 Bitmap 標籤的文章。 顯示所有文章
顯示具有 Bitmap 標籤的文章。 顯示所有文章

2013年1月1日

[Android]製作縮圖

問題:
在使用Android的ImageView元件放入圖檔時,發現圖檔尺寸比ImageView元件的尺寸來的大很多。

解決:
使用Android製作縮圖,



/**
 原始圖檔 bitmap
*/

//取得圖檔寬度
int bmpWidth  = bitmap.getWidth(); 

//取得圖檔高度
int bmpHeight  = bitmap.getHeight(); 

//設定縮圖寬度
float scaleWidth  = (float) sWidth / bmpWidth;     
//按固定大小缩放sWidth,要多大有多大

//設定縮圖高度
float scaleHeight = (float) sHeight / bmpHeight;  

//轉換矩陣
Matrix matrix = new Matrix(); 
matrix.postScale(scaleWidth, scaleHeight); 

//產生縮圖
Bitmap resizeBitmap = 
Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight,matrix, false); 

//記得做資源回收,不然會發生溢位
bitmap.recycle(); 


參考網站:Android中图片缩放方法