2014-07-25 21 views

Trả lời

18

Heey Amigo,

Sau khi kiểm tra mã của tôi, tôi thấy một vấn đề nhỏ. Bởi vì tôi đã sử dụng "scaleY" nó chỉ "kéo dài" chế độ xem. Điều đó có nghĩa là nếu có một số văn bản hoặc một cái gì đó trong xem, nó sẽ chỉ kéo dài nó và sẽ không nhìn tốt đẹp. Hãy thử với ValueAnimator thay vào đó, các công trình của nó mịn hơn

public void onClick(View v) 
{ 
    if(!isBig){ 
     ValueAnimator va = ValueAnimator.ofInt(100, 200); 
     va.setDuration(400); 
     va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
      public void onAnimationUpdate(ValueAnimator animation) { 
       Integer value = (Integer) animation.getAnimatedValue(); 
       v.getLayoutParams().height = value.intValue(); 
       v.requestLayout(); 
      } 
     }); 
     va.start(); 
     isBig = true; 
    } 
    else{ 
     ValueAnimator va = ValueAnimator.ofInt(200, 100); 
     va.setDuration(400); 
     va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
      public void onAnimationUpdate(ValueAnimator animation) { 
       Integer value = (Integer) animation.getAnimatedValue(); 
       v.getLayoutParams().height = value.intValue(); 
       v.requestLayout(); 
      } 
     }); 
     va.start(); 
     isBig = false; 
    } 
} 

XML:

<RelativeLayout 
    android:layout_width="150dp" 
    android:layout_height="100dp" 
    android:layout_centerHorizontal="true" 
    android:background="@android:color/holo_red_dark" 
    android:onClick="onButtonClick" 
    android:clickable="true"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:text="My Layout"/> 
</RelativeLayout> 

Old trả lời Bạn có thể sử dụng ObjectAnimator, chỉ hãy nhớ để thiết lập pivotY(0) vì vậy nó chỉ di chuyển trên đáy. Chơi với chính mình để phù hợp với nhu cầu của bạn :)

private boolean isBig = false; 

... 

public void onClick(View v) 
{ 
    v.setPivotY(0f); 
    if(!isBig){ 
     ObjectAnimator scaleY = ObjectAnimator.ofFloat(v, "scaleY", 2f); 
     scaleY.setInterpolator(new DecelerateInterpolator()); 
     scaleY.start(); 
     isBig = true; 
    } 
    else{ 
     ObjectAnimator scaleY = ObjectAnimator.ofFloat(v, "scaleY", 1f); 
     scaleY.setInterpolator(new DecelerateInterpolator()); 
     scaleY.start(); 
     isBig = false; 
    } 
} 
+0

tnx bebé ......;) –

+0

Tôi đã cập nhật câu trả lời. Thay vào đó, hãy sử dụng 'ValueAnimator', nó sẽ hoạt động tốt hơn nếu bạn có một số văn bản hoặc nội dung nào khác bên trong chế độ xem/bố cục –

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