开发者

How do I force a ViewGroup to draw off screen?

开发者 https://www.devze.com 2023-03-01 16:18 出处:网络
I have several LinearLayouts that get filled with downloaded images or text within a ScrollView.The LinearLayouts have a LayoutAnimation applied to them, so each one \"slides\" into place when drawn.I

I have several LinearLayouts that get filled with downloaded images or text within a ScrollView. The LinearLayouts have a LayoutAnimation applied to them, so each one "slides" into place when drawn. Is there a way to force the offscreen LinearLayouts to draw so that by the time the user scrolls to them, the animation has already completed? I've tried measuring each view like so: (container is the ViewGroup)

int measuredWidth = View.MeasureSpec.makeMeasureSpec(LayoutParams.FILL_PARENT, View.MeasureSpec.AT_MOST); 
int measuredHeight = View.MeasureSpec.makeMeasureSpec(LayoutParams.WRAP_CONTENT, View.MeasureSpec.UNSPECIFIED); 
container.measure(measuredWidth, measuredHeight); 
container.layout(0, 0, container.getMeasuredWidth(), container.getMeasuredHeight());
container.requestLayout();

But they still won't draw until they appear on screen during scrolling (which normally is fine but the animation makes it.. er,开发者_StackOverflow not fine)


If you don't want to run the animation why don't you simply remove the animation? The framework will apply the animation because you tells it to.

Also note that none of your code causes a redraw. To draw you need to call invalidate() or draw().


For any future readers, here's what I ended up doing: I subclassed LinearLayout and overrode onLayout to only apply animation if the layout is currently on screen at the moment it is populated:

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom)
{
    super.onLayout(changed, left, top, right, bottom);

    // only animate if viewgroup is currently on screen
    int[] xy = new int[2];
    this.getLocationOnScreen(xy);
    int yPos = xy[1];

    if (yPos < availableScreenHeight && bottom > 200)
    {
        Animation slide_down = AnimationUtils.loadAnimation(getContext(), R.anim.container_slide_down);
        LayoutAnimationController controller = new LayoutAnimationController(slide_down, 0.25f);
        this.setLayoutAnimation(controller);
    }
}

This actually saves some cycles since I'm not applying animation across the board then removing it from views that don't need it. (BTW "availableScreenHeight" is just that, and "200" is simply a threshold that I know a populated view will never be smaller than. Your case may vary.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号