vendredi 2 décembre 2016

How to create runnable to generate random number allowing it to pause/resume

Activity XML:

...
<ImageView
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:id="@+id/imageView1"
    app:srcCompat="@drawable/image1" /> <!-- replace by image_update_1 -->
<ImageView
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:id="@+id/imageView2"
    app:srcCompat="@drawable/image2" /> <!-- replace by image_update_2 -->
<ImageView
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:id="@+id/imageView2"
    app:srcCompat="@drawable/imagePause" /> <!-- replace by imagePlay -->
<TextView
    android:text=""
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textView" />
...

Acitivty Java:

...
int i = 0;
ImageView iv1 = (ImageView) findViewById(R.id.imageView1);
ImageView iv2 = (ImageView) findViewById(R.id.imageView2);
ImageView iv3 = (ImageView) findViewById(R.id.imagePause);
iv3.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (RunRandom is not on pause) {
            //pause the RunRandom runnable
            //display a toast
            //replace iv3 drawable to imagePlay
        }
        else {
            //resume the RunRandom runnable
            //display a toast
            //replace iv3 drawable to imagePause
        }
    }
});
TextView tv = (TextView) findViewById(R.id.textView);
tv.post(new RunRandom(2000));
...

//Generate number between 0 and 1
private class RunRandom implements Runnable
{
    private long delayMillis;

    public RunRandom(long delayMillis)
    {
        this.delayMillis = delayMillis;
    }

    @Override
    public void run()
    {
        if (tv != null)
        {
            int min = 0;
            int max = 1;
            int n = rand.nextInt(max - min + 1) + min;
            String Sroll = String.valueOf(n);
            tv.setText("Random number: " + Sroll);
            if (Sroll == 0) {
                //replace iv1 drawable to image_update_1
                //wait for click on iv1
                //if clicked, handle click and revert iv1 back to image1
                //add 1 to i
                //if iv2 is clicked, exit the runnable and display toast
            }
            if (Sroll = 1) {
                //replace iv2 drawable to image_update_2
                //wait for click on iv2
                //if clicked, handle click and revert iv2 back to image2
                //add 1 to i
                //if iv1 is clicked, exit the runnable and display toast
            }

            tv.postDelayed(this, delayMillis);
        }
    }
}

I am trying to generate the following:

Create a runnable which will generate a random number and based on the number change an image drawable.
The runnable will start execution at 2 seconds per "run", and decrease by .5 second every 5 minutes.
The runnable can be paused and resumed, in which case the execution time should complete the duration after resume.

Can I get some assistance in completing my code.




Aucun commentaire:

Enregistrer un commentaire