Android 如何调整图像大小以适应多个屏幕密度
我需要添加一个化身到网格项目 我想知道如何处理从手机图库中选择的图像的大小调整。一旦被选中,我想需要进行一些调整,以适应网格 但是,我是否需要为每个屏幕密度存储一个调整大小的图像;存储一个xhdpi版本并为其他设备缩小规模,还是以其他方式变得更聪明Android 如何调整图像大小以适应多个屏幕密度,android,android-image,android-screen-support,Android,Android Image,Android Screen Support,我需要添加一个化身到网格项目 我想知道如何处理从手机图库中选择的图像的大小调整。一旦被选中,我想需要进行一些调整,以适应网格 但是,我是否需要为每个屏幕密度存储一个调整大小的图像;存储一个xhdpi版本并为其他设备缩小规模,还是以其他方式变得更聪明 原因是,该应用程序将此图像存储到云数据库中,其他人可以下载此图像。他们可能在不同的设备上看到图像(因此需要不同的图像大小)。如何处理此图像的管理?我希望下面的代码对您有用。它将以最小的开销返回具有所需尺寸的图像。我已经用过很多次了,效果很好。您可以根
原因是,该应用程序将此图像存储到云数据库中,其他人可以下载此图像。他们可能在不同的设备上看到图像(因此需要不同的图像大小)。如何处理此图像的管理?我希望下面的代码对您有用。它将以最小的开销返回具有所需尺寸的图像。我已经用过很多次了,效果很好。您可以根据目标设备设置所需的尺寸。缩放会导致图片模糊,但事实并非如此
private Bitmap getBitmap(Uri uri) {
InputStream in = null;
try {
final int IMAGE_MAX_SIZE = 200000; // 0.2MP
in = my_context.getContentResolver().openInputStream(uri);
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true; //request only the dimesion
BitmapFactory.decodeStream(in, null, o);
in.close();
int scale = 1;
while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 2)) > IMAGE_MAX_SIZE) {
scale++;
}
Bitmap b = null;
in = my_context.getContentResolver().openInputStream(uri);
if (scale > 1) {
scale--;
// scale to max possible inSampleSize that still yields an image
// larger than target
o = new BitmapFactory.Options();
o.inSampleSize = scale;
b = BitmapFactory.decodeStream(in, null, o);
// resize to desired dimensions
int height = b.getHeight();
int width = b.getWidth();
double y = Math.sqrt(IMAGE_MAX_SIZE
/ (((double) width) / height));
double x = (y / height) * width;
Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x, (int) y, true);
b.recycle();
b = scaledBitmap;
System.gc();
} else {
b = BitmapFactory.decodeStream(in);
}
in.close();
return b;
} catch (IOException e) {
return null;
}
}
将缩放图像并将其居中调整为要填充父对象的容器的大小,并且应该这样做。您可以将图像置于可绘制状态(无需创建xhdpi、hdpi、mdpi、ldpi…)(全局图像) 然后,您可以为不同的屏幕大小创建4个相同的布局。所有布局都可以使用drawable director中的图像。因此,您可以轻松调整图像大小 这样做:
我不打算用预先制作好的图像填充res文件夹。用户从图库中选择一幅图像(就像自己的照片),但逻辑是一样的。用户选择的图片可以在您创建的不同布局中调整大小。您可以使用android:scaleType=“fitXY”将图像放在该图像上。android:scaleType=“fitXY:如果您使用standart imageview size,它可能会很有用。但是不同的图像视图大小??您的图像比例将被分解。您可以通过裁剪图像来解决此问题。android:scaleType=“centerCrop“您将在设备上的何处存储这些图像?你可以看看这个天秤座,它很有用,很有趣,谢谢。看起来我可以保存我所需要的最大尺寸的图像,并用它来管理它是如何适合的。谢谢。我将把它视为一个调整大小的选项。
android:scaleType="fitXY"
android:layout_gravity="center"
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Bitmap bitmapOrg = new BitmapDrawable(getResources(), new ByteArrayInputStream(imageThumbnail)).getBitmap();
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
float scaleWidth = metrics.scaledDensity;
float scaleHeight = metrics.scaledDensity;
// 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(bitmapOrg, 0, 0, width, height, matrix, true);