2010-08-23 186 views
1

我做了一个示例应用程序来翻转viewflipper中的不同布局。Android的主屏幕像设置child.setvisibility(View.Visible)效果闪烁的问题

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 linearlayouts屏幕)。

问题是,当我做nextScreen.setVisibility(View.VISIBLE)。当下一个屏幕被设置为可见时,它会在屏幕上闪烁,然后移动到合适的位置。 (我想它是在0位置可见的。)

有没有办法加载下一个屏幕而不会在屏幕上闪烁?我想让它在屏幕之外加载(显示),以免闪烁。

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

回答

3

+1。我有完全相同的问题。我尝试将layout()和setVisible()调用切换为无效。

更新: 问题原来是设置nextScreen视图可见性的正确顺序。如果您在调用布局()之前将可见性设置为可见,则会在您注意到的位置0处出现闪烁。但是,如果您先调用layout(),则会因为可见性为GONE而被忽略。我做了两件事来解决这个问题:

  1. 在第一次调用layout()之前,将可见性设置为INVISIBLE。这与GONE的不同之处在于layout()被执行 - 你只是没有看到它。
  2. 设置能见度可见异步,所以布局()和相关信息的处理首先

在代码:

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

更优雅/更容易的解决方案,欢迎!