Skip to content Skip to sidebar Skip to footer

Draw Square 3x3 Cm In Android

I need to draw an exact square 3x3 cm on the screen, you need the exact dimensions in any type of screen as design work. The result in a Samsung Ace is 2.8 cm, the result is not ex

Solution 1:

I think you need to use the dpi in the specific directions x and y eg:

// We want 30/25.4 inches but in dotsfloatinches=30/25.4f;

floatxdpi= getResources().getDisplayMetrics().xdpi;
floatxDots= inches * xdpi;
floatydpi= getResources().getDisplayMetrics().ydpi;
floatyDots= inches * ydpi;

Paintpaint=newPaint();
paint.setColor(Color.WHITE);
canvas.drawRect(0, 0, xDots, yDots, paint);

Then you will have more chance, but they are not accurate for every phone. I got the following results after placing this code in the onDraw() method of and Android app:

Nexus 7:2.8cmGalaxyNexus3.0cmHTCSensation3.0cmHTCDesireHD2.7cm

So it is not exact in all cases.

Post a Comment for "Draw Square 3x3 Cm In Android"