How to convert cm to pixel in android ?

Shay Wilner 1,746 Reputation points
2023-08-15T09:22:08.1466667+00:00

Hello

How to convert cm to pixel in android ?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,366 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 77,256 Reputation points Microsoft Vendor
    2023-08-16T06:47:32.1133333+00:00

    Hello,

    ===========Update==============

    To convert between pixels (px) and millimeters (mm), the first step is to calculate the DPI (dots per inch) of the screen. Please see getDPI method like following code.

    To calculate the DPI, you need to know the screen's pixel count and screen size in inches. See my ScreenInchUtils.getScreenInch method.

    The DPI can be calculated using the following formula:

    DPI = Pixel count / Screen size (inches)

    For example, if your screen resolution is 1920x1080 pixels and the screen size is 15.6 inches, you can calculate the DPI as follows:

    DPI = 1920 / 15.6 ≈ 123.08

    Once you have the DPI value, you can use it to convert between pixels and millimeters. By definition, 1 inch is equal to 25.4 millimeters. Therefore, you can use the following formulas for conversion:

    Pixels to millimeters:
    Millimeters = Pixels / DPI * 25.4

    Millimeters to pixels:
    Pixels = Millimeters * DPI / 25.4

    By using the above formulas, you can convert pixels to millimeters or millimeters to pixels, depending on your requirements.

    You can use CmToPx method to convert it. You need to CM convert to px, I have convert cm to mm for converting.

     //cm convert to px
            public int CmToPx(int value)
            {
                float inch = value*10 / 25.4f;
                int c_value = (int)(inch * getDPI(this));
                return c_value;
            }
    
    
            /**
            * Get the DPI (Dots Per Inch), which is the number of pixels per inch of length in an image.
            * DPI = width / ((size^2 × width^2) / (width^2 + height^2))^(1/2) = height / ((size^2 × height^2) / (width^2 + height^2))^(1/2)
            * @return
            */
          
      public float getDPI(Activity activity)
            {
                //get screen size
                double screenSize = ScreenInchUtils.getScreenInch(activity);
                //Get Width and height
                int widthPx = Resources.DisplayMetrics.WidthPixels;
                int heightPx = Resources.DisplayMetrics.HeightPixels;
                float dpi = (float)(widthPx / Math.Sqrt((screenSize * screenSize * widthPx * widthPx) / (widthPx * widthPx + heightPx * heightPx)));
                return dpi;
            }
    

    ScreenInchUtils.cs

    public class ScreenInchUtils
        {
            private static double mInch = 0;
    
    
    
    
    
           public static double getScreenInch(Activity activity)
            {
                if (mInch != 0.0d)
                {
                    return mInch;
                }   
                    int realWidth = 0, realHeight = 0;
                    Display display = activity.WindowManager.DefaultDisplay;
                    DisplayMetrics metrics = new DisplayMetrics();
                    display.GetMetrics(metrics);
                    Point size = new Point();
                    display.GetRealSize(size);
                    realWidth = size.X;
                    realHeight = size.Y;
                    mInch = formatDouble(Math.Sqrt((realWidth / metrics.Xdpi) * (realWidth / metrics.Xdpi) + (realHeight / metrics.Ydpi) * (realHeight / metrics.Ydpi)), 1);
                return mInch;
            }
            /**
          * Returns a double value with the specified number of decimal places (rounded using half-up rounding).
          * newScale is the specified number of decimal places.
          * @return
           */
            public static double formatDouble(double d, int newScale)
            {
            BigDecimal bd = new BigDecimal(d);
            return bd.SetScale(newScale, RoundOptions.HalfUp).DoubleValue();
            }
    }
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.