- //根据指定的bitmap路径加载,并定义好了目标的大小
- public static Bitmap decodeFile(String path, int dstWidth, int dstHeight) {
- Options options = new Options();
- options.inJustDecodeBounds = true;
- BitmapFactory.decodeFile(path, options);
- options.inJustDecodeBounds = false;
- options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, dstWidth,
- dstHeight);
- Bitmap unscaledBitmap = BitmapFactory.decodeFile(path, options);
- return unscaledBitmap;
- }
- /**
- * 获得经过处理的bitmap
- */
- public static Bitmap createScaledBitmap(Bitmap unscaledBitmap, int dstWidth, int dstHeight) {
- Rect srcRect = calculateSrcRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(),
- dstWidth, dstHeight);
- Rect dstRect = calculateDstRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(),
- dstWidth, dstHeight);
- Bitmap scaledBitmap = Bitmap.createBitmap(dstRect.width(), dstRect.height(),
- Config.ARGB_8888);
- Canvas canvas = new Canvas(scaledBitmap);
- canvas.drawBitmap(unscaledBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));
- return scaledBitmap;
- }
- /**
- * 计算Option的inSampleSize属性
- */
- public static int calculateSampleSize(int srcWidth, int srcHeight, int dstWidth, int dstHeight) {
- final float srcAspect = (float) srcWidth / (float) srcHeight;
- final float dstAspect = (float) dstWidth / (float) dstHeight;
- if (srcAspect > dstAspect) {
- return srcWidth / dstWidth;
- } else {
- return srcHeight / dstHeight;
- }
- }
- /**
- * 计算源文件的Rect
- */
- public static Rect calculateSrcRect(int srcWidth, int srcHeight, int dstWidth, int dstHeight) {
- return new Rect(0, 0, srcWidth, srcHeight);
- }
- /**
- * 计算目标bitmap的rect区域
- */
- public static Rect calculateDstRect(int srcWidth, int srcHeight, int dstWidth, int dstHeight) {
- final float srcAspect = (float) srcWidth / (float) srcHeight;
- final float dstAspect = (float) dstWidth / (float) dstHeight;
- if (srcAspect > dstAspect) {
- return new Rect(0, 0, dstWidth, (int) (dstWidth / srcAspect));
- } else {
- return new Rect(0, 0, (int) (dstHeight * srcAspect), dstHeight);
- }
- }
- /**
- * View转化为BitMap
- */
- public static Bitmap convertViewToBitmap(View view) {
- view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
- MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
- view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
- view.buildDrawingCache();
- Bitmap bitmap = view.getDrawingCache();
- return bitmap;
- }
- //该片段来自于http://www.codesnippet.cn/detail/0608201513354.html
来源: http://www.codesnippet.cn/detail/0608201513354.html