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 dots
float inches = 30/25.4f;
float xdpi = getResources().getDisplayMetrics().xdpi;
float xDots = inches * xdpi;
float ydpi = getResources().getDisplayMetrics().ydpi;
float yDots = inches * ydpi;
Paint paint = new Paint();
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.8 cm
Galaxy Nexus 3.0 cm
HTC Sensation 3.0 cm
HTC Desire HD 2.7 cm
So it is not exact in all cases.
Post a Comment for "Draw Square 3x3 Cm In Android"