-1

对不起,我是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]);

        }
    }
}
4

1 回答 1

0

好的,

首先,保留对您正在使用的 ImageViews 的引用,并在每次通过循环时使用 findViewById,因为这是浪费时间,因为它很昂贵。

这将为您提供 id-3 的视图,

int vs = imageViews[i]-3;

所以如果你打电话

ImageView ivs = (ImageView) findViewById( vs );

ivs 它可能为空,或者与列表中的 i-3rd 不同的视图。

此代码将为 3 个随机 ImageView 显示相同的 randomImage 并将另一个 randomImage 从 Other 数组设置为最后一个 ImageView

Random random = new Random( System.currentTimeMillis() );

ImageView[] imageViews = new ImageView[4];

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() {
     imageViews[0]= (ImageView)findViewById(R.id.A);
     imageViews[1]= (ImageView)findViewById(R.id.B);
     imageViews[2]= (ImageView)findViewById(R.id.C);
     imageViews[3]= (ImageView)findViewById(R.id.D);
}

    public void randomImage(){
    // List<Integer> generated = new ArrayList<Integer>(); i dont get what that is supposed to do?

    int next = random.nextInt( img.length+1 ); // will generate a position from a random image within the img array
    int randomOtherImage = random.nextInt( ansImg.length+1 );
    int differentImage = random.nextInt( imageViews.length+1 );
    for (int i = 0; i < imageViews.length; i++) {
        if (differentImage == i)
            imageViews[i].setImageRessource(ansImg[randomOtherImage]);
        else
            imageViews[i].setImageRessource(ans[next]);
    }
}
于 2013-09-21T21:03:46.560 回答