第一种方法 /** * 当图片超过手机屏幕尺寸 * 宽度和高度进行一半缩放 * @param res * @param id * @param opts
第一种方法
/** * 当图片超过手机屏幕尺寸 * 宽度和高度进行一半缩放 * @param res * @param id * @param opts * @return */ public Bitmap DecodeFileBitmap(Resources res, int id, Options opts){ BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeResource(res, id, o); final int REQUIRED_HEIGHT = screenheight; //800 final int REQUIRED_WIDTH = screenwidth; //480 int width_tmp = o.outWidth,height_tmp = o.outHeight; System.out.println("source"+"weight"+width_tmp + " " + "source height"+height_tmp); int scale = 1; while(true){ if (width_tmp / 2 < REQUIRED_WIDTH && height_tmp / 2 < REQUIRED_HEIGHT) break; width_tmp /= 2; height_tmp /= 2; scale++; Log.w("===", scale + "''" + width_tmp + " " + height_tmp); } BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; return BitmapFactory.decodeResource(res,id,o2); }
第二种方法:
/** * 此根据输入的宽和高来定制缩放比例 * @param res * @param id * @return */ public Bitmap DecodeFileBitmap(Resources res, int id){ BitmapFactory.Options op = new BitmapFactory.Options(); op.inJustDecodeBounds = true; Bitmap bmp = BitmapFactory.decodeResource(res, id, op); int wRatio = (int)Math.ceil(op.outWidth/200); int hRatio = (int)Math.ceil(op.outHeight/200); System.out.println("wRatio"+wRatio +"/n"+"hRatio"+hRatio); if(wRatio > 1 && hRatio > 1){ if(wRatio > hRatio){ op.inSampleSize = wRatio; }else{ op.inSampleSize = hRatio; } } op.inJustDecodeBounds = false; bmp = BitmapFactory.decodeResource(res, id, op); System.out.println("BMP Width"+bmp.getWidth()+"/n"+"BMP Height"+bmp.getHeight()); return bmp; }
第三种:Android自带工具类
/** * 利用android工具类来对图片以中心点进行缩放 *(此缩放会产生局部周边景物损失,起保证中心点图片完整) * @param res * @param id * @return BitMap图片 */ public Bitmap decodebitmapunit(Resources res, int id){ Bitmap source = BitmapFactory.decodeResource(res, id);// source = ThumbnailUtils.extractThumbnail(source, 200, 200, 0|ThumbnailUtils.OPTIONS_RECYCLE_INPUT);// source = ThumbnailUtils.extractThumbnail(source, 50, 50); source = ImageZoom.extractThumbnail(source, 200,200); return source; }
工具类:
package com.play.image;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Matrix;import android.graphics.Rect;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; }}
ThumbnailUtils.java 源码:
. http://www.oschina.net/code/explore/android-2.2-froyo/android/media/ThumbnailUtils.java .
所以本人把2.2版本的SDK的android.media.*包下的类ThumbnailUtils.java、DecoderCapabilities.java、ExifInterface.java、MediaFile.java、MediaMetadataRetriever.java五个类源码放到我的项目里,使用 ThumbnailUtils.createVideoThumbnail(imageUrl, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);方法在Google Nexus S手机上能获取到网络视频缩略图(不管是别的手机拍的还是自己的手机拍的都可以),而使用索爱X8 e151、MOTO ME811和三星I9100都不能获取网络视频缩略图,这是为什么呢?求帮助。