Draw an inch square on Android

Asked 2 years ago, Updated 2 years ago, 64 views

I'd like to set up a program that draws an inch square regardless of the terminal.
I thought about
①Get 1 dpi and get 1 inch square px

 int dpi=getResources().getDisplayMetrics().densityDpi;

仮にIf it is 100px, it will be 10x10, so do をpx.

intippenn=(int)Math.sqrt(dpi);

正方Specify xpx on one side of the square

rect.set(0,0,ippenn,ippenn);

It was written in this way, but it didn't work at all and the square itself didn't appear.
If you rewrite the variable ippenn to 100 and so on ( rect.set(0,0,100,100);), the square was displayed, so I think drawing the square itself is correct.
How can I make it?

android java

2022-09-30 20:49

2 Answers

DPI is the number of pixels per inch in length.The DisplayMetrics specification also returns one of 120, 160, or 240, so you don't need to take a square root.


2022-09-30 20:49

Because dpi values can be different in the x and y directions, you might want to use xdpi and ydpi in DisplayMetrics.

http://developer.android.com/intl/ja/reference/android/util/DisplayMetrics.html


2022-09-30 20:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.