2011-11-09 3 views
33

이렇게하는 방법에 대한 좋은 예를 찾을 수 없습니다.애니메이션을 사용하여 레이아웃 높이를 확장하는 방법은 무엇입니까?

x 높이를 기준으로 RelativeLayout을 설정했습니다.

높이를 x + y 높이로 확장하는 버튼을 추가하고 싶습니다.

나를 프로그래밍 방식으로 수행하는 방법에 대한 좋은 예를 참조 할 수 있습니까?

+0

확인 확장 및 축소 솔루션을 내가 한 : http://stackoverflow.com/questions/20973089/android-add-view-with-expand- 애니메이션없이 - 깜박임 –

+0

http://stackoverflow.com/a/8162779/5996106 최상의 솔루션 –

답변

12

있는 공식 문서

입니다 필요합니다. 하지만 여기서 정확한 높이를 알아야합니다.

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

잘 레이아웃을 확장했지만 약간의 변경이있었습니다. 애니메이션이 없습니다. – piojo

+0

지금 확인해보십시오. 정답이 편집되었습니다. – Maneesh

+1

버튼이 이걸로 무엇을 해야할지 모르겠군요. 왜 당신은 AllAllView를 제거하고 단추에 새로운 너비, 높이 및 텍스트를 설정해야합니다. 이 코드는 레이아웃 만 확장합니다. 어떻게 접 힙니 까? – piojo

15

당신은 규모 애니메이션 here 아래로 새로운 편집 답변을 아래에 확인하시기 바랍니다이 코드에서

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

스케일링? 나는 번역이 필요하다고 생각했다. LayoutParams를 조정하고 있습니다. 스케일링이 필요합니까? – piojo

+2

크기를 늘리려면 크기 조정이 필요합니다. 물건을 옮기고 싶으면 번역이 필요합니다. – Janusz

+0

괜찮 았지만, 높이 및 높이를 시작하는 스케일 애니메이션이 필요하며 축척을 얼마나 할 것인가가 – piojo

45

가장 가까운 해결책을 표시했습니다. 이것은 정확한 해결책입니다. 나는 똑같은 문제가 있었다. 바라건대이 대답은 다른 사람들을 도울 것입니다.

1) XML 레이아웃 파일에 android:animateLayoutChanges="true"을 설정합니다

인스턴스화 ResizeAnimation

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

ResizeAnimation 클래스는 Animation 클래스없이이 작업을 수행하는이

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

고가의 애니메이션을 지원하도록 newHeight 계산을 수정했습니다! int newHeight = (int) (startHeight + (targetHeight - startHeight) * interpolatedTime); –

+0

매력처럼 작동합니다. 고맙습니다. – GrafOrlov

+0

슬라이딩 트랜지션을하지 않습니다. – iYonatan

2

두 가지 간단한 방법과 같아야합니다

2) 피벗 어디, 기본값은 내 경험에서 거의 절대 당신이 원하는없는 중간에까지 확장하는보기를 알려주는 VIEWPROPERTY 애니메이터에게

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

를 사용합니다. 기간은 선택 사항입니다 (기본값 = 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(); 

       } 
      }); 

     } 
    }); 

및 클래스

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(); 
    } 
} 
관련 문제