Android Home Screen like effect flickering problem when set child.setvisibility(View.Visible)

六眼飞鱼酱① 提交于 2019-12-01 10:11:24

问题


I have made a sample application to flip through different layouts in a viewflipper.

XML is basically (pseudo-code)

<ViewFlipper>
<LinearLayout><TextView text:"this is the first page" /></LinearLayout>
<LinearLayout><TextView text:"this is the second page" /></LinearLayout>
<LinearLayout><TextView text:"this is the third page" /></LinearLayout>
</ViewFlipper>

And in Java code,

public boolean onTouchEvent(MotionEvent event)
case MotionEvent.ACTION_DOWN {
   oldTouchValue = event.getX()
} case MotionEvent.ACTION_UP {
   //depending on Direction, do viewFlipper.showPrevious or viewFlipper.showNext
   //after setting appropriate animations with appropriate start/end locations
} case MotionEvent.ACTION_MOVE {
   //Depending on the direction
   nextScreen.setVisibility(View.Visible)
   nextScreen.layout(l, t, r, b) // l computed appropriately
   CurrentScreen.layout(l2, t2, r2, b2) // l2 computed appropriately
}

Above pseudo code works well moving linearlayouts inside viewflipper when dragging on the screen (just like the home screen).

The problem is when I do nextScreen.setVisibility(View.VISIBLE). When the next screen is set visible, it flickers on the screen before moving to its appropriate position. (I guess it is made visible at 0 location.)

Is there a way to load the next screen without making it flicker on the screen? I want it to be loaded (made visible) out of screen so it doesn't flicker.

Thank you very much for your time and help!


回答1:


+1. I'm having the exact same problem. I tried switching the layout() and setVisible() calls to no effect.

Update: The problem turns out to be the correct sequence in setting the visibility of the nextScreen view. If you set the visibility to VISIBLE before calling layout(), you get the flicker at position 0 as you noticed. But if you call layout() first, it gets ignored because the visibility is GONE. I did two things to fix this:

  1. Set the visibility to INVISIBLE before first layout() call. This differs from GONE in that the layout() is executed - you just don't see it.
  2. Set the visibility to VISIBLE asynchronously, so the layout() and related messages are processed first

In code:

case MotionEvent.ACTION_DOWN:
    nextScreen.setVisibility(View.INVISIBLE); //from View.GONE

case MotionEvent.ACTION_MOVE:
    nextScreen.layout(l, t, r, b);
    if (nextScreen.getVisibility() != View.VISIBLE) {
    //the view is not visible, so send a message to make it so
    mHandler.sendMessage(Message.obtain(mHandler, 0));
}

private class ViewHandler extends Handler {

    @Override
    public void handleMessage(Message msg) {
        nextScreen.setVisibility(View.VISIBLE);
    }
}

More elegant/easier solutions are welcome!



来源:https://stackoverflow.com/questions/3544826/android-home-screen-like-effect-flickering-problem-when-set-child-setvisibility

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!