Android scaling imageview from setImageBitmap

Tom picture Tom · May 25, 2013 · Viewed 15.5k times · Source

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.

Answer

Aashish Bhatnagar picture Aashish Bhatnagar · May 25, 2013

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);