2013-07-28 67 views
0

我的問題的區域如下:不考慮背景的計算項目

我有一個ImageView與背景。背景是可繪製的,根據狀態繪製不同的圖像。

context_menu_button.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

<!-- Focused --> 
<item android:drawable="@drawable/button_round_focused" 
    android:state_focused="true"/> 

<!-- Pressed --> 
<item android:drawable="@drawable/button_round_focused" 
    android:state_pressed="true"/> 

<!-- Normal --> 
<item android:drawable="@android:color/transparent"/> 

</selector> 

然後,我有一個star icon這個context_menu_button應用爲背景:

<ImageView 
    android:id="@+id/btnStar" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/context_menu_button" 
    android:focusable="true" 
    android:src="@drawable/icon_star" /> 

圖像0​​其實是非常大的,因爲它是一個圖像具有非常微妙的漸變。在正常狀態下沒有圖像(透明色)。這是上面的XML的樣子(聚焦狀態):

Star icon

這種背景的大小是造成我帶來些麻煩。繪製星形圖標時,會考慮背景以計算圖標星形的面積。所以,當我將兩個或更多的圖標對齊時,它們彼此相距很遠。

<ImageView 
    android:id="@+id/btnStar" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/context_menu_button" 
    android:focusable="true" 
    android:src="@drawable/icon_star" /> 

<ImageView 
    android:id="@+id/btnShare" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/context_menu_button" 
    android:focusable="true" 
    android:src="@drawable/icon_share" /> 

<ImageView 
    android:id="@+id/btnRate" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/context_menu_button" 
    android:focusable="true" 
    android:src="@drawable/icon_rate" /> 

star-share-rate

我的解決辦法是設置左切緣陰性,他們和我們一起關閉。

star-share-rate-with-negative-margins

但是,這個方案引起了另一個問題。現在,項目重疊:現在

star-overlapped

share-overlapped

rate-overlapped

,例如,當我嘗試在項目份額單擊(中間按鈕),它實際上是第三個按鈕被點擊,因爲它們重疊。

我的理想解決方案是應用於ImageView的背景沒有被考慮到,以計算它應用到的項目的面積。事情是這樣的:

star-with-background-out-of-bounds

換句話說,背景可能走出ImageView的面積。

我認爲這可以解決問題,但我一直在閱讀文檔,我找不到與此相關的任何內容。任何其他的想法將不勝感激(雖然改變背景的大小是不可能的)。

感謝您的幫助。

回答

0

最後,我設法解決了這個問題。我做的是以下幾點:

1)從按鈕中刪除背景。

背景是這個問題的根源。沒有背景,一切都按預期工作。

2)當它聚焦或單擊時,以編程方式顯示背後的背景和圖標。

爲了做到這一點,我做的第一件事就是改變佈局。我把所有的圖標(明星,分享,比率)都放在FrameLayout之內。我還附加了一個ImageView和可繪製的button_round_focused(上面顯示的暈圈)。當一個圖標變得焦點時,這個想法是將這個圖像移動到正確的圖標上。由於圖片位於圖標之前,因此它的Z-index較低,所以它會被繪製在圖標後面。

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="85dp" 
    android:gravity="left|center_vertical" > 

    <ImageView 
     android:id="@+id/button_round_focused" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/button_round_focused" 
     android:visibility="invisible" /> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" > 

     <!-- Favorite button --> 

     <ImageView 
      android:id="@+id/btnFav" 
      style="@style/MediaDetailsStarButton" 
      android:contentDescription="@string/star" /> 

     <!-- Share button --> 

     <ImageView 
      android:id="@+id/btnShare" 
      style="@style/MediaDetailsShareButton" 
      android:contentDescription="@string/share" /> 

     <!-- Rate button --> 

     <ImageView 
      android:id="@+id/btnRate" 
      style="@style/MediaDetailsRateButton" 
      android:contentDescription="@string/to_rate" /> 
    </LinearLayout> 

</FrameLayout> 

在重新定義佈局之後,我在必要時編程了將暈圈移動到正確位置的邏輯。當圖標聚焦時移動它相對容易。我重視的OnFocusChangeListener像這樣每一個圖標:

icon.setOnFocusChangeListener(new View.OnFocusChangeListener() { 

    @Override 
    public void onFocusChange(View v, boolean hasFocus) { 
     if (hasFocus) { 
      btnRoundFocused.setX(center(v)); 
      btnRoundFocused.setVisibility(View.VISIBLE); 
     } else { 
      btnRoundFocused.setVisibility(View.INVISIBLE); 
     } 
    } 
}); 

的方法center()計算btnRoundFocused右邊的X座標,所以這兩個圖標,背景得到居中。

private float center(View v) { 
    int middle = btnRoundFocused.getWidth()/2; 
    return v.getX() + (v.getWidth()/2) - middle; 
} 

然後,當點擊一個圖標時,我做了同樣的事情。這比較困難。我必須做的是:當一個圖標被點擊時,顯示一段時間的背景,然後隱藏它。

private void withButtonPressed(View v, IButtonAction action) { 
    buttonPressed(v); 
    action.execute(v); 
    buttonUnpressed(v); 
} 

protected void buttonPressed(View v) { 
    btnRoundFocused.setX(center(v)); 
    btnRoundFocused.setVisibility(View.VISIBLE); 
    v.setActivated(true); 
} 

protected void buttonUnpressed(final View v) { 
    new Handler().post(new Runnable() { 

     private final int ELLAPSE_TIME = 250; 

     @Override 
     public void run() { 
      try { 
       Thread.sleep(ELLAPSE_TIME); 
       v.setActivated(false); 
       btnRoundFocused.setVisibility(View.INVISIBLE); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

interface IButtonAction { 
    void execute(View v); 
} 

我所定義,其接收IButtonAction的方法withButtonPressed()IButtonAction實現通訊員圖標(星號,分享,比率)的業務邏輯。 withButtonPressed()將背景設置在正確的位置,顯示背景一段時間(250毫秒),然後再次隱藏。

最後,我還必須在圖標的可繪製(例如* icon_share.xml *)中添加一個激活的新狀態。一個圖標是一個帶有幾個可繪圖的XML,具體取決於它的狀態(當它關注或點擊時圖標爲黑色,正常狀態下圖標爲灰色)。

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <!-- Activated --> 
    <item android:drawable="@drawable/ico_share_focused" 
     android:state_activated="true"/> 

    <!-- Focused --> 
    <item android:drawable="@drawable/ico_share_focused" 
     android:state_focused="true"/> 

    <!-- Normal --> 
    <item android:drawable="@drawable/ico_share"/> 

</selector> 

就是這樣。我不知道是否有更好的方法來解決這個問題,但至少這是有效的。希望對面臨類似問題的其他人有所幫助。