0

我希望从 5 个图像视图中随机显示一个图像视图,然后当用户单击显示的图像视图时,我希望出现另一个图像视图,并且该过程将重复直到计数达到 5,但代码一次显示多个图像视图。

我的代码:

public class Gamepage extends Activity 
{
    ImageView img,img1,img2,img3,img4;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.trygame);

        img=(ImageView) findViewById(R.id.imageView2);
        img1=(ImageView) findViewById(R.id.imageView3);
        img2=(ImageView) findViewById(R.id.imageView4);
        img3=(ImageView) findViewById(R.id.imageView5);
        img4=(ImageView) findViewById(R.id.imageView6);
        int choose;
        int count=0;
            choose=(int) (Math.random()*5);
            while(count!=5)
            {
            switch(choose)
        {
        case 1:

        img.setVisibility(View.VISIBLE);
                img.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub

            Toast toast= Toast.makeText(Gamepage.this, "caught!! ",     200);
            toast.setGravity(Gravity.RIGHT, 10,10);
            toast.show();

            img.setVisibility(View.INVISIBLE);
            return true;

        }
    });
            //choose=(int) (Math.random()*5);
            //count++;
    break;
    case 2:
        img1.setVisibility(View.VISIBLE);


        img1.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub

                Toast toast= Toast.makeText(Gamepage.this,     "caught!! ", 200);
                toast.setGravity(Gravity.RIGHT, 10,10);
                toast.show();
                img1.setVisibility(View.INVISIBLE);
                return true;
            }
        });
        //choose=(int) (Math.random()*5);
        //count++;
        break;
    case 3:

        img2.setVisibility(View.VISIBLE);

        img2.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub

                Toast toast= Toast.makeText(Gamepage.this, "caught!! ", 200);
                toast.setGravity(Gravity.RIGHT, 10,10);
                toast.show();
                img2.setVisibility(View.INVISIBLE);
                return true;
            }
        });
        //choose=(int) (Math.random()*5);
        //count++;
        break;
    case 4:

        img3.setVisibility(View.VISIBLE);

        img3.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub

                Toast toast= Toast.makeText(Gamepage.this, "caught!! ", 200);
                toast.setGravity(Gravity.RIGHT, 10,10);
                toast.show();
                img3.setVisibility(View.INVISIBLE);
                return true;
            }
        });
        //choose=(int) (Math.random()*5);
        //count++;
        break;
    case 5:

        img4.setVisibility(View.VISIBLE);

        img4.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub

                Toast toast= Toast.makeText(Gamepage.this, "caught!! ", 200);
                toast.setGravity(Gravity.RIGHT, 10,10);
                toast.show();
                img4.setVisibility(View.INVISIBLE);
                return true;

            }
        });
        //choose=(int) (Math.random()*5);
    //  count++;
        break;
        default:
            img4.setVisibility(View.VISIBLE);

            img4.setOnTouchListener(new View.OnTouchListener() {

                public boolean onTouch(View v, MotionEvent event) {
                    // TODO Auto-generated method stub

                    Toast toast= Toast.makeText(Gamepage.this, "caught!! ", 200);
                    toast.setGravity(Gravity.RIGHT, 10,10);
                    toast.show();
                    img4.setVisibility(View.INVISIBLE);
                    return true;

                }
            });
            //choose=(int) (Math.random()*5);
            //count++;
            break;

    }
        choose=(int) (Math.random()*5);
        count++;
        }


}


}
4

4 回答 4

0
  • 在循环之前,您是否将所有 ImageView 的可见性设置为 INVISIBLE?
  • 由于循环可以非常快地运行,因此您可能不会以这种方式看到结果。尝试将 Thread.sleep(3000) 放在循环的末尾,这样它会减慢循环速度。
于 2012-05-19T09:03:10.700 回答
0

像这样设置可见性真假是非常糟糕的设计。您可以改为创建一个 ImageView 并使用“setImageResource()”更改图像资源。

如果,据我了解,单击图像的功能只是显示另一个随机图像,您可以执行以下操作:

1- 在您的 XML 布局中,创建一个“ImageView”。

2- 在您的活动 java 代码中,使用您的图像资源创建一个数组(最好是一个数组列表)。

ArrayList<int> myImages = new ArrayList<int>();
myImages.add(R.drawable.firstimage);
myImages.add(R.drawable.secondImage);

3-不要编写所有这些侦听器和长开关案例,而是使用以下简单代码:

int rnd = Math.random()*5;
int counter = 0;
public void onClick(View v){
   if(counter<5){
      img.setImageResource(myImages.get(rnd));
      counter++;
   }
   else{
      //do whatever you want
   }
}
于 2012-05-19T13:11:11.337 回答
0

我想我有解决办法。它在这里工作......试试这个:

public class GamePage extends Activity {
/** Called when the activity is first created. */

private int count=0;
private ImageView img;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.trygame);

    img=(ImageView) findViewById(R.id.imageView1);
    img.setImageResource(R.drawable.your_bitmap_drawable1);
    img.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (count<5){
                int choose=(int) (Math.random()*5);
                switch(choose){
                    case 1:
                        img.setImageResource(R.drawable.your_bitmap_drawable1);
                    break;
                    case 2:
                        img.setImageResource(R.drawable.your_bitmap_drawable2);
                    break;
                    case 3:
                        img.setImageResource(R.drawable.your_bitmap_drawable3);
                    break;
                    case 4:
                        img.setImageResource(R.drawable.your_bitmap_drawable4);
                    break;
                    case 5:
                        img.setImageResource(R.drawable.your_bitmap_drawable5);
                    break;
                    default:
                        img.setImageResource(R.drawable.your_bitmap_drawable5);
                    break;
                }
                Toast toast= Toast.makeText(TextViewEffectsActivity.this, "caught!! ",     200);
                toast.setGravity(Gravity.RIGHT, 10,10);
                toast.show();
            }
            count++;
        }
    });
}
}

尝试游戏.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

你不需要有 5 个 ImageViews ......告诉我它是否适合你......

于 2012-05-19T09:11:37.177 回答
0

我在这里有一个问题:

- 你把你的图像放入一个数组列表中

- 随机化你的列表

- 获取列表中的第一张图片以显示

- 设置监听器或超时以更改为下一个图像。

于 2012-05-19T13:22:20.260 回答