background - Android - scaling image after I did it programmatically -


strange thing happens - @ least don't it.

i have image (w: 120px, h: 63px) presented on 1 imagebutton. variant of image have placed in drawable-hdpi (and there no other drawable directory). ok image , every resolution (density), android takes care of nicely.

but, trouble when take photo album (some big photo, example), scale dimensions of button (previously mentioned, w: 120px h: 63px) , set small image imagebutton background.

in case when testing on emulator medium density (160) ok, when testing on device high density button gets resized since scaled image appears smaller.

does has idea happening , why size of image changed once more after scaled it?

any clue highly appreciated.

here code use resizing image:

public static void resizeandsaveimage(string pathtoresize, int newwidth, int newheight, fileoutputstream output) { try {     bitmapfactory.options options = new bitmapfactory.options();     options.insamplesize = 8;     bitmap bitmap = bitmapfactory.decodestream(new fileinputstream(pathtoresize), null, options);     if (bitmap != null) {         int width = bitmap.getwidth();         int height = bitmap.getheight();         float scalewidth = ((float) newwidth) / width;         float scaleheight = ((float) newheight) / height;         matrix matrix = new matrix();         matrix.postscale(scalewidth, scaleheight);         bitmap resizedbitmap = bitmap.createbitmap(bitmap, 0, 0, width, height, matrix, true);         resizedbitmap.compress(bitmap.compressformat.png, 100, output);         output.close();     } } catch (exception e) {     // ... } 

dimension parameters passed in follows: newwidth = 120, newheight = 63.

in order achieve want need use density-independent pixels (dp) , not physical pixels. reading here:

the density-independent pixel equivalent 1 physical pixel on 160 dpi screen, baseline density assumed platform (as described later in document). @ run time, platform transparently handles scaling of dp units needed, based on actual density of screen in use. conversion of dp units screen pixels simple: pixels = dps * (density / 160). example, on 240 dpi screen, 1 dp equal 1.5 physical pixels. using dp units define application's ui highly recommended, way of ensuring proper display of ui on different screens.

in order explain in more detail why happening 1 image (album photo) , not image resource, need post code using scaling.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -