2017-04-07 44 views
1

如何在點擊時將視圖的高度更改爲match_parent?設置高度視圖以配合父母與動畫在Android中

public class ResizeAnimation extends Animation { 
    final int startHeight; 
    final int targetHeight; 
    private final boolean isOpen; 
    View view; 

    public ResizeAnimation(View view, int height, boolean isOpen) { 
     this.view = view; 
     this.targetHeight = height; 
     this.isOpen = isOpen; 
     startHeight = view.getHeight(); 
    } 

    @Override 
    protected void applyTransformation(float interpolatedTime, Transformation t) { 
     int newHeight; 
     if (isOpen) { 
      newHeight = (int) (startHeight + (targetHeight - startHeight) * interpolatedTime); 
     } else { 
      newHeight = (int) (startHeight + targetHeight * interpolatedTime); 
     } 
     view.getLayoutParams().height = newHeight; 
     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; 
    } 
} 

ResizeAnimation resizeAnimation = new ResizeAnimation(view, MATCH_PARENT, false); 
resizeAnimation.setDuration(500); 
view.startAnimation(resizeAnimation); 

回答

0

,因爲你逝去的View.MATCH_PARENT動畫不工作(哪個值-1)作爲目標高度。引用文檔:

INT MATCH_PARENT [...] 常數值:-1(爲0xffffffff)

你要通過真正的目標高度。您可以在渲染完成後在父佈局中測量未來的目標高度(我建議您使用ViewTreeObserver.onGlobalLayout())。