Anonymous
Anonymous

Reputation: 1773

Images scales defferently in setImageDrawable() and setImageBitmap()?

I am new in android development. I have an issue regarding the image scaling in my app. When i set images using setImageDrawable() images look proper like in screenshot:

enter image description here

But if i set image using setImageBitmap() method it does not look proper. Some black color occurs around it like in screenshot bellow:

enter image description here

I have tried it in multiple applications and i observed that in some applications the image size is becoming smaller than the size ofter setting image through setImageDrawable() or setImageResource(). the reason for setting images through setImageBitmap() is that i want to set images to the imageviews which are kept in assets folder and not in drawable.

Following is the code i used to create a bitmap and set it to imageview:

                        BitmapFactory.Options opts = new Options();
                        opts.inPurgeable = true;

                        try
                        {
                            Bitmap preview_bitmap = BitmapFactory.decodeStream(assetManager.open(drawableFolder+"/"+tmpArr[0]),null,opts);
                            view.setImageBitmap(preview_bitmap);

                        }
                        catch (Exception e) 
                        {
                            LNLog.writeTOErrorToLog(e.getMessage());
                        }

So, please can anyone tell me where i'm i going wrong or missing something to set. I tried to set Options but did not worked. I want to set images as in first screenshot that is how they looks by using setImageDrawable(). Thanx in advance.

Note: in screenshots images are referent in text on it only.IF i set hindi images i.e. the images on which text is in hindi language using setImageDrawable() then they looks proper as in like in screen shot one.So , no issue of image files.

Upvotes: 1

Views: 2778

Answers (2)

Anonymous
Anonymous

Reputation: 1773

This worked for me :

try
{
   view.setAdjustViewBounds(true);
   int height = view.getDrawable().getIntrinsicHeight();
   int width = view.getDrawable().getIntrinsicWidth();
   BitmapFactory.Options opts = new BitmapFactory.Options();
   opts.inPurgeable = true;
   opts.inScaled = true;
   Bitmap preview_bitmap = BitmapFactory.decodeStream(assetManager.open("drawable-mdpi/FAQ_hi_in.png",null,opts);
   Bitmap bmp = Bitmap.createScaledBitmap(preview_bitmap, width, height, true);
   view.setImageBitmap(bmp);
}

In this code I have taken the height and width of actual image and then scaled my bitmap to that height and width. Then I converted this scaled bitmap to drawable and set it into my ImageView, thats it. It resolved my problem of black border around image.

Upvotes: 0

maaz
maaz

Reputation: 224

You can get rid of the black borders by using

android:adjustViewBounds="true" in xml 

or

imageView.setAdjustViewBounds(true) in Java.

EDIT: Then try using drawable only

Drawable d = Drawable.createFromStream(assetManager.open(filePath), null);
view.setImageDrawable(d)

Upvotes: 1

Related Questions