2011-05-30 63 views
3

我已經實現了使用選擇器XML組件維護按鈕的狀態。例如,請參閱代碼片段。Android中按鈕的狀態維護(資源處理)?

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- Non focused states 
    --> 
    <item android:state_focused="false" android:state_selected="false" 
     android:state_pressed="false" android:drawable="@drawable/back_arrow_normal" /> 
    <item android:state_focused="false" android:state_selected="true" 
     android:state_pressed="false" android:drawable="@drawable/back_arrow_hover" /> 
    <!-- Focused states 
    --> 
    <item android:state_focused="true" android:state_selected="false" 
     android:state_pressed="false" android:drawable="@drawable/back_arrow_hover" /> 
    <item android:state_focused="true" android:state_selected="true" 
     android:state_pressed="false" android:drawable="@drawable/back_arrow_hover" /> 
    <!-- Pressed 
    --> 
    <item android:state_pressed="true" android:drawable="@drawable/back_arrow_hover" /> 
</selector> 

使用這個想法,我們應該有至少兩個圖像。如果我在一個項目中有十個按鈕,那麼我應該在可繪製的資源中有二十個圖像。爲了支持多種密度/屏幕大小的應用程序,那麼我需要60(20X3)圖像。

我有兩種想法,但我不知道如何實現它們。它們是:

  1. 我們必須定義一個方法來代替選擇器文件。
  2. 我們應該有一個圖像,我們需要像iPhone一樣改變alpha值。

是否還有其他偉大的想法是最需要的。因爲這會增加應用程序的大小。

請表達您的想法和想法。

+1

好吧,不要爲每個密度/屏幕尺寸製作新圖像,爲什麼不考慮使用NinePatch?請參閱:http://developer.android.com/reference/android/graphics/NinePatch.html – 2011-06-01 13:43:12

+0

@willytate只是ooi爲什麼我們會使用/ drawable-hdpi/etc如果9patch會調整它的大小呢?如果你想讓你的評論成爲答案,我會很感激的。D – Blundell 2011-07-09 22:48:24

+0

爲了回答我自己的問題,使用9個插圖,圖像的「角落」永遠不會變得更大,只有兩邊會拉伸。這是您將使用/ drawable-hdpi/in的場景 – Blundell 2011-07-10 12:36:34

回答

1

我想,你的按鈕的背景不是太複雜。如果它們不是,那麼你可以使用9-patch PNG,所以可繪製的數量將減少到只有一組。 9貼片PNG將根據需要進行調整和縮放。

但是,如果背景複雜,則需要每個分辨率的多個資源。