0
votes

I have:

  • A Dialog with an webview in it. The dialog can have different sizes on the screen.
  • I have a html in the webview with an image tag and size properties.

What I want:

  • I would like to have the picture with a size of 1 cm (Centimeter) square. How can I achieve this ? I realized that the size of width and height of the image tag is not equal to pixels in my case. If I set the width of the image to the screensize, then the picture is displayed much larger then the screenwidth.

Code: Cm in Pixel:

    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    fDensityDpi = metrics.densityDpi;
    float pixelperinch = getfDensityDpi();
    float inchpercm = 1.0f / 2.54f;
    pixelPerCm = (int) (inchpercm * pixelperinch + 0.5);

Content for the webview:

 "<img width=\"" + pixelPerCm + "\" height=\"" + pixelPerCm + "\"  src=\"" + imagePath + "\" " + sStyle + ">"

The size of the picture on the device is not the expecting 1 cm.

Can someone help me howto size in Image in a webview to exactly one cm on every device ?

2
I finally found the solution here: stackoverflow.com/a/7822858/1344545 - mcfly soft

2 Answers

0
votes

you need to set fix height and width of image in webpage.

1 cm = 38px almost

you can use 38px for 1cm.

0
votes

For webview you can use dpTopx method

   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);
    }