こんにちは、このモデルで 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);
}
解決策はありますか、または代替手段はありますか。ありがとうございました。