在按钮上随机更改图像并触发相应的侦听器

时间:2013-03-22 11:26:23

标签: android random onclick imageview listener

我要求我有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,我想播放一些声音,因为我可以使用处理程序。

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

即,

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

我知道这一切都很混乱,但任何理解我的要求的人都建议/帮助我。

编辑:

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;
}

}

1 个答案:

答案 0 :(得分:1)

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

它只是一个快速的10分钟原型,请点击此处。

https://github.com/sathvik87/RandomImage