单击按钮后每秒更改imageView

时间:2015-09-22 17:18:08

标签: java android

按下按钮后,我正在尝试更改imageView。我希望图像以1秒的间隔改变。它应该不断改变图像60秒。我已经为它写了一个代码。但最终的更改仅在应用程序中显示。未显示所有其他59个更改。我是线程和计时器的新手。请帮我解决问题。

package com.example.android.guessmycolor;

import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Random;


public class Game extends AppCompatActivity {
    Button pink;
    Button blue;
    Button red;
    Button orange;
    Button green;
    Button black;
    Button brown;
    Button yellow;
    Button turquoise;
    Button play;
    TextView time;
    TextView score;
    TextView comment;
    ImageView imageView;
    String[] color = {"pink", "blue", "red", "orange", "green", "black", "brown", "yellow", "turquoise"};
    int[] image = {R.drawable.pink, R.drawable.blue, R.drawable.red, R.drawable.orange, R.drawable.green, R.drawable.black, R.drawable.brown, R.drawable.yellow, R.drawable.turquoise};

    @Override


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        pink = (Button) findViewById(R.id.pink);
        blue = (Button) findViewById(R.id.blue);
        red = (Button) findViewById(R.id.red);
        orange = (Button) findViewById(R.id.orange);
        green = (Button) findViewById(R.id.green);
        black = (Button) findViewById(R.id.black);
        brown = (Button) findViewById(R.id.brown);
        yellow = (Button) findViewById(R.id.yellow);
        turquoise = (Button) findViewById(R.id.turquoise);
        play = (Button) findViewById(R.id.play);
        time = (TextView) findViewById(R.id.time);
        score = (TextView) findViewById(R.id.score);
        comment = (TextView) findViewById(R.id.comment);
        imageView = (ImageView) findViewById(R.id.changingImage);
        imageView.setImageResource(R.drawable.black);
        play();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_game, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    public void play() {
        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int timer = 0;

                while (timer < 60) {
                    timer++;
                    Random random = new Random();
                    int guess = random.nextInt(9);
                    Log.d("guess", "" + guess);
                    final int[] selected = new int[1];
                    pink.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            selected[0] = 0;
                        }
                    });
                    blue.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            selected[0] = 1;
                        }
                    });
                    red.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            selected[0] = 2;
                        }
                    });
                    orange.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            selected[0] = 3;
                        }
                    });
                    green.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            selected[0] = 4;
                        }
                    });
                    black.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            selected[0] = 5;
                        }
                    });
                    brown.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            selected[0] = 6;
                        }
                    });
                    yellow.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            selected[0] = 7;
                        }
                    });
                    turquoise.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            selected[0] = 8;
                        }
                    });

                    try {
                        Thread.sleep(1000);
                        timer++;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }


                    if (guess == 0) {
                        Log.d("thismycolor", "pink");
                        imageView.setImageResource(image[0]);
                    } else if (guess == 1) {
                        imageView.setImageResource(image[1]);
                        Log.d("thismycolor", "blue");
                    } else if (guess == 2) {
                        imageView.setImageResource(image[2]);
                        Log.d("thismycolor", "red");
                    } else if (guess == 3) {
                        imageView.setImageResource(image[3]);
                        Log.d("thismycolor", "orange");
                    } else if (guess == 4) {
                        imageView.setImageResource(image[4]);
                        Log.d("thismycolor", "green");
                    } else if (guess == 5) {
                        imageView.setImageResource(image[5]);
                        Log.d("thismycolor", "black");
                    } else if (guess == 6) {
                        imageView.setImageResource(image[6]);
                        Log.d("thismycolor", "brown");
                    } else if (guess == 7) {
                        imageView.setImageResource(image[7]);
                        Log.d("thismycolor", "yellow");
                    } else if (guess == 8) {
                        imageView.setImageResource(image[8]);
                        Log.d("thismycolor", "turquoise");
                    }


                }

            }


        });
    }
}

1 个答案:

答案 0 :(得分:0)

如果我关注你的问题,我认为你需要将你的监听器代码放在onCreate方法中。并在play方法中启动一个新线程。

public void play() {
        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


       new Thread(new Runnable(){

       public void run(){

        int timer = 0;

                    while (timer < 60) {
                        timer++;
                        Random random = new Random();
                        int guess = random.nextInt(9);
                        Log.d("guess", "" + guess);
                        final int[] selected = new int[1];

                       try {
                            Thread.sleep(1000);
                            timer++;
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

              runOnUiThread(new Runnable(){
                    public void run() {

                     if (guess == 0) {
                            Log.d("thismycolor", "pink");
                            imageView.setImageResource(image[0]);
                        } else if (guess == 1) {
                            imageView.setImageResource(image[1]);
                            Log.d("thismycolor", "blue");
                        } else if (guess == 2) {
                            imageView.setImageResource(image[2]);
                            Log.d("thismycolor", "red");
                        } else if (guess == 3) {
                            imageView.setImageResource(image[3]);
                            Log.d("thismycolor", "orange");
                        } else if (guess == 4) {
                            imageView.setImageResource(image[4]);
                            Log.d("thismycolor", "green");
                        } else if (guess == 5) {
                            imageView.setImageResource(image[5]);
                            Log.d("thismycolor", "black");
                        } else if (guess == 6) {
                            imageView.setImageResource(image[6]);
                            Log.d("thismycolor", "brown");
                        } else if (guess == 7) {
                            imageView.setImageResource(image[7]);
                            Log.d("thismycolor", "yellow");
                        } else if (guess == 8) {
                            imageView.setImageResource(image[8]);
                            Log.d("thismycolor", "turquoise");
                        }

                   }
        });

      }
      }).start();

     }
    });
  }

还有一件事让你成像public static volatile ImageView imageView;