Android: drawing same size line or circle on different screen densitys
Frage
When i set a paint stroke size to say 15, it fits one screen while in another it is too large or small, how can i know the screen density to let it set the stroke width to fit the screen density?
i am trying to use it for a live wallpaper, a service, not an application, so i can't use get windows manager...
Lösung
This article might help you:
http://developer.android.com/guide/practices/screens_support.html
Ultimately, you are going to have to develop for different screen densities.
Andere Tipps
i am trying to use it for a live wallpaper, a service, not an application, so i can't use get windows manager...
You mean Activity. Even a live wallpaper Service is still defined as a component inside of an application, but anyway...
The best way is to put your dimension into a resource and let the resource framework do the math for you. Define a res/values/dimens.xml
file that looks like this:
<resources>
<dimen android:name="strokeWidth">15dp</dimen>
</resources>
Then inside of your Service
you can call:
Resources res = getResources();
float strokeWidth = res.getDimension(R.dimen.strokeWidth);
This will return back a value scaled for the screen density since the dimension was defined in "dp" units. So it will be 15@MDPI, 22.5@HDPI, and so on. You can also use getDimensionPixelOffset()
or getDimensionPixelSize()
if you want the value back as an int
instead.