gpt4 book ai didi

java - 解决 UI 线程延迟问题

转载 作者:行者123 更新时间:2023-12-01 10:11:34 24 4
gpt4 key购买 nike

我从 Android SurfaceView 的 onTouchEvent 调用此函数。函数pushGreenText 是我编写的一个自定义函数,它将字符串推送到EditText 中,该EditText 是SurfaceView 的XML 覆盖层的一部分。在我看来,所有工作都在 UI 线程上执行。我看到了一种对我来说很奇怪的行为。由于某种原因,我没有看到文本被推送,直到 firstRollSP 执行完毕并超出范围。我想要实现的情况是,我看到文本立即被推送,而无需等待firstRollSP的最后一个大括号完成。在代码中的所有其他地方,pushGreenText 都会立即工作,但由于某种原因,此函数中存在延迟。

static synchronized public void firstRollSP( ) throws InterruptedException {
mGame.mDice.setDie1( mGame.mDiceFirstRoll.getDie1( ) );

mGame.mDice.setRolled( true );

MainActivity.activity.pushTextGreen( Strings.get_First_roll_X_Die1( ) );

H.waitms( 2000 );


mGame.mDice.setDie2( mGame.mDiceFirstRoll.getDie2( ) );

while( mGame.mDice.getDie1( ) == mGame.mDice.getDie2( ) )
{
Random mRng = new Random( );

int dProposedDie = mRng.nextInt( 6 ) + 1;

mGame.mDice.setDie2( dProposedDie );
}

MainActivity.activity.pushTextGreen( "Android rolled " + Integer.toString( mGame.mDice.getDie2( ) ) );

H.waitms( 2000 );

if( H.initWonFirstRoll( ) )
{
MainActivity.activity.pushTextGreen( "Player won first roll." );

mGame.isFirstRoll = false;

mGame.isTurn = true;

mGameAI.isFirstRoll = false;

mGameAI.isTurn = false;

mGame.mDice.sort( );

mGame.mDice.setRolled( true );

mGame.mDice.setDiceAnimationComplete( true );

mGame.mOppDice.init( );
}
else
{
MainActivity.activity.pushTextGreen( "Android won first roll." );

mGame.isFirstRoll = false;

mGame.isTurn = false;

mGameAI.isFirstRoll = false;

mGameAI.isTurn = true;

mGameAI.mDice.init( );

mGame.mOppDice.init( );

mGame.mDice.sort( );

mGameAI.mDice.setDie1( mGame.mDice.getDie1( ) );

mGameAI.mDice.setDie2( mGame.mDice.getDie2( ) );

mGame.mOppDice.setDie1( mGame.mDice.getDie1( ) );

mGame.mOppDice.setDie2( mGame.mDice.getDie2( ) );

H.waitms( 2000 );

mGameAI.mPossibleIndexes.calcPossibleTrianglesAI( );

if( mGameAI.mPossibleIndexes.anyPossibles( ) )
{
moveWholeTurnAI( );
}
else
{
H.endTurnAI( );
}
}
}

最佳答案

static synchronized public void firstRollSP( ) throws InterruptedException {

mGame.mDice.setDie1( mGame.mDiceFirstRoll.getDie1( ) );

mGame.mDice.setRolled( true );

MainActivity.activity.pushTextGreen( Strings.get_First_roll_X_Die1( ) );
new Handler().postDelay(new Runnable(){
//your code here.
},2000 );

}

关于java - 解决 UI 线程延迟问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36098291/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com