2014-01-07 4 views
8

拡大表示のアニメーションを使用してビューグループにビューを追加したいので、追加ビューが非常に小さくなり、フルサイズに達するまでプロセス内の他のビューを移動する)。Android:拡張アニメーションを含むビューを追加する(点滅なし)

さまざまなアプローチを試した後、私は以下の解決策を考え出しました。それがあなたを助けるか、より良い選択肢を投稿してください、投票してください。私はこれを行う簡単な方法がなければならないと確信しています。

詳しくはthis onethis oneのような投稿を参照してください。ここで

は、私はビューを追加して、アニメーションを開始方法は次のとおりです。

// Trick: set height to 1 so the view doesn't take its full size at the beginning. 
// (0 doesn't work, I don't know why) 
container.addView(view, LayoutParams.MATCH_PARENT, 1); 
Animation expansion = createExpansion(view); 
expansion.setDuration(200); 
view.startAnimation(expansion); 

createExpansion()方法:ところで

/** 
* Like {@link ScaleAnimation} for height scaling that also resizes the view accordingly, 
* so it takes the right amount of space while scaling. 
*/ 
public class SizedHeightScaleAnimation extends ScaleAnimation { 

    private float fromHeight; 
    private float toHeight; 
    private View viewToScale; 

    public SizedHeightScaleAnimation(View viewToScale, float fromY, float toY) { 
     super(1, 1, fromY, toY); 
     init(viewToScale, fromY, toY); 
    } 

    public SizedHeightScaleAnimation(View viewToScale, float fromY, float toY, float pivotX, float pivotY) { 
     super(1, 1, fromY, toY, pivotX, pivotY); 
     init(viewToScale, fromY, toY); 
    } 

    public SizedHeightScaleAnimation(View viewToScale, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) { 
     super(1, 1, fromY, toY, pivotXType, pivotXValue, pivotYType, pivotYValue); 
     init(viewToScale, fromY, toY); 
    } 

    private void init(View viewToScale, float fromY, float toY) { 

     this.viewToScale = viewToScale; 
     viewToScale.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
     fromHeight = viewToScale.getMeasuredHeight() * fromY; 
     toHeight = viewToScale.getMeasuredHeight() * toY; 
    } 

    @Override 
    protected void applyTransformation(float interpolatedTime, Transformation t) { 
     super.applyTransformation(interpolatedTime, t); 

     final int newHeight = (int) (fromHeight * (1 - interpolatedTime) + toHeight * interpolatedTime); 

     viewToScale.getLayoutParams().height = newHeight; 
     viewToScale.requestLayout(); 
    } 
} 

public static Animation createExpansion(View view) { 

    return new SizedHeightScaleAnimation(view, 0, 1, 
      Animation.RELATIVE_TO_SELF, 0f, 
      Animation.RELATIVE_TO_SELF, 0f); 
} 

そして最後にSizedHeightScaleAnimationアニメーションクラス次のように折り畳み効果を作成することができます:

public static Animation createCollapse(View view) { 

    return new SizedHeightScaleAnimation(view, 1, 0, 
      Animation.RELATIVE_TO_SELF, 0f, 
      Animation.RELATIVE_TO_SELF, 0f); 
} 

あなたがそれを使用する場合、あなたはおそらくアニメーションが行われたときに親からビューを削除することをお勧めします:

Animation collapse = createCollapse(view); 
collapse.setDuration(200); 

collapse.setAnimationListener(new AnimationListener() { 
    @Override 
    public void onAnimationEnd(Animation animation) { 
     final ViewGroup parent = (ViewGroup) view.getParent(); 
     parent.removeView(view); 
    } 

    ... other overrides ... 
}); 

view.startAnimation(collapse); 

答えて

5

これは少し単純かもしれません:

public void expand(final View view) { 
    view.getLayoutParams().height = 0; 

    Animation a = new Animation() { 
     @Override 
     protected void applyTransformation(float interpolatedTime, Transformation t) 
     { 
      view.getLayoutParams().height = (int)(mTargetHeight * interpolatedTime); 
      view.requestLayout(); 
     } 

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

    a.setDuration(1000); 
    view.startAnimation(a); 
} 
関連する問題