Question

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

Was it helpful?

Solution

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

OTHER TIPS

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());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top