2011-11-09 55 views
33

Tôi không thể tìm thấy một ví dụ hay về cách thực hiện việc này.Cách mở rộng chiều cao bố cục bằng hoạt ảnh?

Tôi có một bộ RelativeLayout có chiều cao x.

Tôi muốn thêm nút mở rộng chiều cao thành chiều cao x + y.

ai đó có thể giới thiệu cho tôi một ví dụ hay về cách thực hiện chương trình?

+0

Kiểm tra ở đây cho mở rộng và giải pháp sụp đổ tôi đã làm: http://stackoverflow.com/questions/20973089/android-add-view-with-expand- hoạt ảnh mà không chớp mắt –

+0

http://stackoverflow.com/a/8162779/5996106 Giải pháp tốt nhất –

Trả lời

12

Vui lòng kiểm tra bên dưới câu trả lời thay đổi nội dung mới như dưới đây. Nhưng ở đây bạn cần phải biết chính xác chiều cao mới.

public class LayoutAnimationActivity extends Activity 
{ 
    RelativeLayout ril1; 
    Button btn; 
    int initialHeight; 
    int actualHeight; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main2); 
     ril1=(RelativeLayout)findViewById(R.id.relativeLayout1); 
     btn=new Button(this); 
     btn.setWidth(100); 
     btn.setHeight(200); 
     btn.setText("Button"); 
     actualHeight=210; 
     Ani a=new Ani(); 
     a.setDuration(2000); 
     ril1.startAnimation(a); 
    } 

    class Ani extends Animation 
    { 
     public Ani() { 
     } 

     @Override 
     protected void applyTransformation(float interpolatedTime, Transformation t) { 
      int newHeight; 

      newHeight = (int)(initialHeight * interpolatedTime); 

      ril1.removeAllViews(); 
      btn.setWidth(100); 
      btn.setHeight(300); 
      btn.setText("as"); 
      ril1.addView(btn);    
      ril1.getLayoutParams().height = newHeight; 
      ril1.requestLayout(); 
     } 

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

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

cũng mở rộng bố cục (Tôi đã thực hiện một số thay đổi) nhưng không có hoạt ảnh – piojo

+0

Vui lòng kiểm tra ngay..answer được chỉnh sửa – Maneesh

+1

Tôi không hiểu nút này phải làm gì với tất cả điều này. tại sao bạn removeAllView và thiết lập chiều rộng, chiều cao và văn bản mới cho nút. mã này chỉ mở rộng bố cục, làm cách nào để thu gọn nó? – piojo

15

Bạn cần một hình ảnh động quy mô here là tài liệu chính thức

này là trong mã

private void animate() { 
    ImageView imageView = (ImageView) findViewById(R.id.ImageView01); 
    ScaleAnimation scale = new ScaleAnimation((float)1.0, (float)1.5, (float)1.0, (float)1.5); 
    scale.setFillAfter(true); 
    scale.setDuration(500); 
    imageView.startAnimation(scale); 
} 
+0

mở rộng quy mô? tôi nghĩ tôi cần dịch. Tôi đang điều chỉnh LayoutParams, bạn có chắc tôi cần mở rộng không? – piojo

+2

Nếu bạn muốn tăng kích thước bạn cần mở rộng. Nếu bạn chỉ muốn di chuyển những thứ bạn cần dịch. – Janusz

+0

Được rồi, nhưng tôi cần một hoạt ảnh có kích thước bắt đầu chiều cao và chiều cao kết thúc và không phải là bao nhiêu để mở rộng quy mô – piojo

45

Bạn đã đánh dấu giải pháp gần nhất. Đây là giải pháp chính xác. Tôi đã từng gặp vấn đề tương tự. Hy vọng câu trả lời này sẽ giúp người khác.

nhanh chóng ResizeAnimation

ResizeAnimation resizeAnimation = new ResizeAnimation(
    view, 
    targetHeight, 
    startHeight 
); 
resizeAnimation.setDuration(duration); 
view.startAnimation(resizeAnimation); 

ResizeAnimation lớp nên trông như thế này

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

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

    @Override 
    protected void applyTransformation(float interpolatedTime, Transformation t) { 
     int newHeight = (int) (startHeight + targetHeight * interpolatedTime); 
     //to support decent animation, change new heigt as Nico S. recommended in comments 
     //int newHeight = (int) (startHeight+(targetHeight - startHeight) * 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; 
    } 
} 
+17

Cảm ơn bạn! Tôi vừa sửa đổi tính toán mớiHeight để hỗ trợ hoạt hình chiều cao gốc cũng! int newHeight = (int) (startHeight + (targetHeight - startHeight) * interpolatedTime); –

+0

Hoạt động như một sự quyến rũ. Cảm ơn bạn. – GrafOrlov

+0

Nó không làm cho quá trình chuyển đổi trượt – iYonatan

2

Hai cách đơn giản để làm điều này mà không cần một lớp Animation:

1) Thiết lập android:animateLayoutChanges="true" trong bạn bố trí tập tin xml

2) Sử dụng một ViewProperty phim hoạt hình

layout.setPivot(0); 
layout.animate().scaleY(scaleFactor).setDuration(500); 

Trục kể xem nơi để mở rộng quy mô từ, mặc định là ở giữa, mà theo kinh nghiệm của tôi là gần như không bao giờ những gì bạn muốn. Thời lượng là tùy chọn (mặc định = 1000).

1
final Button button1 = (Button) view.findViewById(R.id.button); 
    final CollapseAnimator animator = new CollapseAnimator(topLayout); 

    final ViewTreeObserver.OnGlobalLayoutListener listener = new ViewTreeObserver.OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 
      int mHeight = button1.getMeasuredHeight(); 
      KLog.i("onGlobalLayout() mHeight:" + mHeight); 
      animator.setValues(mHeight*2, mHeight); 

     } 
    }; 
    button1.getViewTreeObserver().addOnGlobalLayoutListener(listener); 
    button1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      view.post(new Runnable() { 
       @Override 
       public void run() { 
        button1.getViewTreeObserver().removeOnGlobalLayoutListener(listener); 
        animator.collapse(); 

       } 
      }); 

     } 
    }); 

và lớp

public class CollapseAnimator { 
    private View view; 
    private boolean collapse=true; 
    private int duation=300; 
    private int destHeight=300; 
    private ValueAnimator animator; 
    private int originHeight=0; 
    private int from=0; 
    private int to=0; 


    public CollapseAnimator(View view) { 
     this.view = view; 
    } 

    public void setValues(int destHeight,int originHeight){ 
     this.destHeight = destHeight; 
     this.originHeight=originHeight; 
     from=originHeight; 
     to=originHeight; 
    } 
    public void collapse(){ 


     from=to; 
     if(collapse){ 

      to=destHeight; 
      collapse=false; 
     }else{ 
      to=originHeight; 
      collapse=true; 
     } 
     KLog.i("from:" + from+",to:"+to); 
     animator = ValueAnimator.ofInt(from, to); 
     animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
      @Override 
      public void onAnimationUpdate(ValueAnimator valueAnimator) { 
       int val = (Integer) valueAnimator.getAnimatedValue(); 
       ViewGroup.LayoutParams layoutParams = view.getLayoutParams(); 
       layoutParams.height = val; 
       view.setLayoutParams(layoutParams); 
      } 
     }); 
     animator.setDuration(duation); 
     animator.start(); 
    } 
} 
Các vấn đề liên quan