10

I have this code:

<ImageView
android:id="@+id/listitem_logo"
android:layout_width="match_parent"                                   
android:layout_height="wrap_content" />

and

imageview_logo.setImageBitmap(bit); // comes from assets
imageview_logo.setAdjustViewBounds(true);        
imageview_logo.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageview_logo.setBackgroundColor(0x00000000); 
imageview_logo.setPadding(0, 0, 0, 0);
imageview_logo.setVisibility(v.VISIBLE);

When loading the image this way, no scaling seems to have been done. However, if I load an image through setImageDrawable() r.res. the image inside the ImageView is resized. However, I need to use setImageBitmap() since I load my images through assets folder.

Am I missing some setting here? That will make Android resize and scale the bitmap, so it uses the full width of the ImageView? I guess I can do it myself in code, but offhand I would think what I want to do would be supported just by setting some properties.

Tom
  • 3,587
  • 9
  • 69
  • 124
  • 1
    Try using `setImageDrawable()` – Sagar Waghmare May 25 '13 at 11:31
  • setImageDrawable does not accept a Bitmap. – Tom May 25 '13 at 11:32
  • 2
    use CENTER_CROP as scaletype AND WRAP_CONTENT for the width – Blackbelt May 25 '13 at 11:34
  • 2
    Drawable d = new BitmapDrawable(getResources(),bitmap); – Sagar Waghmare May 25 '13 at 11:34
  • 1
    then convert the Drawable to bitmap – Zala Janaksinh May 25 '13 at 11:35
  • @Sagar Well, that does not seem to work either (compiles fine). The image still does not resize. (Thanks though, I should of course have tried that as well.) I guess the difference I saw oriignally under my testing must have been caused by the drawable folder I orignally stored my images in. – Tom May 25 '13 at 11:45
  • @blackbelt I want the imageview to resize the bitmap to take the full width of the imageview/parent... And then resize/scale the imageview/bitmap height appropriately to keep the orignal bitmap width/height ratio. – Tom May 25 '13 at 11:48

3 Answers3

13

Can't trust system for everything specially android which behaves very differently sometimes. You can resize the bitmap.

height = (Screenwidth*originalHeight)/originalWidth;

this will generate the appropriate height. width is equal to screen width as you mentioned.

Bitmap pq=Bitmap.createScaledBitmap(pq,Screenwidth,height, true);
Garbit
  • 5,805
  • 6
  • 39
  • 72
Aashish Bhatnagar
  • 2,595
  • 2
  • 22
  • 37
8

As I needed a bit of time to figure out the complete code to make this work, I am posting a full example here relying on the previous answer:

   ImageView bikeImage = (ImageView) view.findViewById(R.id.bikeImage);
   AssetLoader assetLoader = new AssetLoader(getActivity());
   Bitmap bitmap = assetLoader.getBitmapFromAssets(
                Constants.BIKES_FOLDER + "/" + bike.getName().toLowerCase()
                        .replace(' ', '_').replace('-', '_') + ".png");
   int width = getActivity().getResources().getDisplayMetrics().widthPixels;
   int height = (width*bitmap.getHeight())/bitmap.getWidth();
   bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
   bikeImage.setImageBitmap(bitmap);
narko
  • 3,645
  • 1
  • 28
  • 33
0

With bit as your Bitmap image, you can directly set yout imageView and then crop it using the properties of the imageView as:

    imageview_logo.setImageBitmap(bit);
    imageview_logo.setScaleType(ImageView.ScaleType.CENTER_CROP);
Cristian Zumelzu
  • 842
  • 10
  • 15