今天一直在搞這個問題,在網上也搜索了很多的東西發現,沒有什麼用,最後發現可能能用的
代碼如下:
import android.graphics.Bitmap;
import android.graphics.Matrix;
public final class ZoomImage {
public static Bitmap getZoomImage(Bitmap src, int desW, int desH) {
Bitmap desImg = null;
int srcW = src.getWidth(); // 原始圖像寬
int srcH = src.getHeight(); // 原始圖像高
int[] srcBuf = new int[srcW * srcH]; // 原始圖片像素信息緩存
src.getPixels(srcBuf, 0, srcW, 0, 0, srcW, srcH);
// 計算插值表
int[] tabY = new int[desH];
int[] tabX = new int[desW];
int sb = 0;
int db = 0;
int tems = 0;
int temd = 0;
int distance = srcH > desH ? srcH : desH;
for (int i = 0; i <= distance; i++) { /* 垂直方向 */
tabY[db] = sb;
tems += srcH;
temd += desH;
if (tems > distance) {
tems -= distance;
sb++;
}
if (temd > distance) {
temd -= distance;
db++;
}
}
sb = 0;
db = 0;
tems = 0;
temd = 0;
distance = srcW > desW ? srcW : desW;
for (int i = 0; i <= distance; i++) { /* 水平方向 */
tabX[db] = (short) sb;
tems += srcW;
temd += desW;
if (tems > distance) {
tems -= distance;
sb++;
}
if (temd > distance) {
temd -= distance;
db++;
}
}
// 生成放大縮小後圖形像素
int[] desBuf = new int[desW * desH];
int dx = 0;
int dy = 0;
int sy = 0;
int oldy = -1;
for (int i = 0; i < desH; i++) {
if (oldy == tabY[i]) {
System.arraycopy(desBuf, dy - desW, desBuf, dy, desW);
} else {
dx = 0;
for (int j = 0; j < desW; j++) {
desBuf[dy + dx] = srcBuf[sy + tabX[j]];
dx++;
}
sy += (tabY[i] - oldy) * srcW;
}
oldy = tabY[i];
dy += desW;
}
// 生成圖片
desImg = Bitmap.createBitmap(desBuf, desW, desH, Bitmap.Config.ARGB_8888);
// Bitmap target_bit=Bitmap.createBitmap(desImg, 0, 0, desImg.getWidth(), desImg.getHeight(), rot, true);
return desImg;
}
}
但測試後,發現,這個文件,確實是能夠實現縮小,但可能是由於插邊做得不好,所以,裏面的文字效果特別地差,不能達到應該達到的效果,故棄之。
這裏解決了問題,特別要感謝繁星皓月 友友
http://home.51cto.com/index.php?s=/space/1723922,是他提供的一個API方法,來實現,Bitmap.createScaledBitmap(mBitmap,screen_width,screen_height,true);
這個API方法,真的好簡單,而且縮小之後,文字不會出現變形的狀態,效果非常好,值得稱讚。
具體辦法是:
1。InputStream得到圖片01
2。由圖片01創建圖片02
使用createBitmap(Bitmap source, int x, int y, int width, int height) 創建
3。輸出02
簡單代碼如下:Bitmap bitmap = getBitmap();
//
Bitmap bitmap02;
bitmap02 = Bitmap.createScaledBitmap(bitmap,20,20,true);
// 使用矩陣繪製圖片
canvas.drawBitmap(bitmap,0,0,paint);
canvas.drawBitmap(bitmap02, 130,130, paint); 這個是一個簡單的例子。
如有不明白,可以發問。