2

i am making an android app in which i scale the bitmap using bitmap2 = Bitmap.createScaledBitmap(bmp, 150, 150, true);

this image looks good enough on a mobile with big screen ...but it goes out of proportion when used on a phone with small size .....any one knows the solution for it?

Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70
Ankit Srivastava
  • 280
  • 2
  • 7
  • 24
  • There's a similar question here -> http://stackoverflow.com/questions/17835318/making-a-layout-big-as-his-real-content-not-his-background/17837371#17837371 – g00dy Aug 09 '13 at 10:32
  • http://stackoverflow.com/questions/4821488/bad-image-quality-after-resizing-scaling-bitmap and http://stackoverflow.com/questions/6410364/how-to-scale-bitmap-to-screen-size – Simon Aug 09 '13 at 10:35

2 Answers2

6

You need to specify the image size in dp, just like you do in XML. To do the conversion from dp to pixels you can use something like:

float ht_px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, ht, getResources().getDisplayMetrics());
float wt_px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, wt, getResources().getDisplayMetrics());

For your particular case use:

float ht_px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150, getResources().getDisplayMetrics());
float wt_px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150, getResources().getDisplayMetrics());

and scale the bitmap with ht_px and wt_px. You might need to change 150 to 75.

Heinrisch
  • 5,835
  • 4
  • 33
  • 43
  • yeah i was thinking the same .....but bitmap2 = Bitmap.createScaledBitmap(bmp, 150, 150, true); doesn't accept values in dp..... and sorry i cannot understand your sloution,can you please elaborate ? – Ankit Srivastava Aug 09 '13 at 10:34
  • well i had no problem with ht,wt,..... i wanted to ask whats this typedValue thing??? – Ankit Srivastava Aug 09 '13 at 10:43
  • "Container for a dynamically typed data value. Primarily used with Resources for holding resource values." You can check it here: http://developer.android.com/reference/android/util/TypedValue.html – Heinrisch Aug 09 '13 at 11:11
4

take height and width of screen and calculate your size

DisplayMetrics display = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(display);
    int screenWidth = display.widthPixels;
    int  screenHeight = display.heightPixels; 

like you want to show your bitmap half of the screen then-

 bitmap2 = Bitmap.createScaledBitmap(bmp, screenWidth/2, screenHeight/2, true);

calculate it according to you requirement.

T_V
  • 17,440
  • 6
  • 36
  • 48