in android, how to specify inches in an animation resource - android

I want to specify an animation resource to use with Activity.overridePendingTransition. However the only available dimensions for the animations are %ages, pixels, etc but not inches. I need to use inches in my animation. Is there a workaround to this

There's two ways to do this. The easiest (presumably, without seeing your code) is to define the number of DPs that you want for the animation in a dimension xml resource. You can then get this value converted into "local" pixels by calling getResources().getDimensionPixelSize(). That will get you what you want. Alternatively you can use the formula to convert from DPs to PX manually: px = dp * (dpi / 160)

Related

Converting automatically from px to sp or dp in Android

I got a layout design which states sizes in px which I save in the dimen resource file.
Is there an automatic way to somehow let Android convert the px unit automatically to sp for strings or dp for other sizes, without the need for me to call a method every single time which converts the unit?
If not, how can I define specific sizes in the layout's xml if all the units are in px?
You need to call a function all the time,
or you need to create custom views that extends the views and override the size/textSize function that converts them automatically
class CustomTextView : AppCompatTextView {
override fun setTextSize(size: Float) {
// do the convertion here
super.setTextSize(size)
}
override fun setTextSize(unit: Int, size: Float) {
// or here
super.setTextSize(unit, size)
}
}
and you will probably need to extend every view that has been using dimens if you don't want to write a function all the time.
if you read here you will understand that what you want isn't the best solution.
I would suggest to you to change everything px to dp and then put those on the files
most probably your app will work for more than one density screens so the pixel way is very bad choice
Designing layouts for dp
When designing layouts for the screen, calculate an element’s
measurements in dp:
dp = (width in pixels * 160) / density
For example, a 32 x 32 px icon with a screen density of 320 equals 16
x 16 dp.
Update
unfortunately what you want cannot be happen. You will have to generate different dp values for different density screens. Read also how to support diffent screen densities from the official documentation
Unfortunately, px to dp conversion can not be done by the system automatically for you.
however, you can define several values folders for various screen sizes and define different dimensions(in dp) for each of them.
refer this question for more details:
How to define dimens.xml for every different screen size in android?
Conversion from PX to dp or sp is something you have to do yourself.
and to conclude, you should not be doing this as most of the time values defined in dp and sp suits to most of the screen sizes, so try that first.

Android: Is line length in PX or DIP?

I have a custom view.
When I draw a line from (x,y) to (x+10,y), is the distance in DIP or PX?
As documentation says,
At runtime, the system transparently handles any scaling of the dp
units, as necessary, based on the actual density of the screen in use.
Thus, all units are in dp.
If you are using one of the Canvas.drawLine methods it is in PX
I believe it's in pixels. If you want to know how to convert from DP to pixels as in if you want your custom view to work with DPs just like all other Views.
Converting pixels to dp

How can I make correctly sized images to use in my android app

I have an image that I want to use in my app but I want it to look good on multiple devices, which is the reason there are many drawable folders (hpdi, xhpdi etc etc).
My image is just one size 170 x 80 px. Is there a tool I can use to generate appropriately sized versions of this image to place in my drawable folders?
Also, on a side note. If I were to set the 170x80 px image as the source of an image button. Do I set the width and height to wrap_content or do I set it to 170 and 80 dp?
Thank you.
If you only have one image and copy it in your drawables folder, the android framework assumes it is mdpi and automatically scales it up or down if you don't provide the others.
To answer your last question, you should set layout_width and layout_height of your image in the layout to wrap content then. You also should not assume that 170px look the same as 170dp.
convert from dp to pixels:
px = dp * (dpi / 160)
convert from pixels to dp:
dp = px / (dpi / 160)
Here is a very useful article about how to support different devices on android
The Android Asset Studio can generate images for you in different dpi formats.

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!

Is it wise to use "dp" instead of something else?

In Android when Im measuring, is it wise to use DP, or something else? Im scared that dp might change from device to device and my app will look horrible in anything other than my phone.
You should ALWAYS use dp. If you need the value in pixels, you could use this method -
public static int dpToPixels(Context context, float dp) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
}
always use dip
density independant pixel
If you look at the reference documentation you'll see that's the point of dp. Specifically,
The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.
So unless you want to be limited to a single screen size and density, always use dp.
Qasim, dp or dip was invented to prevent what your afraid of. Check out this article: http://developer.android.com/guide/practices/screens_support.html, as well as this other question: What is the difference between "px", "dp", "dip" and "sp" on Android?
In addition to dp, you may also use pt (point = 1/72 of an inch), in (inch) and mm (millimeter).
Since all of these units are based on actual physical size, your UI elements will retain the same physical size on any device.

Categories

Resources