排除UI线程上的延迟

时间:2016-03-19 05:39:17

标签: java android

我从Android SurfaceView的onTouchEvent调用此函数。函数pushGreenText是我编写的一个自定义函数,它将一个字符串推送到一个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( );
                    }
                }
            }

1 个答案:

答案 0 :(得分:1)

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

}