2012-02-13 36 views
1

嗨我借用Seth's方法找到here來動畫我的viewGroups。它很好用,但方向不對 - 這是我第一次使用/擴展Animation類,並且我不知道如何減小我的視圖的大小 - 這擴展了它。如何設置我的視圖LayoutParams寬度更小?

以下是課程以及我如何使用它。任何幫助將不勝感激。

public class ContainerAnim extends Animation { 
    int targetWidth; 
    View view; 
    boolean opened; 

    public ContainerAnim(View v, int targetWidth, boolean opened) { 
     this.view = v; 
     this.targetWidth = targetWidth; 
     this.opened = opened; 
    } 

    @Override 
    protected void applyTransformation(float interpolatedTime, Transformation t) { 
     int newWidth; 
     if (opened) { 
      newWidth = (int) (targetWidth * interpolatedTime); 
     } else { 
      newWidth = (int) (targetWidth * (1 - interpolatedTime)); 
     } 
     view.getLayoutParams().width = newWidth; 
     view.requestLayout(); 
    } 

    @Override 
    public void initialize(int width, int height, int parentWidth, 
      int parentHeight) { 
     super.initialize(width, height, parentWidth, parentHeight); 
    } 

    @Override 
    public boolean willChangeBounds() { 
     return true; 
    } 
} 

用法:

... case R.id.menu_shrink: 
      FragmentTransaction ft = getFragmentManager().beginTransaction(); 
      FrameLayout frame = (FrameLayout) findViewById(R.id.listFragment_container); 

      ContainerAnim set = new ContainerAnim(frame, 100, true); 
      set.setDuration(200); 
      LayoutAnimationController c = new LayoutAnimationController(set, 
        0.25f); 
      frame.setLayoutAnimation(c); 
      frame.startLayoutAnimation(); 

      ft.commit(); 
... 
+0

在應用轉換時將(targetWidth * interpolatedTime)更改爲(targetWidth * interpolatedTime)時發生了什麼? – 2012-02-13 21:35:55

+0

@Paul Nikonowicz視圖展開,然後返回到原始寬度 – CelticParser 2012-02-13 21:40:49

+0

如何製作新寬度 - = 10寬度縮小的帽子? – 2012-02-13 21:44:03

回答

0

我想你混淆了這第三個參數(布爾)的使用。你稱之爲「打開」,並將其傳遞給你,因爲你想縮小它。但是,這與我原來的代碼中的用法相反!在我的代碼中,如果視圖爲已關閉,則布爾值爲true,但我希望增長

「和d =指定方向的布爾值(true = expand,false = collapsing)。」

如果將動畫類中的條件交換爲if(!opened){},它應該可以工作。或者傳遞錯誤而不是真實。或者通過它,但將變量改爲「關閉」而不是「打開」。

-Seth

相關問題