2012-01-24 92 views
1

我有一個Activity,其中包含一個Gallery視圖。我在Gallery上定義了一個onItemSelectedListener,所以無論何時選擇一個項目,我都會將一個比例動畫應用到當前選定的視圖。聆聽者和縮放的工作方式與預期相似,除了一個問題。Android圖庫縮放中心圖像和間距問題

當我定義圖庫時,我設置了spacing屬性,但是在縮放視圖之後,間距未被正確設置。請參閱下面的圖像作爲問題的示例。

有關如何保持適當間距的任何想法?

enter image description here

下面是代碼的片段:

main.xml中

<Gallery 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/featured_gallery" 
    android:layout_width="1355px" 
    android:layout_height="490px" 
    android:layout_gravity="center" 
    android:spacing="5px" 
    android:layout_marginTop="70dp" 
    />  

feature_selected.xml

<?xml version="1.0" encoding="utf-8"?> 
<scale xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromXScale="1.0" 
    android:toXScale="1.075" 
    android:fromYScale="1.0" 
    android:toYScale="1.075" 
    android:duration="100" 
    android:pivotX="50%" 
    android:pivotY="50%" 

    android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
    android:fillAfter="true"> 
</scale> 

onItemSelectListener

private class FeaturedSelectListener implements AdapterView.OnItemSelectedListener { 

    private Animation grow = null; 
    private View lastView = null; 

    public FeaturedSelectListener(Context c) { 
     grow = AnimationUtils.loadAnimation(c, R.anim.featured_selected); 
    } 

    @Override 
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
     // Shrink the view that was zoomed 
     try { 
      if (null != lastView) 
       lastView.clearAnimation(); 
     } catch (Exception clear) { 
     } 

     // Zoom the new selected view 
     try { 
      view.startAnimation(grow); 
     } catch (Exception animate) { 
     } 

     // Set the last view so we can clear the animation 
     lastView = view; 

    } 

    @Override 
    public void onNothingSelected(AdapterView<?> arg0) { 
     // TODO Auto-generated method stub 

    } 

} 
+0

您是否嘗試在所選圖像上設置左右邊距?也許它可以幫助你。 – Andreas

+0

我剛剛嘗試過,但不幸的是,這沒有奏效。如果我從動畫中刪除了pivotX,則縮放視圖的左側會有間距,但不是右側。右側的項目沒有正確移動。 – Steve

+0

你好史蒂夫,我需要在圖庫中做同樣的動畫,如果你已經達到了這個目標,你可以給我發送代碼嗎? – Hasmukh

回答

1

去Android動畫是一個重大的PITA。當視圖必須「移動其他視圖」時,它幾乎不會正常工作。有時使用fill_after參數,視圖將不再響應觸摸事件。上帝知道爲什麼。

我給你兩個選擇次優:

  1. 使得前視圖更大代替,使側視圖較小。空間將增長,這可能比你現在擁有的更具吸引力

  2. 刪除fill_after,然後使用AnimationListener並使用animationEnd中的LayouParameters設置視圖大小。

祝你好運。

+0

哈哈......謝謝你告訴我我已經知道的東西...... PITA的一部分。我會嘗試這些建議。謝謝。 – Steve