Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android scaling of images to screen density

I have an application with an embedded drawable 48x48 pixel at 71,12 pixels/inch I load the same image via a stream to a webserver, then load that stream

return new BitmapDrawable(getActivity().getResources(), new ByteArrayInputStream(imageThumbnail));

the displayed result is:

screenshot

How can i get the BitmapDrawable to scale the same as the rest of the drawables?

like image 567
user1035344 Avatar asked Nov 08 '11 09:11

user1035344


People also ask

What is Android screen density?

Android categorizes mobile screen density into one of six families: LDPI (low): ~120dpi. MDPI (medium): ~160dpi. HDPI (high): ~240dpi. XHDPI (extra high): ~320 dpi.

Do smaller screens have higher pixel density?

The image quality on the smaller monitor can often look sharper and more vivid because of its pixel density. This is measured in PPI (Pixels Per Inch). The smaller screen will have a higher number of pixels per inch than the larger screen.

What is 4x pixel density?

In 4x screen (~640 DPI) it takes four times as many pixels (352 x 144 PX) to fill the area of 88 x 36 density-independent-pixels.


2 Answers

you can trigger android bitmapfactory to scale bitmap automatically, codes for this:

BitmapFactory.Options options = new BitmapFactory.Options();
DisplayMetrics metrics = context.getApplicationContext().getResources().getDisplayMetrics();
options.inScreenDensity = metrics.densityDpi;
options.inTargetDensity =  metrics.densityDpi;
options.inDensity = DisplayMetrics.DENSITY_DEFAULT;

Bitmap bm = BitmapFactory.decodeStream(in, null, options);
in.close();
BitmapDrawable bitmapDrawable = new BitmapDrawable(context.getResources(), bm);
like image 129
figofuture Avatar answered Oct 06 '22 07:10

figofuture


Do something like this:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

Bitmap bitmapOrg = new BitmapDrawable(getResources(), new  ByteArrayInputStream(imageThumbnail)).getBitmap();

int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();

float scaleWidth = metrics.scaledDensity;
float scaleHeight = metrics.scaledDensity;

// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);

// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true);

Or

Maybe try a differnet approach... try setting the height and width of images in the XML layout in dips, I am guessing you have the ImageView with wrap_content height and width at the moment, try setting the height and width to 48dip

like image 43
SnowyTracks Avatar answered Oct 06 '22 07:10

SnowyTracks