2014-07-25 3 views

답변

18

Heey 아미고,

내 코드를 테스트 한 후, 나는 작은 문제를 보았다. 왜냐하면 나는 "scaleY"을 사용했기 때문에 뷰를 "확장"합니다. 즉,보기에 텍스트 나 무언가가 있으면 스트레칭되어 멋지게 보일 것입니다. 대신, 그 작품을 더 부드럽게 ValueAnimator으로 시도

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> 

올드 대답 당신은 pivotY(0) 그래서 그것은 단지에 이동 설정 만 remeber의 ObjectAnimator을 사용할 수 있습니다 바닥. 그것을 당신 자신의 요구에 맞춰 직접 연주하십시오 :)

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

답변을 업데이트했습니다. 'ValueAnimator'를 대신 사용하십시오.보기/레이아웃 안에 텍스트 나 다른 것이 있으면 더 잘 작동합니다. –

관련 문제