简体   繁体   中英

Android ImageView not scaling to screen width

I am using following function to scale any image to screen width:

DisplayMetrics metrics = new DisplayMetrics();
((Activity)context)
.getWindowManager()
.getDefaultDisplay()
.getMetrics(metrics);

int width = bitmap.getWidth();
int height = bitmap.getHeight();

// Calculate the ratio between height and width of Original Image
float ratio = (float) height / (float) width;

int newWidth = metrics.widthPixels; // This will be equal to screen width
float newHeight = newWidth * ratio; // This will be according to the ratio calulated

// calculate the scale
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = 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(
                bitmap, 0, 0,
                width, height,
                matrix, true
);

// make a Drawable from Bitmap to allow to set the BitMap
// to the ImageView, ImageButton or what ever
return new BitmapDrawable(resizedBitmap);

After putting some checks, the values are, metrics.widthPixels=540 and the width of new Bitmap is also 540. That means Bitmap or Imageview should use full screen width. Instead, the resulting imageviews are short of full screen width. I am including a screenshot:

屏幕截图

As you can see in the image, the remaining blank part of the screen is shown in Black Color.

The code for creating ImageView is:

ImageView imageBanner = new ImageView(context);
imageBanner.setLayoutParams(new
    LinearLayout.LayoutParams(
        Globals.wrapContent,
        Globals.wrapContent));
imageBanner.setBackgroundResource(R.drawable.imv_banner);
new SyncImage(context, imageBanner, urlImage).execute();

Globals.wrapContent are the explicit constants which contain same values for standard Layout Params, so don't think them as different. SyncImage the Async Class used to download and show image in ImageView.

Please provide a solution to scale the image to full screen width and image should retain its original dimension ratio.

Thank You Ram Ram

Set Width of imageView to MatchParent instead of WrapContent and calculate height to maintain the aspect ratio of the image. Try following code to resize the image:

    Display display = getActivity().getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int newWidth = size.x;

    //Get actual width and height of image
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    // Calculate the ratio between height and width of Original Image
    float ratio = (float) height / (float) width;
    float scale = getApplicationContext().getResources().getDisplayMetrics().density;
    int newHeight = (int) (width * ratio)/scale;

    return Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM