0

我有一个要求,我有 5 ImageButtons。我想在这 5 个上ImageButtons随机加载一些 60 - 80 个图像。我怎样才能做到这一点?

public void setRandomImages(int n){
    Random rand = new Random();
    int x = rand.nextInt(5)+1;

    switch (x) {
    case 1:
        bt1.setVisibility(View.VISIBLE);
        bt1.setBackgroundColor(Color.RED);
        bt1.setOnClickListener(new listener());

        bt2.setVisibility(View.VISIBLE);
        bt2.setBackgroundColor(Color.BLACK);
        bt2.setOnClickListener(new listener());

        bt3.setVisibility(View.VISIBLE);
        bt3.setBackgroundColor(Color.BLUE);
        bt3.setOnClickListener(new listener());

        bt4.setVisibility(View.VISIBLE);
        bt4.setBackgroundColor(Color.GREEN);
        bt4.setOnClickListener(new listener());

        bt5.setVisibility(View.VISIBLE);
        bt5.setBackgroundColor(Color.YELLOW);
        bt5.setOnClickListener(new listener());
        break;

    default:
        break;
    }

同样onclick对于相应的加载图像,我想播放一些声音,因为我可以使用处理程序。

我想知道的是如何将相应的侦听器随机引用到选定的图像。

IE,

如果我选择一个图像,我将播放声音,然后在其位置加载不同的图像,并重复上述所有过程。

我知道这很混乱,但是任何了解我的要求的人都会建议/帮助我。

编辑:

RandomImagePicker.java

public class RandImagePicker extends Activity {

Handler handler = new Handler();
int resId1,resId2,resId3,resId4;
List<ImageSound> ImageSoundObjs = new ArrayList<ImageSound>();
ImageView iv5;
ImageView img1,img2,img3,img4;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rand_image_picker);

    iv5 = (ImageView)findViewById(R.id.image5);

    createImageAndSoundObjects();
    showObjects();
}

private void createImageAndSoundObjects() {
    ImageSound obj;

    TypedArray imgs = getResources().obtainTypedArray(R.array.random_imgs);
    for(int i = 0 ; i <= 12 ; i++) {

        obj = new ImageSound(imgs.getResourceId(i, 1));
        ImageSoundObjs.add(obj);
    }
}

private void showObjects() {
    Random rand = new Random();
    img1 = (ImageView) findViewById(R.id.image1);
    img2 = (ImageView) findViewById(R.id.image2);
    img3 = (ImageView) findViewById(R.id.image3);
    img4 = (ImageView) findViewById(R.id.image4);

    for(int i = 0 ; i <= 12 ; i++) {
        int  n1 = rand.nextInt(12)+1;
        int  n2 = rand.nextInt(12)+1;
        int  n3 = rand.nextInt(12)+1;
        int  n4 = rand.nextInt(12)+1;

        resId1 = ImageSoundObjs.get(n1).getResourceId();
        img1.setImageResource(resId1);
        img1.setOnClickListener(new Listener());

        resId2 = ImageSoundObjs.get(n2).getResourceId();
        img2.setImageResource(resId2);
        img2.setOnClickListener(new Listener());


        resId3 = ImageSoundObjs.get(n3).getResourceId();
        img3.setImageResource(resId3);
        img3.setOnClickListener(new Listener());

        resId4 = ImageSoundObjs.get(n4).getResourceId();
        img4.setImageResource(resId4);
        img4.setOnClickListener(new Listener());
    }

}

class Listener implements OnClickListener{

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


        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub

                img1.setVisibility(View.VISIBLE);
                img2.setVisibility(View.VISIBLE);
                img3.setVisibility(View.VISIBLE);
                img4.setVisibility(View.VISIBLE);
                showObjects();
                iv5.setVisibility(View.GONE);
            }
        }, 1000);

        img1.setVisibility(View.GONE);
        img2.setVisibility(View.GONE);
        img3.setVisibility(View.GONE);
        img4.setVisibility(View.GONE);

        if (v.getId() == R.id.image1) {
            iv5.setVisibility(View.VISIBLE);
            iv5.setImageResource(resId1);
        } else if (v.getId() == R.id.image2) {
            iv5.setVisibility(View.VISIBLE);
            iv5.setImageResource(resId2);
        } else if (v.getId() == R.id.image3) {
            iv5.setVisibility(View.VISIBLE);
            iv5.setImageResource(resId3);
        } else if (v.getId() == R.id.image4) {
            iv5.setVisibility(View.VISIBLE);
            iv5.setImageResource(resId4);
        }

    }

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.rand_image_picker, menu);
    return true;
}

}

ImageSound.java

public class ImageSound {
int mResourceId;
ImageSound(int i) {
    mResourceId = i;
}

public int getResourceId() {
    return mResourceId;
}

}

4

1 回答 1

1

我创建了一个非常基本的示例,向您展示如何随机选择图像,这可以通过根据需要向对象添加属性来扩展,就像您的情况中的声音一样。

它只是一个 10 分钟的快速原型制作,请在此处查看。

https://github.com/sathvik87/RandomImage

于 2013-03-22T12:44:54.103 回答