我刚刚使用了这个代码表开发者网站,
public static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth,int reqHeight){
//Raw height & width of the image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height>reqHeight || width>reqWidth) {
//Calculate ratios of height & width to requested height & width.
final int heightRatio = Math.round((float)height / (float)reqHeight);
final int widthRatio = Math.round((float)width / (float)reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
public static 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);
//Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
//Decode Bitmap with inSampleSize Set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
通过使用此代码,我真的使用这行代码成功调用缩放的位图到我的ImageViews,
image.setImageBitmap(decodeSampledBitmapFromresource(getResources(), resId, width, height));
image.setImageBitmap(decodeSampledBitmapFromresource(getResources(), res, 640, 360));
这行代码实际上是将我的图像缩放到我想要的任何尺寸。
但我在这里遇到的问题是“内存异常。”
- 如何根据屏幕可用的比例缩放位图。
我使用以下设备根据设备的密度构建了我的代码:
int screenDensity = getResources().getDisplayMetrics().densityDpi;
这将返回屏幕密度,例如160,320,480,600,720,....
ImageView image = new ImageView(this);
if (screenDensity<=240) {
image.setImageBitmap(decodeSampledBitmapFromresource(getResources(), res, 320, 180));
} else if (screenDensity<=320) {
image.setImageBitmap(decodeSampledBitmapFromresource(getResources(), res, 430, 240));
} else if (screenDensity<=640) {
image.setImageBitmap(decodeSampledBitmapFromresource(getResources(), res, 640, 360));
} else {
image.setImageBitmap(decodeSampledBitmapFromresource(getResources(), res, 1280, 720));
}
background_image.addView(image);
我第一次在设备上运行时,我意识到我的轨道错误,对于一些Local Phablets,平板电脑,具有大屏幕的移动设备屏幕密度较低。
例如,
所以,在Xperia E类设备上,我的上述方程式正常运行。但对于具有高屏幕分辨率的设备,但在低屏幕密度设备上,所应用的图像实际上是完全无法识别的BLURRED或OUTSHAPED。
我也尝试使用因子屏幕宽度和高度。但是对于堆积较少的设备来说太致命了。
所以,问题是:
如何根据设备完美地扩展我的位图,无论密度和密度如何?决议。在这种情况下宽度和宽度Bitmpa的高度完全不为人知!
答案 0 :(得分:0)
我希望此代码对您有所帮助,请检查
public static Bitmap resizeImageWithOrignal(Bitmap orignal, int maxDimension) {
// load the origial BitMap
int width = orignal.getWidth();
int height = orignal.getHeight();
if (width > maxDimension || height > maxDimension) {
int new_width;
int new_height;
float ratio = (float) width / height;
if (ratio > 1.0f) {
new_width = maxDimension;
new_height = (int) ((float) new_width / ratio);
} else {
new_height = maxDimension;
new_width = (int) ((float) new_height * ratio);
}
float scaleWidth = ((float) new_width) / width;
float scaleHeight = ((float) new_height) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(orignal, 0, 0, width,
height, matrix, true);
return resizedBitmap;
}
return orignal;
}