loop and delay problem

若如初见. 提交于 2019-12-12 05:29:34

问题


I am trying to make a little application that simulates the dice. I want the picture of the dice side randomly change 6 times after clicking the button. It should randomly change 6 times with 0,3 second delay after every random change. The problem is that it changes always only one time not six times as wished. I guess it will be just some trivial mistake but I could not find the right answer anywhere on the web. Here is the code and I thank u all in advance:

   import java.util.Random;

import android.app.Activity;
import android.os.*;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class OneDice extends Activity{

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.onedicelayout);


        final ImageView image = (ImageView) findViewById(R.id.OneDiceRollImage);
        final int[] arrayOfDices = {
                R.drawable.icon,
                R.drawable.icon2,
                R.drawable.icon3,
        };
        final Random rand = new Random();

        View.OnClickListener rollOneDiceListener = new View.OnClickListener() {

            @Override
            public void onClick(View v){
                int j = 0;
                for(int i = 0; i<6; i++){
                    try {
                        j = rand.nextInt(3);
                        image.setImageResource(arrayOfDices[j]);
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        Button oneDiceRollButton = (Button) findViewById(R.id.OneDiceRollButton);
        oneDiceRollButton.setOnClickListener(rollOneDiceListener);

    }

}

回答1:


The onClick method that circles through your dice images is a callback from the system. Meaning that the UI thread which calls the callback will only operate again when the method returns. And the system is not recording what your are doing internally, but just taking the last state (i.e. the last selected image).

You can solve this with an AsyncTask where you have doInBackground() (pseudo code):

for (int i = 0; i< 6 ; i++) {
   publishProgress(randomNumber);
   Thread.sleep(300);
}

And within onProgressUpdate() you can then display the image




回答2:


I don't know it is a proper way or not but it did worked for me to show object moving step by step.

new Thread(new Runnable() {

                @Override
                public void run() {
                    for(int i = 0;i<diceNum;i++){
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                            //do your Ui task here
                                                    }
                        });

                        try {

                            Thread.sleep(500);

                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
//              }

                }
            }).start();


来源:https://stackoverflow.com/questions/4873228/loop-and-delay-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!