2

当我将绘画笔触大小设置为 15 时,它适合一个屏幕,而在另一个屏幕上它太大或太小,我怎么知道屏幕密度以让它设置笔触宽度以适应屏幕密度?

我正在尝试将它用于动态壁纸、服务而不是应用程序,所以我无法使用获取 Windows 管理器...

4

2 回答 2

2

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.

于 2012-07-11T21:43:15.210 回答
1

这篇文章可能会帮助你:

http://developer.android.com/guide/practices/screens_support.html

最终,您将不得不针对不同的屏幕密度进行开发。

于 2012-07-11T21:33:24.393 回答