【问题标题】:how to implement 3 same images and 1 different image in 4 image views in android? [closed]如何在 android 的 4 个图像视图中实现 3 个相同的图像和 1 个不同的图像? [关闭]
【发布时间】:2013-09-21 20:25:10
【问题描述】:

对不起大家,我是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 个不同的 imageViews 中显示图像,并且一个图像应该是不同的形式其他。但是没有处理任何类型的用户输入
  • 对不起先生..我的意思是如果答案是正确的并且玩家找到不同的图像它将再次循环和随机。
  • 所以你需要一个代码来做到这一点?

标签: android image random


【解决方案1】:

好的,

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

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

int vs = imageViews[i]-3;

所以如果你打电话

ImageView ivs = (ImageView) findViewById( vs );

它可能为空,或者与列表中的 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]);
    }
}

【讨论】:

  • 谢谢先生。 @丹尼尔博。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-28
  • 2015-07-04
  • 1970-01-01
  • 2019-10-10
  • 2017-12-14
  • 2019-08-12
  • 1970-01-01
相关资源
最近更新 更多