2012-07-11 84 views

回答

1

我試圖用它的動態壁紙,一個服務,而不是一個應用程序,所以我不能使用get窗口管理器...

你的意思是活動。即使是動態壁紙服務仍然被定義爲應用程序內部的組件,但無論如何...

最好的方法是將您的維度放入資源中,讓資源框架爲您做數學運算。定義一個res/values/dimens.xml文件看起來像這樣:

<resources> 
    <dimen android:name="strokeWidth">15dp</dimen> 
</resources> 

然後裏面你Service您可以撥打:

Resources res = getResources(); 
float strokeWidth = res.getDimension(R.dimen.strokeWidth); 

這將返回比例爲屏幕密度值,因爲尺寸在定義「 DP「單位。所以它將是15 @ MDPI,[email protected]等等。如果您希望返回值爲int,您也可以使用getDimensionPixelOffset()getDimensionPixelSize()

+0

我希望我也能接受這個答案,謝謝我也會使用它:) – 2012-07-11 21:58:28