Android主屏设置child.setvisibility(View.Visible)时效果闪烁问题

发布于 2024-09-15 04:07:02 字数 1223 浏览 5 评论 0原文

我制作了一个示例应用程序来翻转视图翻转器中的不同布局。

XML 基本上是(伪代码),

<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>

在 Java 代码中,

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
}

上面的伪代码可以很好地在屏幕上拖动时在 viewflipper 内移动线性布局(就像主屏幕一样)。

问题是当我执行 nextScreen.setVisibility(View.VISIBLE) 时。当下一个屏幕设置为可见时,它会在移动到适当位置之前在屏幕上闪烁。 (我猜它在 0 位置可见。)

有没有办法加载下一个屏幕而不使其在屏幕上闪烁?我希望它从屏幕加载(使其可见),这样它就不会闪烁。

非常感谢您的时间和帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许仙没带伞 2024-09-22 04:07:02

+1。我遇到了完全相同的问题。我尝试切换layout()和setVisible()调用没有效果。

更新:
事实证明,问题在于设置下一个屏幕视图的可见性的正确顺序。如果您在调用layout()之前将可见性设置为VISIBLE,您会发现位置0处出现闪烁。但是如果你先调用layout(),它会被忽略,因为可见性消失了。我做了两件事来解决这个问题:

  1. 在第一次layout()调用之前将可见性设置为INVISIBLE。这与 GONE 的不同之处在于,layout() 被执行了——你只是看不到它。
  2. 异步将可见性设置为 VISIBLE,因此首先处理布局()和相关消息

在代码中:

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);
    }
}

欢迎更优雅/更简单的解决方案!

+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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文