android 固定大小,android 固定大小取图片缩略图
在做一個項目的時候有個要求就是固定大小取圖像的縮略圖,android2.2以前的話都是通過BitmapFactory.Options 設(shè)置Options通過一定的寬高比例來得到一個縮略圖,但是得到的并不是要求的固定大小的縮略圖,而是寬和高按一定比例縮放后的,android2.2以后提供了一個新的類ThumbnailUtils類可以直接獲取,但是考慮到2.2以版本的兼容性ThumbnailUtils雖然很好但是不適用,通過對ThumbnailUtils類的解析,項目中抽出了一個新的類把ThumbnailUtils類中與圖片不相關(guān)的內(nèi)容剔除出來,這樣就可以兼容所有的版本:
上代碼:
private static class ImageZoom {
private static final int OPTIONS_SCALE_UP = 0x1;
public static final int OPTIONS_RECYCLE_INPUT = 0x2;
private static final int OPTIONS_NONE = 0x0;
public static ?Bitmap ?extractThumbnail(Bitmap source, int width, int height)
{
return extractThumbnail(source,width,height,OPTIONS_NONE);
}
private static Bitmap ?extractThumbnail(
Bitmap source, int width, int height, int options) {
if (source == null) {
return null;
}
float scale;
if (source.getWidth() < source.getHeight()) {
scale = width / (float) source.getWidth();
} else {
scale = height / (float) source.getHeight();
}
Matrix matrix = new Matrix();
matrix.setScale(scale, scale);
Bitmap thumbnail = transform(matrix, source, width, height,
OPTIONS_SCALE_UP | options);
return thumbnail;
}
private static Bitmap transform(Matrix scaler,
Bitmap source,
int targetWidth,
int targetHeight,
int options) {
boolean scaleUp = (options & OPTIONS_SCALE_UP) != 0;
boolean recycle = (options & OPTIONS_RECYCLE_INPUT) != 0;
int deltaX = source.getWidth() - targetWidth;
int deltaY = source.getHeight() - targetHeight;
if (!scaleUp && (deltaX < 0 || deltaY < 0)) {
Bitmap b2 = Bitmap.createBitmap(targetWidth, targetHeight,
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b2);
int deltaXHalf = Math.max(0, deltaX / 2);
int deltaYHalf = Math.max(0, deltaY / 2);
Rect src = new Rect(
deltaXHalf,
deltaYHalf,
deltaXHalf + Math.min(targetWidth, source.getWidth()),
deltaYHalf + Math.min(targetHeight, source.getHeight()));
int dstX = (targetWidth ?- src.width()) ?/ 2;
int dstY = (targetHeight - src.height()) / 2;
Rect dst = new Rect(
dstX,
dstY,
targetWidth - dstX,
targetHeight - dstY);
c.drawBitmap(source, src, dst, null);
if (recycle) {
source.recycle();
}
return b2;
}
float bitmapWidthF = source.getWidth();
float bitmapHeightF = source.getHeight();
float bitmapAspect = bitmapWidthF / bitmapHeightF;
float viewAspect ? = (float) targetWidth / targetHeight;
if (bitmapAspect > viewAspect) {
float scale = targetHeight / bitmapHeightF;
if (scale < .9F || scale > 1F) {
scaler.setScale(scale, scale);
} else {
scaler = null;
}
} else {
float scale = targetWidth / bitmapWidthF;
if (scale < .9F || scale > 1F) {
scaler.setScale(scale, scale);
} else {
scaler = null;
}
}
Bitmap b1;
if (scaler != null) {
b1 = Bitmap.createBitmap(source, 0, 0,
source.getWidth(), source.getHeight(), scaler, true);
} else {
b1 = source;
}
if (recycle && b1 != source) {
source.recycle();
}
int dx1 = Math.max(0, b1.getWidth() - targetWidth);
int dy1 = Math.max(0, b1.getHeight() - targetHeight);
Bitmap b2 = Bitmap.createBitmap(
b1,
dx1 / 2,
dy1 / 2,
targetWidth,
targetHeight);
if (b2 != b1) {
if (recycle || b1 != source) {
b1.recycle();
}
}
return b2;
}
}
總結(jié)
以上是生活随笔為你收集整理的android 固定大小,android 固定大小取图片缩略图的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 单位转换工具,Andro
- 下一篇: android判断参数非空,Androi