Android drawable resizing on screen when reading image from file

I have an image in a personal file. I read the file, create the drawing and assigned it an ImageView. ImageView has WRAP_CONTENT, so the size is automatic.

On 320x480 screens, the image looks good. But on screens with high resolution and high density 480x800 or 480x854 (N1, droid), when the image, for example, is 150x150, I see the image as 100x100.

Of course, it has something to do with density, but I'm not sure how to resolve it.

This is my code:

FileInputStream fis = this.openFileInput("icon.png"); icon = Drawable.createFromStream(fis, "icon"); fis.close(); imageView.setImageDrawable(icon); 

thanks

==================================================== ===============

update:

with the following code:

FileInputStream fis = this.openFileInput ("icon.png");

icon = Drawable.createFromStream (fis, "icon");

If I then inspect the size of the icon, android thinks the size is 100x100, when really 150x150.

It seems that it reduces the image in density. Can someone explain this and how to avoid it.

thanks

+4
source share
4 answers

Scan the image to the required size:

  d = Drawable.createFromPath(filePath); if (d != null) { Bitmap bitmapOrg = ((BitmapDrawable) d).getBitmap(); int width = bitmapOrg.getWidth(); int height = bitmapOrg.getHeight(); int newWidth = 170; int newHeight = 170; // calculate the scale float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // create a matrix for the manipulation Matrix matrix = new Matrix(); // resize the bit map matrix.postScale(scaleWidth, scaleHeight); Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true); // make a Drawable from Bitmap to allow to set the BitMap d = new BitmapDrawable(resizedBitmap); } 
+2
source

I am working on a similar problem, try the following:

 TypedValue typedValue = new TypedValue(); //density none divides by the density, density default keeps the original size typedValue.density = TypedValue.DENSITY_DEFAULT; Drawable d = Drawable.createFromResourceStream(null, typedValue, fis, "icon"); 

I'm still working on how to develop a common solution for selecting resolution / resize images from a common stream instead of drawable- * directories.

+2
source

Try BitmapDrawable # setTargetDensity:

 Bitmap b = BitmapFactory.decodeFile(path) BitmapDrawable bmd = new BitmapDrawable(b); bmd.setTargetDensity(getResources().getDisplayMetrics()); 
+2
source

Define some measurements in independent devices (or DIP).

+1
source

Source: https://habr.com/ru/post/1307044/


All Articles