0

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 ?

mcfly soft
  • 11,289
  • 26
  • 98
  • 202

2 Answers2

0

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

1 cm = 38px almost

you can use 38px for 1cm.

  • Thats wrong. Every device has a different density, so 38px is different on every device regarding physical size. – mcfly soft Apr 26 '19 at 13:14
0

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);
    }
Ganesh Pokale
  • 1,538
  • 1
  • 14
  • 28
  • My question is not howto get the size, but howto apply the size to html in a webview. You can see, that I have already the correct size. – mcfly soft Apr 26 '19 at 13:14