0

我正在下载一张图片,我想在 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 之间显示空间。

我应该采用什么方法。

4

1 回答 1

0

我建议你不要使用背景图片,而是使用ImageView并设置他的ScaleType以满足您的需求。

于 2013-05-12T07:47:04.693 回答