backgroundResource的异步任务

时间:2018-04-24 12:33:13

标签: android android-asynctask android-drawable android-thread

我有一个基于某些变量而改变的imageButton,我想让这个过程在后台发生。

我已经尝试过把它放在一个帖子里,但我还不太了解AsyncTask。

public void initGameButtonOne() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            final ImageButton gameButton1 = getView().findViewById(R.id.multiPlayerGameButton1);
            final ImageButton gameButton2 = getView().findViewById(R.id.multiPlayerGameButton2);
            final ImageButton gameButton3 = getView().findViewById(R.id.multiPlayerGameButton3);
            final ImageButton gameButton4 = getView().findViewById(R.id.multiPlayerGameButton4);
            final ImageButton gameButton5 = getView().findViewById(R.id.multiPlayerGameButton5);
            final ImageButton gameButton6 = getView().findViewById(R.id.multiPlayerGameButton6);
            final ImageButton gameButton7 = getView().findViewById(R.id.multiPlayerGameButton7);
            final ImageButton gameButton8 = getView().findViewById(R.id.multiPlayerGameButton8);
            final ImageButton gameButton9 = getView().findViewById(R.id.multiPlayerGameButton9);
            gameButton1.getBackground().setAlpha(0);
            gameButton1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (player1Turn) {
                        gameButton1.setBackgroundResource(R.drawable.gameobutton);
                        gameButton1.getLayoutParams().height = 300;
                        gameButton1.getLayoutParams().width = 300;
                        gameButton1.getBackground().setAlpha(255);
                        gameButton1.setClickable(false);
                        player1Turn = !player1Turn;
                        player1ClickedButton1 = true;
                        if (!gameButton1.isClickable() &&
                                !gameButton2.isClickable() &&
                                !gameButton3.isClickable() &&
                                !gameButton4.isClickable() &&
                                !gameButton5.isClickable() &&
                                !gameButton6.isClickable() &&
                                !gameButton7.isClickable() &&
                                !gameButton8.isClickable() &&
                                !gameButton9.isClickable()) {
                            goToPlayerDrawScreen(getView());
                        }
                        checkPlayer1WinCondition();

                    } else {
                        gameButton1.setBackgroundResource(R.drawable.gamexbutton);
                        gameButton1.getLayoutParams().height = 220;
                        gameButton1.getLayoutParams().width = 220;
                        gameButton1.getBackground().setAlpha(255);
                        gameButton1.setClickable(false);
                        player1Turn = !player1Turn;
                        player2ClickedButton1 = true;
                        checkPlayer2WinCondition();
                    }
                }
            });
        }
    }).run();
}

问题是我只想让背景资源和布局参数在后台发生,而且我不明白为什么当我已经把它放在主线程上时这会给我带来如此糟糕的表现在一个新的线程中。

因此我去了AsyncTask

1 个答案:

答案 0 :(得分:0)

你忘了启动帖子。

run()方法不创建新线程但在主线程中运行可运行实例。start()创建新线程实例。所以创建一个像这样的新背景线程

Thread thread = new Thread() {
    @Override
    public void run() {
       //your code
    }
};

thread.start();