对不起,我是android编程的新手。
我有一个游戏,它由数组中的多个图像组成。我想在 4 个图像视图中显示 3 个相同的图像和 1 个不同的图像,如果答案是正确的并且玩家找到不同的图像,它将再次循环和随机。请任何人给我一个例子。
Random random = new Random( System.currentTimeMillis() );
int next = random.nextInt( 23 ) + 1;
int[] imageViews = {R.id.A,R.id.B,R.id.C,R.id.D };
int[] img = new int[] {R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,
R.drawable.img5,R.drawable.img6,R.drawable.img7,R.drawable.img8,
R.drawable.img9,R.drawable.img10};
int [] ansImg = new int[]{R.drawable.aimg1,R.drawable.aimg2,R.drawable.aimg3,R.drawable.aimg4,
R.drawable.aimg5,R.drawable.aimg6,R.drawable.aimg7,R.drawable.aimg8,
R.drawable.aimg9,R.drawable.aimg10};
private void allImage() {
ImageView a= (ImageView)findViewById(R.id.A);
ImageView b= (ImageView)findViewById(R.id.B);
ImageView c= (ImageView)findViewById(R.id.C);
ImageView d= (ImageView)findViewById(R.id.D);
}
public void randomImage(){
List<Integer> generated = new ArrayList<Integer>();
for (int i = 0; i < imageViews.length; i++) {
int v = imageViews[i]-1;
int vs = imageViews[i]-3;
if ( generated.contains( next ) ) {
generated.add( next );
ImageView iv = (ImageView) findViewById( v );
ImageView ivs = (ImageView) findViewById( vs );
iv.setImageResource( img[next] );
ivs.setImageResource(ansImg[next]);
}
}
}