2011-11-15 31 views
38

Tôi muốn đổi kích thước một số bố cục trong Hoạt động của mình.Thay đổi kích thước bố cục theo chương trình (dưới dạng hoạt ảnh)

Đây là mã của XML chính:

<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> 

Như bạn có thể thấy, phía trên và bố trí phía dưới chiều cao là 0, và giữa bố trí bao gồm tất cả các nơi.

Tôi muốn giảm kích thước bố cục ở giữa theo chương trình, đồng thời tăng cả hai kích thước đầu trang và bố cục dưới cùng cho đến khi tất cả bố cục có cùng chiều cao.

Tôi muốn nó trông giống như hoạt ảnh.

Tôi nên làm như thế nào?

Cảm ơn

Trả lời

91

Tôi đã viết ResizeAnimation cho mục đích tương tự. Nó đơn giản nhưng tốn kém.

/** 
* 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

Tôi nghĩ đây là một giải pháp tuyệt vời (http://stackoverflow.com/a/13381228/1276636). – Sufian

+1

Tôi đoán bạn cần ghi đè 'public void initialize' &' public boolean willChangeBounds'. –

+0

Cảm ơn bạn rất nhiều! –

4

On Honeycomb (Android 3.0) có các AnimatorObjectAnimator lớp cho hình ảnh động mượt mà hơn.

đọc nó here

Ví dụ về cách animate di chuyển của một nhóm view (LinearLayout) với một người xen vào thư bị trả lại.

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

này sẽ kích hoạt một hình ảnh động trơn tru với hiệu ứng bị trả lại và thực sự di chuyển các quan điểm không thích phim hoạt hình trước khi Honeycomb. Từ tài liệu:

Hoạt ảnh trước đã thay đổi giao diện trực quan của đối tượng mục tiêu ... nhưng chúng không thực sự thay đổi đối tượng.

+1

Bạn có thể đăng một số mẫu mã hoặc thông tin thêm về các đối tượng này không? –

Các vấn đề liên quan