0

I am rather confused.

I have an App which has a matrix of nine imagebuttons 3 x 3

The centre button has a logo which is loaded from a resource ID R.drawable.logo and the others are initialised to "no image yet" images. All images are 86 x 86 and set from the screen builder tool.

When the user logs in, one or more of the surrounding 8 images will be downloaded from the web. They are mainly 86x86 as well.

The downloaded images come up appx half size in x and y directions compared to the pre-set images.

Images are examined and known to be 86 x 86 once downloaded by examining bitmapFactory options object.

Images downloaded by two different methods in this page... How to load an ImageView by URL in Android?

Community
  • 1
  • 1
Dave Appleton
  • 465
  • 1
  • 6
  • 18
  • what is your question? I'm assuming you want them to be 86x86. You have to change the scaleType attribute on your ImageView for the images to scale in a desirable way for you – dymmeh Mar 05 '12 at 17:09
  • The question is WHY images set in the layout file appear a different size from images downloaded and set using either setImageBitmap or setImageDrawable and how I can ensure that all images are rendered to the same size more or less – Dave Appleton Mar 05 '12 at 17:13

2 Answers2

3

An image pulled from resources will get up-scaled if one cannot be found for the target device's density dpi. For example, if you are on a device that is DisplayMetrics.DENSITY_HIGH (hdpi) but you only have the image in /res/drawable-mdpi that image will be automatically up-scaled when you retrieve it via something like getDrawable().

However, with a downloaded image the system does not know which density the image is designed for because it is not contained in a resource folder which specifies its density, so scaling cannot be done automatically. You must define the density manually using BitmapFactory.Options. Consider the following function:

/**
 * Downloads an image for a specified density DPI.
 * @param context the current application context
 * @param url the url of the image to download
 * @param imgDensity the density DPI the image is designed for (DisplayMetrics.DENSITY_MEDIUM, DisplayMetrics.DENSITY_HIGH, etc.)
 * @return the downloaded image as a Bitmap
 */
public static Bitmap loadBitmap(Context context, String url, int imgDensity) {
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    BitmapFactory.Options options = new BitmapFactory.Options();

    // This defines the density DPI the image is designed for.
    options.inDensity = imgDensity;

    // These define the density DPI you would like the image to be scaled to (if necessary).
    options.inScreenDensity = metrics.densityDpi;
    options.inTargetDensity = metrics.densityDpi;

    try {
        // Download image
        InputStream is = new java.net.URL(url).openStream();
        return BitmapFactory.decodeStream(is, null, options);
    }
    catch(Exception ex) {
        // Handle error
    }

    return null;
}

So if your image at the specified URL is designed for an mdpi screen, you would pass DisplayMetrics.DENSITY_MEDIUM for the imgDensity parameter. If the current context has a larger density DPI (such as DisplayMetrics.DENSITY_HIGH), the image will be up-scaled accordingly.

David
  • 834
  • 1
  • 10
  • 27
0

Add android:scaleType="fitXY" to the ImageView. This would always render the image within the specified border.

Anenth
  • 99
  • 2
  • 8