我正在使用尺寸很大的位图。在内存中加载位图时,它给了我 outofmemoryexception 有没有办法在不改变高度、宽度和质量的情况下减小大小。
user1901719
问问题
1056 次
4 回答
0
通过这些课程有效地显示位图:http: //developer.android.com/training/displaying-bitmaps/index.html
于 2012-12-18T12:11:19.413 回答
0
使用此代码减小您需要减少图像尺寸的图像大小
public static Bitmap getResizedBitmap(Bitmap image, int newHeight, int newWidth) {
int width = image.getWidth();
int height = image.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(image, 0, 0, width, height,
matrix, false);
return resizedBitmap;
}
于 2012-12-18T12:13:35.480 回答
0
以下代码将在运行时为您提供屏幕分辨率。
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
deviceHeightPix = displaymetrics.heightPixels;
deviceWidthPix = displaymetrics.widthPixels;
并且您可以根据需要在以下功能中传递这些高度和宽度或分辨率。此函数将返回从原始样本创建的样本大小。它不会降低任何质量。例如,您的屏幕尺寸为 480 * 800(普通和中等密度)而您的图像尺寸为 1500*2000,所以首先获取您的屏幕尺寸,传递给以下函数,它将返回 480*800 分辨率的图像而不减小其质量。
public Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
Log.d("Home", "Image Resizer -> ReqHieght : " + reqHeight);
Log.d("Home", "Image Resizer -> ReqWidth : " + reqWidth);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
Log.d("Home", "Image Resizer -> Samplesize:" + options.inSampleSize);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
要使用上述功能,
Bitmap bitmap = DecodeSampledBitmapFromResource(getResources(),R.drawable.home_screen,480,800);
使用此功能的好处是,您无需TWO
创建Bitmap
. 第一个对象保存decoded
图像,第二个对象使用第一个对象scale
。
于 2012-12-18T12:17:41.287 回答
0
/**
* decodes image and scales it to reduce memory consumption
*
* @param file
* @param requiredSize
* @return
*/
public static Bitmap decodeFile(File file, int requiredSize) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(file), null, o);
// The new size we want to scale to
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < requiredSize
|| height_tmp / 2 < requiredSize)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(file),
null, o2);
return bmp;
} catch (FileNotFoundException e) {
} finally {
}
return null;
}
于 2012-12-18T12:30:06.350 回答