Convert mm to pixels - android

Does anyone have a good algorithm to convert mm to pixels on Android?
The thing is, I want to be able to set a minimum height of a View using mm as unit.
And no, I don't want to use android:minHeight in xml, this should be in the code.

Convert 1mm to pixel
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 1,
getResources().getDisplayMetrics());

Related

animating xml elements in java code in dps (android studio)

I'm using android studio to design a game.
I used the 'dp' unit in my 'xml' file for defining my elements.
In java code, I want to move those elements by function animate() like image_button_red1.animate().xBy(first value).yBy(second value);
this function only takes float value, But the animation is different in each device.
I want to use the 'dp' unit to solve this problem.
Is there a function that takes another unit like 'dp'?
I've found an answer.
I designed my layout in pixels and when I used, for example, animate().xby(10.0f), the translation was right in each device. The equation between the pixel and dp is: px = dp * (dpi / 160);
Then I used dp instead of px and used the function Resources.getSystem().getDisplayMetrics().density to get the density of each screen which is running the code.
So I used a float variable instead of 10.0f which this value calculated from the noted equation. My object size was 360 dp, and the equation changed to px = 2.25*dpi.
And in my case, I used a variable like this:
float House_size = 2.25f * 14.44f * Resources.getSystem().getDisplayMetrics().density;
then I used animate().xby(House_size);
And now the result works properly on every single device.

How to get View size in meters?

There is a way to convert view's float coordinates to length in meters or another real unit?
I have a surfaceview that displays the motion of some real body and I want to display the scale of the animation, so I have to get the conversion from coordinates or sizes of the view to meters...
may be in millimeters?
int width= view.getMeasuredWidth(); // will be in px
float mm = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, width, getResources().getDisplayMetrics());
also check out other TypedValue.COMPLEX_UNIT_ (maybe inches?)

Measure text returns float value

It is a stupid question, probably...
However, what is the unit of measure of the measureText() value returned?
I need the width of a text in px.
I thought px, dp, sp would be integer values. So why measureText() in TextPaint returns a float value?
Why would the return type not be a float, representhing pixels?
Drawing on screen uses floats all the way down to the GPU level. This makes calculations more convenient and makes it possible to use antialiasing and subpixel rendering.
Dimensions such as px, dp, sp are floats too, though there are convenience methods to get an integer pixel size value.
as per the documentation.........
http://developer.android.com/reference/android/graphics/Paint.html#measureText(java.lang.String)
The returned value is the number of pixels (not dp or sp), thus is returned as you need it :) It is returned as a float probably due to the official Android classes finding a float value more useful for calculations and optimization, but I could be wrong on that.

Change text height and width separately when drawing with canvas

I want to know if there is any way to modify text height and width separately when drawing with canvas. To set text size you can simply do paint.setTextSize(x);but it change text size in X and Y and I need to change text size in X and Y separately as you do with paint.setTextScaleX(x);but there isn't anything like paint.setTextScaleY(y);.
Is any way to implement this or does it already exists in Android ?
Thank you
There is no setTextScaleY method, because there is no need for one. setTextSize multiplies both X and Y scale factors, and setTextScaleX multiplies the X scale factor only. So you could reach any desired scaling scaleX, scaleY this way:
setTextSize(scaleY);
setTextScaleX(scaleX/scaleY); //setTextScaleX scales according to the CURRENT size (setTextScaleX(1) does nothing).

Android: Convert points to pixels

I have been using the pt unit in XML files. Now I need to set the width of an element at runtime. How can I set the width by points so that it is the same unit I have been using everywhere else. I assume I will need to multiply by resolution and dpi. A code sample would be best.
First you should really read the following in-depth article from the Android Developer Documentation :
http://developer.android.com/guide/practices/screens_support.html
Right in the middle you'll find the following under the title :
Do not use hard-coded pixel values in your code
// Convert the dps to pixels
final float scale = getContext().getResources().getDisplayMetrics().density;
mGestureThreshold = (int) (GESTURE_THRESHOLD_DP * scale + 0.5f);
You can use dip instead of pt

Categories

Resources