-1

my issue is i want to re-size all images inside the post by showing 2 images per row

my app send the width of screen to the php api the php api must convert the dip to pixels and set image width 50% of screen dip

this is what aim doing so far

Display d = getWindowManager().getDefaultDisplay(); 

int w = display.getWidth();  
int h = display.getHeight(); 

String response = get_html_postinfo("bla.php?postid=1&h="+h+"&w="+w);

//response will append it to WebView

in php side

$screen_dip_w = $_GET['w'];
$screen_dip_h = $_GET['h'];

//i want this dip to be converted to pixel how ?
$screen_px_w = ( ? X $screen_dip_w );

$set_image_w = $screen_px_w/2;

any idea ? thank u

Kodr.F
  • 13,932
  • 13
  • 46
  • 91

2 Answers2

3

Display.getWidth() and Display.getHeight() already return the size of the screen in pixels, no conversion is required.

Also, I recommend switching to using Display.getSize(Point outSize) as the width and height methods are deprecated.

developer.android.com/reference/android/view/Display.html

Yjay
  • 2,717
  • 1
  • 18
  • 11
  • can i plz help me by given me the full code of `Display.getSize(Point outSize)` ? – Kodr.F Jan 31 '14 at 18:21
  • i have xperia TX to test the project its send with device width 720 , i set the picture to 360 width and its fill the screen , if i set it to 720 the picture goz over the screen ! – Kodr.F Jan 31 '14 at 18:42
1

here are some solutions i found:

public static int dpToPx(int dp)
{
    return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}

public static int pxToDp(int px)
{
    return (int) (px / Resources.getSystem().getDisplayMetrics().density);
}

and this:

// Converts 14 dip into its equivalent px

Resources r = getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, r.getDisplayMetrics());
Hamad
  • 5,096
  • 13
  • 37
  • 65