[英]Resizing image taken from internet to display in android screens
我正在下載圖像,並希望在RelativeLayout中將其顯示為背景。 但是,由於android具有不同的屏幕,我發現很難根據屏幕尺寸來調整圖像大小。
這是我的布局
[Top Bar]
[Relative Layout] ..width= fill_parent , height = wrap_content
[ListView]
在調整大小時,我正在使用此代碼
bitmap = Utilities.decodeSampledBitmap(in, imageView.getWidth(), imageView.getWidth());
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
{
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth)
{
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmap(InputStream in, int reqWidth, int reqHeight)
{
if (in != null)
{
byte[] image;
try {
image = readFully(in);
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
BitmapFactory.decodeByteArray(image, 0, image.length, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeByteArray(image, 0, image.length, options);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
else
return null;
}
但是這些仍在拉伸圖像,僅在hdpi中看起來好一些,ldpi更差,mdpi很好,xhdpi也很差。 拉伸效果。 我無法固定RelativeLayout的大小或使用ImageView,因為它會顯示Image和ListView之間的空間。
我應該采用哪種方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.