2013-05-20 16 views
14

Potrzebuję programowo zmienić szerokość RelativeLayout z 0 na 600px, w animowany sposób. Używam następującego kodu:Animowana zmiana szerokości Androida RelativeLayout

Animation a = new Animation() { 
    @Override 
    protected void applyTransformation(float interpolatedTime, Transformation t) { 
     RelativeLayout.LayoutParams drawerParams = (LayoutParams) drawer.getLayoutParams(); 
     drawerParams.width = 600; 
     drawer.setLayoutParams(drawerParams); 
    } 
}; 

a.setDuration(1000); //Animate for 1s 
layout.startAnimation(a); 

Jednak z jakiegoś powodu to nie działa. Czy ktoś może wskazać mi właściwy kierunek?

Odpowiedz

40

Właśnie to wpisane w górę i działa idealnie tutaj

Animacja

public class ResizeWidthAnimation extends Animation { 
    private int mWidth; 
    private int mStartWidth; 
    private View mView; 

    public ResizeWidthAnimation(View view, int width) { 
     mView = view; 
     mWidth = width; 
     mStartWidth = view.getWidth(); 
    } 

    @Override 
    protected void applyTransformation(float interpolatedTime, Transformation t) { 
     int newWidth = mStartWidth + (int) ((mWidth - mStartWidth) * interpolatedTime); 

     mView.getLayoutParams().width = newWidth; 
     mView.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; 
    } 
} 

Wykorzystanie

if (animate) { 
    ResizeWidthAnimation anim = new ResizeWidthAnimation(leftFrame, leftFragmentWidthPx); 
    anim.setDuration(500); 
    leftFrame.startAnimation(anim); 
} else { 
    this.leftFragmentWidthPx = leftFragmentWidthPx; 
    LayoutParams lp = (LayoutParams) leftFrame.getLayoutParams(); 
    lp.width = leftFragmentWidthPx; 
    leftFrame.setLayoutParams(lp); 
} 
+2

to doskonale działa. dziękuję – beginners

+0

Czy to tylko ja lub zmiana czasu nie ma żadnego efektu? – Zyoo

+0

Czasem działa, czasami nie. Czy to tylko ja :) – Karacago

0

Zamiast setLayoutParams(), po zmianie szerokości zadzwoń pod numer requestLayout().

+0

Czy robię to poza metodą applyTransformation lub w środku? – Wakka02

+0

To nadal nie działa ... – Wakka02

+0

oczywiście to nie działa ... to jak ustawić rozmiary układu. int newWidth = originalWidth + (int) ((this.newWidth - originalWidth) * interpolatedTime); LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams (newWidth, this.newHeight); mView.setLayoutParams (parms); mView.requestLayout(); – Karoly

0

To co pracował dla mnie:

public class CloseImageFromRightToLeft extends Animation { 
    private final int newWidth; 
    private final int newHeight; 
    private final int originalWidth; 
    private ImageView mView; 

    public CloseImageFromRightToLeft(ImageView v, int[] widthAndHeight) { 
     mView = v; 
     this.newWidth = widthAndHeight[0]/3; 
     this.originalWidth = widthAndHeight[0]; 
     this.newHeight = widthAndHeight[1]; 
    } 

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

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

    @Override 
    public void applyTransformation(float interpolatedTime, Transformation t) { 
     int newWidth = originalWidth + (int) ((this.newWidth - originalWidth) * interpolatedTime); 


     LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(newWidth, this.newHeight); 
     mView.setLayoutParams(parms); 
     mView.requestLayout(); 
    } 
} 
Powiązane problemy