2010-12-20 26 views
13

我正在創建一個1x1小部件,無論我嘗試什麼,我都無法獲得背景圖像,看起來不錯而且清脆。我已經閱讀過任何我能找到的資源,但我仍然無法獲勝。如何爲1x1 Android小部件創建清晰的背景圖像?

我正在爲HTC Desire/Nexus 1設計,並希望有人在Photoshop中創建背景時告訴我,使用什麼dpi /高度/寬度(當前使用72/100/80)。我會擔心其他設備的解決方案,一旦我能夠首先在我的測試設備上看起來不錯。

此外,如果有什麼特別的,我需要放在@ layout/main.xml和Widget_Provider.xml文件中。我根本無法找到1x1的小工具任何的例子,所以有以下幾點:

main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/widget" 
android:layout_width="fill_parent" 
android:orientation="vertical" 
android:background="@drawable/background" 
android:layout_gravity="center" 
android:layout_height="wrap_content"> 

Widget_Provider.xml

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
android:minWidth="72dip" 
android:minHeight="72dip" 
android:updatePeriodMillis="6000000" 
android:initialLayout="@layout/main" 
/> 

任何幫助將不勝感激。

回答

18

您可能想看看Google的Supporting Multiple Screen Sizes文檔。基本上這裏發生的事情是Android設備上的屏幕具有不同的像素密度。這些分爲低,中,高(ldpi,mdpi,hdpi)。如果一個資產不足以容納更大密度的屏幕,它會被炸成適當的大小 - 這可能是你正在發生的事情。

Nexus One的DPI大約在250左右,將其放入hdpi類中。使用谷歌公式(單元格數量* 74) - 2來計算您的1x1小部件的dp會使小部件的尺寸爲72x72 dp。

從DP到像素的轉換:

pixels = dp * (density/160) 

所以對於一個最高72x72 DP圖像,基於密度相應的圖像尺寸是:

ldpi (120 DPI) = 72 * (120/160) == 54 x 54 pixels 
mdpi (160 DPI) = 72 * (160/160) == 72 x 72 pixels 
hdpi (240 DPI) = 72 * (240/160) == 108 x 108 pixels 
xhdpi (320 DPI) = 72 * (320/160) == 144 x 144 pixels 

使用這些公式來創建您的資產你應該得到清晰的圖像。

+3

感謝您的解釋。出於某種原因,Google文檔並沒有爲我提供幫助....我把你的公式寫成了一個快速的webapp/calculator,以便於重複使用:http://android.npike.net/widgetcalculator/ – NPike 2011-03-21 22:24:55