如何在android中的按钮中随机化图像

时间:2015-06-25 01:55:43

标签: android

我正在开发一个游戏程序,我想在按钮中存储图像。当用户再次玩游戏时,我想随机化按钮中显示的图像,但我不知道如何随机化图像。

public class MainActivity extends Activity {
    public static final String COME_FROM = "come_from";
    private int[] id_mc = new int[16];
    private Integer[][] img_mc = new Integer [16][2];
    private Button[] myMcs = new Button[16];
    private int mc_counter = 0;
    private int firstid = 0;
    private int secondid = 0;
    private Boolean mc_isfirst = false;
    private int correctcounter = 0;
    private TextView tFeedback;
    private MediaPlayer mp;
    private Boolean b_snd_inc, b_snd_cor;
     Random r = new Random();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//      setContentView(R.layout.activity_main);
        Toast.makeText(this, "onCreate", Toast.LENGTH_SHORT).show();
        initGame();
    }
    private void initGame() {
        setContentView(R.layout.activity_main);
        SharedPreferences settings = getSharedPreferences("memoryPrefs", 0);
        b_snd_cor =settings.getBoolean("play_sound_when_correct", true);
        b_snd_inc =settings.getBoolean("play_sound_when_incorrect", true);      
        mc_counter = 0;
        firstid = 0;
        secondid = 0;
        mc_isfirst = false;
        correctcounter = 0; 
        tFeedback = (TextView) findViewById(R.id.mc_feedback);
        // setup button listeners
        Button startButton = (Button) findViewById(R.id.game_menu);
        startButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                startMenu();
            }
        });     
        Button settingsButton = (Button) findViewById(R.id.game_settings);
        settingsButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                startPrefs();
            }
        });
        // fill arrays with resources
        id_mc[0] = R.id.mc0;
        id_mc[1] = R.id.mc1;
        id_mc[2] = R.id.mc2;
        id_mc[3] = R.id.mc3;
        id_mc[4] = R.id.mc4;
        id_mc[5] = R.id.mc5;
        id_mc[6] = R.id.mc6;
        id_mc[7] = R.id.mc7;
        id_mc[8] = R.id.mc8;
        id_mc[9] = R.id.mc9;
        id_mc[10] = R.id.mc10;
        id_mc[11] = R.id.mc11;
        id_mc[12] = R.id.mc12;
        id_mc[13] = R.id.mc13;
        id_mc[14] = R.id.mc14;
        id_mc[15] = R.id.mc15;
        img_mc[0][0] = R.drawable.back1;
        img_mc[0][1] = R.drawable.ic_img1;
        img_mc[1][0] = R.drawable.back2;
        img_mc[1][1] = R.drawable.ic_img2;
        img_mc[2][0] = R.drawable.back3;
        img_mc[2][1] = R.drawable.ic_img3;
        img_mc[3][0] = R.drawable.back4;
        img_mc[3][1] = R.drawable.ic_img4;
        img_mc[4][0] = R.drawable.back5;
        img_mc[4][1] = R.drawable.ic_img5;
        img_mc[5][0] = R.drawable.back6;
        img_mc[5][1] = R.drawable.ic_img6;
        img_mc[6][0] = R.drawable.back7;
        img_mc[6][1] = R.drawable.ic_img7;
        img_mc[7][0] = R.drawable.back8;
        img_mc[7][1] = R.drawable.ic_img8;
        img_mc[8][0] = R.drawable.back1;
        img_mc[8][1] = R.drawable.ic_img1;
        img_mc[9][0] = R.drawable.back2;
        img_mc[9][1] = R.drawable.ic_img2;
        img_mc[10][0] = R.drawable.back3;
        img_mc[10][1] = R.drawable.ic_img3;
        img_mc[11][0] = R.drawable.back4;
        img_mc[11][1] = R.drawable.ic_img4;
        img_mc[12][0] = R.drawable.back5;
        img_mc[12][1] = R.drawable.ic_img5;
        img_mc[13][0] = R.drawable.back6;
        img_mc[13][1] = R.drawable.ic_img6;
        img_mc[14][0] = R.drawable.back7;
        img_mc[14][1] = R.drawable.ic_img7;
        img_mc[15][0] = R.drawable.back8;
        img_mc[15][1] = R.drawable.ic_img8;
        //Collections.shuffle(Arrays.asList(img_mc));
        for (int i = 0; i < 16; i++) {
            try{
            myMcs[i] = (Button) findViewById(id_mc[i]);
            myMcs[i].setBackgroundResource(img_mc[i][0]);
            myMcs[i].setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    int i = 0;
                    for (int n = 0; n < 16; n++) {
                        if (id_mc[n] == view.getId())
                            i = n;
                    }
                    doClickAction(view, i);
                }
            });
        }catch(Exception e)
           {
               Toast.makeText(getBaseContext(), e.getMessage(),  Toast.LENGTH_SHORT).show();
           }}
    }
    private void doClickAction(View v, int i)
    {
        v.setBackgroundResource(img_mc[i][1]);
        mc_isfirst = !mc_isfirst;

        // disable all buttons
        for (Button b : myMcs) {
            b.setEnabled(false);
        }
        if (mc_isfirst) {
            // turning the first card
            firstid = i;
            // re enable all except this one
            for (Button b : myMcs) {
                if (b.getId() != firstid) {
                    b.setEnabled(true);
                }
            }
        } else {
            // turning the second card
            secondid = i;   
            doPlayMove();
        }
    }
    private void doPlayMove() {
        mc_counter++;
        if (img_mc[firstid][1] - img_mc[secondid][1] == 0) {
            //correct
            if (b_snd_cor) playSound(R.raw.correct);
            waiting(200);
            myMcs[firstid].setVisibility(View.INVISIBLE);
            myMcs[secondid].setVisibility(View.INVISIBLE);
            correctcounter++;
        } else {
            //incorrect
            if (b_snd_inc) playSound(R.raw.incorrect);
            waiting(400);
        }
        // reenable and turn cards back
        for (Button b : myMcs) {
            if (b.getVisibility() != View.INVISIBLE) {
                b.setEnabled(true);
                b.setBackgroundResource(R.drawable.memory_back);
                for (int i = 0; i < 16; i++) {
                    myMcs[i].setBackgroundResource(img_mc[i][0]);
                }
            }
        }
        tFeedback.setText("" + correctcounter + " / " + mc_counter);
        if (correctcounter > 7) {
            Intent iSc = new Intent(getApplicationContext(), Scoreboard.class);
            iSc.putExtra("com.gertrietveld.memorygame.SCORE", mc_counter);
            startActivity(iSc);
            finish();
        }
    }
    public void playSound(int sound) {
        mp = MediaPlayer.create(this, sound);
        mp.setVolume((float).5,(float).5);
        mp.start();
        mp.setOnCompletionListener(new OnCompletionListener() {
            public void onCompletion(MediaPlayer mp) {
                mp.release();
            }
        });
    }
    public static void waiting(int n) {
        long t0, t1;
        t0 = System.currentTimeMillis();
        do {
            t1 = System.currentTimeMillis();
        } while ((t1 - t0) < (n));
    }
    private void startMenu() {
        Intent launchMenu = new Intent(this, MenuScreen.class);
        launchMenu.putExtra(COME_FROM,"PlayGame");
        startActivity(launchMenu);
    }
    private void startPrefs() {
        Intent launchPrefs = new Intent(this, Setting.class);
        startActivity(launchPrefs);
    }
    ////////////////////////////////    
    @Override
    protected void onRestart() {
        super.onRestart();
        //String sender = getIntent().getExtras().getString("SENDER");  
            //initGame();
            Toast.makeText(this, "onRestart-sender is " , Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onResume() {
    super.onResume();
    SharedPreferences settings = getSharedPreferences("memoryPrefs", 0);
    b_snd_cor =settings.getBoolean("play_sound_when_correct", true);
    b_snd_inc =settings.getBoolean("play_sound_when_incorrect", true);
    Toast.makeText(this, "onResume", Toast.LENGTH_SHORT).show();
    }


    ////////////////////////////////

}

1 个答案:

答案 0 :(得分:0)

你的Collections.shuffle()部分是正确的,你只需要将随机列表放回到你的数组中:

        private Integer[][] img_mc = new Integer [16][2];
        ...
        List<Integer[]> img_mc_list = new ArrayList<Integer[]>();
        for (Integer[] img : img_mc) {
            img_mc_list.add(img);
        }
        Collections.shuffle(img_mc_list);
        img_mc_list.toArray(img_mc);

或者使用它:

private void randomize(Integer[][] array) {
    int index;
    Integer[] temp;
    Random random = new Random();
    for (int i = array.length - 1; i > 0; i--) {
        index = random.nextInt(i + 1);
        temp = array[index];
        array[index] = array[i];
        array[i] = temp;
    }
}