2011-11-15 14 views
38

アクティビティ内のレイアウトのサイズを変更したい。ここでプログラムによるレイアウトのサイズ変更(アニメーションとして)

は、メインXMLのコードです:

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:id="@+id/top" 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" 
     android:background="#3ee3e3" > 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/middle" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1"> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/bottom" 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" 
     android:background="#fe51e6" > 
    </LinearLayout> 
</LinearLayout> 

あなたが見ることができるように、上部と下部のレイアウトの高さのは0であり、中央の レイアウトはすべての場所をカバーしています。

すべてのレイアウトが同じ高さになるまで、中間のレイアウトサイズをプログラムで減らして、上部の と下部レイアウトサイズの両方を増やしたいとします。

アニメーションのように見えます。

どうすればよいですか?

おかげ

答えて

91

は、私は同様の目的のためにResizeAnimationを書きました。シンプルだがコストがかかる。ハニカム(アンドロイド3.0)で

/** 
* an animation for resizing the view. 
*/ 
public class ResizeAnimation extends Animation { 
    private View mView; 
    private float mToHeight; 
    private float mFromHeight; 

    private float mToWidth; 
    private float mFromWidth; 

    public ResizeAnimation(View v, float fromWidth, float fromHeight, float toWidth, float toHeight) { 
     mToHeight = toHeight; 
     mToWidth = toWidth; 
     mFromHeight = fromHeight; 
     mFromWidth = fromWidth; 
     mView = v; 
     setDuration(300); 
    } 

    @Override 
    protected void applyTransformation(float interpolatedTime, Transformation t) { 
     float height = 
       (mToHeight - mFromHeight) * interpolatedTime + mFromHeight; 
     float width = (mToWidth - mFromWidth) * interpolatedTime + mFromWidth; 
     LayoutParams p = mView.getLayoutParams(); 
     p.height = (int) height; 
     p.width = (int) width; 
     mView.requestLayout(); 
    } 
} 
+1

私はこれが素晴らしい[解決策]だと思います(http://stackoverflow.com/a/13381228/1276636)。 – Sufian

+1

私は 'public void initialize'と' public boolean willChangeBounds'をオーバーライドする必要があると思います。 –

+0

ありがとうございました! –

4

スムーズなアニメーションのためのAnimatorObjectAnimatorクラスがあります。

バウンス補間とビューグループ(のLinearLayout)の動きをアニメーション化する方法についてそれはhere

例を読みます。

BounceInterpolator bounceInterpolator = new BounceInterpolator(); 
ObjectAnimator anim = ObjectAnimator.ofFloat(myViewGroup, "translationY", 0f, -200); 
anim.setInterpolator(bounceInterpolator); 
anim.setDuration(1100).start(); 

これは、バウンス効果を伴う滑らかなアニメーションをトリガし、ハニカムの前にアニメーションのようなビューを実際に移動させます。ドキュメントから:

以前のアニメーションでは、ターゲットオブジェクトの外観が変更されました...しかし、実際にオブジェクト自体は変更されませんでした。

+1

これらのオブジェクト@ddcoyにいくつかのコードサンプルまたはより多くの情報を投稿できますか? –

関連する問題