How to define a line in centimeters in Android - android

I'm trying to define a line in Android which measure must be 10cm wide.
I tried to put the width in mm and inches, but the results weren't satisfactory.
Also, I tried to follow this link: draw square 3x3 cm in Android
Desperately, i tried to measure 10cm in dip on the screen, but not is the same length in all screens
Is there any way to get it?
Thanks

I think:
height of screen (cm) / height resolution(pixel) * 3 = 3 cm height in pixel for that screen and resolution.
width of the screen (cm) / width resoluzion(pixel) * 3 = 3 cm width in pixel for that screen and resolution.

Not sure about this.
REFER LINK TO GET dpi
nbpixel(1 inch) = densityDpi
so convert 1 cm to inch who give approximativly 0.4 inch
nbpixel(1cm) = 0.4 * densityDpi
So for make a ligne of one cm you must draw 0.4*DPI pixel

To draw a line of 1 cm, you must use multiple dpi (pixel density) to find the number of physical pixels.
dpi/2.54
Mobile device displays also have something which is called devicePixelRatio. You might want to check that too. When building RealRuler (online ruler) I used to multiple mobile screen width/height with that ratio to get actual size. Not sure if it impacts actual size in cm in Android.
All this is very tricky.

An inch or a mm are unit of linear measurement on a surface, which could be a screen or a piece of paper.I'm strange if the result is not satisfactory for you.See this answer for more details.

Related

Android Graphics: Drawing same size circles

I have a major issue I am working on for days now. It is much understandable by looking at the requirement first. I will list down my requirement as simple as possible in point form below.
I have 5 android phones. 5 different brands, different screen sizes.
Imagine the screen sizes are 4inch, 4.5inch, 5inch, 5.1inch and 5.2inch
I have an android app and it has a drawing canvas.
Now using canvas.drawCircle(x / 2, y / 2, radius, paint) I am drawing a circle. Imagine the radius is 100 (100 pixels?)
I install this app in my smallest screen phone, 4inch. Then I use a ruler and measure the the circle diameter. Imagine the circle diameter is "exact" 3cm.
Now I install this in my other phones.
Unfortunately, in my 4.5 inch phone the circle diameter is 3.2cm. In 5 inch phone it is 3.3 cm. In 5.1 inch phone it is 2.8cm and so on.
However I want my circle diameter to be 3cm (exact 3cm) in every phone.
Above requirement is something I am trying for days now. I tried the following to make sure I get a circle with no size change across all screens.
Using ImageView - I tried using an ImageView and added a circle image. I have given the width, height fixed. I have tryied setting the values in px, dp, inches etc. I also tried scalling options available for ImageView. Also tried placing the same image in drawable-nodpi folder. Tried creating drawable folders for all sizes (ex: drawable-mdpi, drawable-hdpi). Non of this stopped the image from being scaled.
Using Canvas - As explained in the above, I tried using canvas and drawing the image. The drawing scales.
Finding pixels per inch.- Tried finding pixels per inch of each phone programatically thinking I can find a way to develop a logic from it to dynamically draw the images in same size. I used several links including - Calculate PPI of Android Device. Most of them talk about getting screen resolution only.
How can I fulfill my requirement of drawing same size circles? If it can be done by knowing PPI, how should I do it?
The Answer before has worked for me but as long as it didn't for you I will try explain something I think it will help.
I have a device that have a density of 1 (160 px per inch), and the Display metrics told me that i have 320 * 480 pixels, with simple calculation I should know my mobile width and height in inch like this : 320 /160 = 2 inch for width , 480/160 = 3 . for now everything was just fine until i got the ruler and measured it and the surprise was this : I have a 1.65 * 2.48 inch !!.
Then I noticed that there is something called physical pixels per inch of the screen this was the relief for me and you can get it like this :
getResources().getDisplayMetrics().xdpi; //for width physical dpi
getResources().getDisplayMetrics().ydpi; //for height physical dpi
And now I can calculate my Physical width and height of my device like this :
my physical dpi for both width and height is 193.5238 so...
320 / 193.5238 = 1.65 inch width
480 / 193.5238 = 2.48 inch height
And that was correct !!.
Now back to your problem lets get the 3cm in pixels for any Phone :
getting the width in cm :
width in cm = width in inch * 2.54 (because 1 inch equals 2.54 cm)
getting the amount of pixels in each cm :
width in pixels / width in cm = Pixels per cm for width (px/cm)
now you want a 3cm then the pixels according to it is:
3 * Number = 3cm Pixels
all of the above can be shortened like this :
float devicePixelsWidth = getResources().getDisplayMetrics().widthPixels;
float deviceActualDpi = getResources().getDisplayMetrics().xdpi ;
float deviceActualInchWidth = devicePixelsWidth / deviceActualDpi ;
float deviceActualCMWidth = deviceActualInchWidth * 2.54f ;
float PixelsForActual3CM = devicePixelsWidth / deviceActualCMWidth * 3;
In the end, all of the above has been tested and approved by me and has the same accuracy of the previous Answer method :)
maybe this is not answer, but might be helpful
float _100MmAsPx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM,
100/*mm unit set*/, getResources().getDisplayMetrics());
above gives me 1661.5354 on Nexus 5x and 1889.7638 Nexus 5 (emulator). both have fullHD display, but 5 has 4.95 inch versus 5.2 inch in 5x.
it means 100 millimeters is 1662 pixels on 6 and 1890 on 5x

Get size of the Android Phone

I would like to get size of the Android Phone but size in mm and not in pixels.
I know how to get size in pixels, however I would like to do some RND and for that I want to get width of the screen in mm.
Like here Galaxy S4 size is Dimensions 136.6 x 69.8 x 7.9 mm (5.38 x 2.75 x 0.31 in)
136.6 x 69.8 x 7.9 mm (5.38 x 2.75 x 0.31 in)
^^^
Does any one knows what is this size for?
Though I am iPhone developer BUT I am beginner (1 day baby) for Android. Well below is what I want to do as RND.
Let's say designer gives me design for the app of size 1080*1920 and have title of size 30px (which covers 80% exactly) of the screen size.
That means 10% space on left side, 80% text and 10% space on right side.
I know in Android we have something called sp or android:textAppearance="#android:style/TextAppearance.Large", but that won't give the 10% spaces.
What I want to achieve is regardless of size, design should go same.
Considering design to fit with width, if height increases, I would have scrollview and I am okay with that.
"Pixel density" is the number of pixels per inch. So dpi gives you the scale factor you need so that the number of pixels is consistent across devices.
Another way of saying this is that if you have a 480 pixel screen with hdpi, then a 360 pixel screen at mdpi is about the same physical size. While the screen sizes may vary some, it generally is not that important. Even so, as mentioned, see this post about doing the actual calculation:
Is there a way to determine android physical screen height in cm or inches?
However, you are talking about percentages of screen size in your example. If you know the screen size in pixels, then the physical size doesn't matter, just do your calculations using pixels. If you have 480 pixels, 10% is 48.
You cannot do this in XML, which it sounds like you might be wanting to do. You can use "weighted linear layouts" for runtime calculations of the screen size. You can also use different res folders depending on screen size (like res/layout-hdpi).
You may want to read this from Google regarding supporting different screen sizes: http://developer.android.com/guide/practices/screens_support.html
Use DisplayMetrics. The API is: http://developer.android.com/reference/android/util/DisplayMetrics.html
You can perform a calculation on the given fields and obtain the height and width in inches, then convert that to mm.

Can Android support dp's less than 1dp?

I am making a calendar and I need a grid. Problem is the borders, or rather the space between each grid, which is what I am using to kind of simulate a grid, is 1 dp. But its rather thick. I am looking at other calendar apps that have the borders, and they are very thin. Even if I were to use a drawable shape and make it 1dp, it still has that thickness. I tried using .5, but that did not seem to work. Is that not possible?
Android does allow you to enter fractional values for dp into XML, though I'm not sure I would recommend it because the results become less easy to predict/compute. Devices convert dp values into pixels using (basically) the following formula:
px = (int)(scale * dp + 0.5)
(i.e. the device density scale rounded to the nearest whole pixel value)
The scale value would be based on the screen density of the device:
MDPI = 1
HDPI = 1.5
XHDPI = 2
XXHDPI = 3
So 0.5dp would result in {1px, 1px, 1px, 2px} for the above densities, whereas 1dp would be {1px, 2px, 2px, 3px}. A tiny value like 0.1dp would resolve to {1px, 1px, 1px, 1px} because anything less than 1 in the above formula resolves to a single pixel unless the value was explicitly 0dp (it's not possible to draw something thinner than a single pixel width).
If you want to ensure that thinnest possible width is used, probably best to define the width explicitly with px instead of a scaled unit like dp. Setting a value of 1px will draw with a single pixel on all devices, and is much more readable than hoping 0.5dp or 0.1dp does the same.
Yes.
I set to 0.1dp and it is smaller than 1dp on my 4" ME860 DRIOD.
Simple answer is yes, you can.
Simpe solution to your problem (i.e. achieve the thinnest possible line) set thinckness to:
1px
I suggest px as its not device dependant and it is the smallest measure available, furthermore it wont be converted into another form, therefore no rounding errors nor any unexpected rendering... let me explain.
Android layout xml file, or dimensions xml values file, will allow you to enter decimal values for dp.
HOWEVER:
I have experimented with several values from 0.1dp to 1dp, on many devices. Depending on the device - it may not render so as you expect.
On lower pixel density devices, the lines may render thicker on one side compared to another side even though they are coded to have the same... This is due to inaccuracy introduced when truncating the converted to dp to px value - (as Devunwired mentions):
px = (int)(scale * dp + 0.5)
A pixel (px) is the smallest unit and must be an integer. so - if your aim is to simply have the smallest possible line/border, why not set it to:
1px
I hope this helps!

Understanding Samsung Galaxy Tab screen density

One would say that if the Galaxy Tab screen resolution (in portrait mode) is 600px and the screen width is 3.55inch, the screen density would be 600/3.55 = 169 dpi. Knowing that and keeping in mind the way the device independent pixels (dp) is computed (http://developer.android.com/guide/practices/screens_support.html):
px = dp * (dpi / 160);
600 = dp * (169 / 160);
dip = 568
So drawing a horizontal line of 568dp (device independent pixels) width starting at position 0 must exactly match the width of the screen. But if you try this on device you will find that the screen width is 400dp. I will use the same formula again but for getting dpi:
600 = 400 * (dpi / 160);
dpi = 240
So having the 240dpi, 3.55inch screen width and 600pixels, does it mean that one physical pixel is composed of more ‘dots’ otherwise the parameters corresponds to the width of 852pixel (3.55*240).
I thought that dpi means dots per inch, which is pixels per inch. But this seems to not be true...
Added later:
This (http://developer.android.com/guide/topics/resources/more-resources.html#Dimension) says:
160dp is always one inch regardless of the screen density
Which is not true. Just check the measurement source from this:
Difference between android dimension: pt and dp
Added even later:
The reason I am asking is that violating the rule that 160dp = 1inch leads to the fact that when specifying the control width to e.g. 320dp it will cover on Galaxy Tab much bigger portion that that really necessary and much bigger then what you would expect from 600x1024px screen...
Thanks for clarification
BR
STeN
Galaxy Tab (7") doesn't report its real density. To understand this issue, read the following article:
http://realmike.org/blog/2010/12/21/multiple-screen-sizes-with-processing-for-android/
Apparently, that’s also what Samsung found when they made the Galaxy
Tab. The Galaxy Tab has a 7″, 1024×600 screen with 170 dpi. Yet, the
Tab does not report its density as “mdpi” but as “hdpi”, so the layout
looks exactly as in the second screenshot. If they used “mdpi”, the
icons would be .28″ wide, with “hdpi”, they are .42″ wide—not a big
deal, and I must admit, the layout does look prettier this way.
The article contains some images that will make you understand the problem.

Calculate pixels based on DPI and Physical Size

I'm trying to make an Evolus Pencil template for Android. My device has a 1.75x2.5625" screen, and I would like to emulate these exact physical dimensions. From reading this post:
Android multiple screen sizes with same density
I see Physical Size = Pixels / Density. So if I create an image that is 168px wide, on a 96dpi screen, should I not get a physical image of 168/96=1.75" wide?
Because I get one about 1.5" and I'm lost. I am running Ubuntu and confirmed my screen dpi with xdpyinfo as being 96x96. So what is a formula I can use for this? I was thinking one could get the pixels needed by multiplying the inches we want on the screen by the dpi of the screen, but this gives me the 168 mentioned above and obviously gets me no where.
Can anyone point me in the right direction, I'm honestly terrible with numbers and math so my apologies if I'm missing something simple or obvious.
Are you sure that the display is actually 96 ppi? Going by the specs, and my own calculations, your Lenovo S10e actually has a density of ~116.36 ppi, which is probably where your difference is coming in.
Assuming these specs are correct:
10.1" diagonal
1024 x 576 resolution
16:9 screen ratio (taken from above resolution)
Using some geometric formulas, you can get the actual width and height of the monitor as:
Width: 8.8"
Height: 4.95"
Dividing 1024/8.8 and 576/4.95 gives you 116.36 pixels per inch, rather than 96.
Using this instead, a 168 pixel image should display as 168/116.36, or ~1.44", which is consistent with your results. I wouldn't put too much faith in the xdpyinfo results. :)

Categories

Resources