2011-05-08 15 views

Respuesta

12

Solución:

private static int measureViewHeight(View view2Expand, View view2Measure) { 
    try { 
     Method m = view2Measure.getClass().getDeclaredMethod("onMeasure", int.class, int.class); 
     m.setAccessible(true); 
     m.invoke(view2Measure, 
        MeasureSpec.makeMeasureSpec(view2Expand.getWidth(), MeasureSpec.AT_MOST), 
        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); 
    } catch (Exception e) { 
     return -1; 
    } 

    int measuredHeight = view2Measure.getMeasuredHeight(); 
    return measuredHeight; 
} 

static public void expandOrCollapse(View view2Expand, View view2Measure, 
     int collapsedHeight) { 
    if (view2Expand.getHeight() < collapsedHeight) 
     return; 

    int measuredHeight = measureViewHeight(view2Expand, view2Measure); 

    if (measuredHeight < collapsedHeight) 
     measuredHeight = collapsedHeight; 

    final int startHeight = view2Expand.getHeight(); 
    final int finishHeight = startHeight <= collapsedHeight ? 
      measuredHeight : collapsedHeight; 

    view2Expand.startAnimation(new ExpandAnimation(view2Expand, startHeight, finishHeight)); 
} 

class ExpandAnimation extends Animation { 
    private final View _view; 
    private final int _startHeight; 
    private final int _finishHeight; 

    public ExpandAnimation(View view, int startHeight, int finishHeight) { 
     _view = view; 
     _startHeight = startHeight; 
     _finishHeight = finishHeight; 
     setDuration(220); 
    } 

    @Override 
    protected void applyTransformation(float interpolatedTime, Transformation t) { 
     final int newHeight = (int)((_finishHeight - _startHeight) * interpolatedTime + _startHeight); 
     _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; 
    } 
}; 
+3

le puede dar más detalle, la forma de devolución de llamada measureViewHeight() lo que es view2Expand Ver, Ver view2Measure?? – pengwang

+2

Por supuesto. En mi caso, view2Measure - es TextView. view2Expand - FrameLayout que contiene TextView. Medimos la altura completa de la vista de texto y la aplicamos a FrameLayout. FrameLayout se presenta para el efecto de desvanecimiento cuando el texto está contraído (vea http://stackoverflow.com/questions/5947758/force-fading-edge-drawing) – HighFlyer

+0

@HighFlyer ¿Puede publicar más cómo se trata de lograr todo esto? No entiendo cómo lo hizo – ingsaurabh