随机图像生成器Android

时间:2012-02-22 22:44:25

标签: android eclipse image random generator

我在Eclipse中使用Android。我的随机图像生成器工作。此时加载页面时,在用户单击按钮之前会自动在ImageView1中加载随机图像。 (在用户单击按钮后,加载另一个随机图像)。 *加载页面时,我不希望在用户单击按钮之前在ImageView1上显示任何图像。我怎样才能更改我的代码,以便在用户点击按钮之前不会自动加载图像?

    package com.senior.callacab2;

    import java.util.Random;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.ImageView;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    public class Generatedrinks extends Activity implements OnClickListener {


private int[] mPhotoIds = new int [] { R.drawable.img1,
        R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5,
        R.drawable.img6, R.drawable.img7 };

private static final Random rgenerator = new Random();

private ImageView iv;

// Makes new images appear after every button click
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.generatedrinks);

    Integer q = mPhotoIds[rgenerator.nextInt(mPhotoIds.length)];
    iv = (ImageView) findViewById(R.id.imageView1);
    iv.setImageResource(q);

    View nextButton = findViewById(R.id.button2);
    nextButton.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.button2:

 iv.setImageResource(mPhotoIds[rgenerator.nextInt(mPhotoIds.length)]);
        break;
    }

    // Makes button2 invisible after two clicks the first click generates a
    // random image and the second click makes the button disappear
    Button generator = (Button) findViewById(R.id.button2);
    generator.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Button button = (Button) v;
            button.setVisibility(View.INVISIBLE);
        }
    });
    // When this button is pressed it takes the user to the next layout
    // called Scoreclass.class

    Button next = (Button) findViewById(R.id.button1);
    next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent myIntent = new Intent(view.getContext(), Scorecard.class);
            startActivityForResult(myIntent, 0);
        }

    });

}

    }

1 个答案:

答案 0 :(得分:1)

好吧,如果您不希望活动在单击之前加载图像,则在开始活动时不要加载图像...

您的onCreate

中有此代码
Integer q = mPhotoIds[rgenerator.nextInt(mPhotoIds.length)];
iv = (ImageView) findViewById(R.id.imageView1);
iv.setImageResource(q);

删除第一行和第三行(第二行仍可用于查找视图供以后使用),并且在活动开始时不会加载图像。