0

こんにちは、このモデルで Android アプリを作成しようとしています。主な目的は、以下のコードに示すスイッチ条件でケースをランダム化することです。それぞれの場合に再度メソッドを呼び出しているため、同じ画像が表示される可能性が高いため、このロジックを作成して画像を順番に表示する方法を知りたいです (つまり、ランダム化したいのですが、画像は一度表示されました)すべての画像サイクルが完了するまで表示されません)

public class MainActivity extends Activity{

//int blue,green,yellow,red,brown,white,pink,black;
//Boolean BLUE,GREEN,YELLOW,RED,BROWN,WHITE,PINK,BLACK;
ImageView iv1,iv2,iv3,fs;
private Handler handler = new Handler();
MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    iv1 = (ImageView)findViewById(R.id.image1);
    iv2 = (ImageView)findViewById(R.id.image2);
    iv3 = (ImageView)findViewById(R.id.image3);
    fs = (ImageView)findViewById(R.id.img_fs);
    fs.setVisibility(View.GONE);
    switchingLogic();

}

public void switchingLogic() {      
    Random random = new Random();
    int x = random.nextInt(4)+1;

    switch (x) {
    case 1:
        mp = MediaPlayer.create(getApplicationContext(), R.raw.touch_blue);
        mp.start();
        iv1.setVisibility(View.VISIBLE);
        iv2.setVisibility(View.VISIBLE);
        iv3.setVisibility(View.VISIBLE);
        iv1.setImageResource(R.drawable.play_blue_1);
        iv2.setImageResource(R.drawable.play_blue_2);
        iv3.setImageResource(R.drawable.play_blue_3);
        iv1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                switchCases();

                iv_gone();
                mp = MediaPlayer.create(getApplicationContext(), R.raw.correct_cheers);
                mp.start();

                fs.setVisibility(View.VISIBLE);
                fs.setImageResource(R.drawable.big_play_blue);

            }
        });
        iv2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                iv2.setVisibility(View.GONE);

            }
        });
        iv3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                iv3.setVisibility(View.GONE);

            }
        });

        break;

    case 2:
        iv1.setVisibility(View.VISIBLE);
        iv2.setVisibility(View.VISIBLE);
        iv3.setVisibility(View.VISIBLE);
        iv1.setImageResource(R.drawable.play_green_1);
        iv2.setImageResource(R.drawable.play_green_2);
        iv3.setImageResource(R.drawable.play_green_3);

        iv2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                switchCases();
                iv_gone();
                mp = MediaPlayer.create(getApplicationContext(), R.raw.correct_cheers);
                mp.start();
                fs.setVisibility(View.VISIBLE);
                fs.setImageResource(R.drawable.big_play_green);
            }
        });
        iv1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                iv1.setVisibility(View.GONE);

            }
        });
        iv3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                iv3.setVisibility(View.GONE);

            }
        });
        break;
    case 3:
        iv1.setVisibility(View.VISIBLE);
        iv2.setVisibility(View.VISIBLE);
        iv3.setVisibility(View.VISIBLE);
        iv1.setImageResource(R.drawable.play_yellow_1);
        iv2.setImageResource(R.drawable.play_yellow_2);
        iv3.setImageResource(R.drawable.play_yellow_3);

        iv3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                switchCases();
                iv_gone();
                mp = MediaPlayer.create(getApplicationContext(), R.raw.correct_cheers);
                mp.start();
                fs.setVisibility(View.VISIBLE);
                fs.setImageResource(R.drawable.big_play_yellow);
            }
        });
        iv1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                iv1.setVisibility(View.GONE);

            }
        });
        iv2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                iv2.setVisibility(View.GONE);

            }
        });

        break;
    case 4:
        iv1.setVisibility(View.VISIBLE);
        iv2.setVisibility(View.VISIBLE);
        iv3.setVisibility(View.VISIBLE);
        iv1.setImageResource(R.drawable.play_red_1);
        iv2.setImageResource(R.drawable.play_red_2);
        iv3.setImageResource(R.drawable.play_red_3);

        iv1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                switchCases();
                iv_gone();
                mp = MediaPlayer.create(getApplicationContext(), R.raw.correct_cheers);
                mp.start();
                fs.setVisibility(View.VISIBLE);
                fs.setImageResource(R.drawable.big_play_red);
            }
        });
        iv2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                iv2.setVisibility(View.GONE);

            }
        });
        iv3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                iv3.setVisibility(View.GONE);

            }
        });
        break;
    default:
        break;
    }
      }
    protected void switchCases() {
        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                fs.setVisibility(View.GONE);
                switchingLogic();
            }
        }, 1500);
    }
    public void iv_gone() {
        iv1.setVisibility(View.GONE);
        iv2.setVisibility(View.GONE);
        iv3.setVisibility(View.GONE);
    }

解決策はありますか、または代替手段はありますか。ありがとうございました。

4

1 に答える 1

0

これが私のコメントの簡単な例です。これを試してください:

public class MainActivity extends Activity{

    //int blue,green,yellow,red,brown,white,pink,black;
    //Boolean BLUE,GREEN,YELLOW,RED,BROWN,WHITE,PINK,BLACK;
    ImageView iv1,iv2,iv3,fs;
    private Handler handler = new Handler();
    MediaPlayer mp;
    Random random = new Random(); // Use only one random object, do not redeclare it
    List<Integer> recentResults; // A list saving your results

    public void switchingLogic() {      
        if(recentResults == null) recentResults = new ArrayList<Integer>();
        if(recentResults.size() == 4) recentResults.clear();

        int x = random.nextInt(4)+1;

        while(recentResults.contains(x)){
            x = random.nextInt(4)+1;
        }

        recentResults.add(x);

        switch (x) { 
            case 1: {
                // Other codes
                setSpecialOnClickListenerWithDrawable(iv1, R.drawable.big_play_blue);
                setGeneralOnClickListener(iv2);
                setGeneralOnClickListener(iv3);

                break;
            }
            // Other cases here
        }
    }

    public void setGeneralOnClickListener(ImageView iv) {      
        iv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // v here will refers back to iv itself
                v.setVisibility(View.GONE);
            }
        });
    }

    public void setSpecialOnClickListenerWithDrawable(ImageView iv, final int drawable) {      
        iv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                switchCases();

                iv_gone();
                mp = MediaPlayer.create(getApplicationContext(), R.raw.correct_cheers);
                mp.start();

                fs.setVisibility(View.VISIBLE);
                fs.setImageResource(drawable);
            }
        });
    }

    // Your other codes here
}

上記を実行する簡単なアプリケーションを作成したところ、次の結果が得られました: 4 3 2 1 2 4 1 3 3 1 4 2 1 3 4 2 3 4 2 1.

于 2013-05-07T06:10:26.937 に答える