【问题标题】:Imageview to show different image depending on Int [closed]Imageview 根据 Int 显示不同的图像 [关闭]
【发布时间】:2014-01-08 10:11:43
【问题描述】:

我有一个随机数生成器,我需要显示不同的图像, 取决于不同的 Int 值。

例如当 randomNumber 是 1 时,我需要显示特定的文本和图像 当数字为 10 时,另一个特定的文本和图像等..

我什至可以用 ImageView 做到这一点,我不知道从哪里开始?

    package com.example.drinktivity;

    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;


    public class Main2Activity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);


Button back = (Button) findViewById(R.id.buttonback); // Here the R.id.button1 is the button from you design
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(Main2Activity.this, MainActivity.class);
startActivity(i);
}
});

ImageView image = (ImageView) findViewById(R.id.imageView1);
Bundle bundle = getIntent().getExtras();
int random = bundle.getInt("Value");
TextView text = (TextView) findViewById(R.id.textView1);
if (random==1) {
    text.setText("");
}
if (random==2) {
    text.setText("");
}
if (random==3) {
    text.setText("");
}
if (random==4) {
    text.setText("");
}
if (random==5) {
    text.setText("");
}
if (random==6) {
    text.setText("");
}
if (random==7) {
    text.setText("");
}
if (random==8) {
    text.setText("");
}
if (random==9) {
    text.setText("");
}
if (random==10) {
    text.setText("");
}


}

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

    }

【问题讨论】:

  • 请添加一些你的代码..
  • 请告诉您的随机数限制.??

标签: android random imageview


【解决方案1】:

创建一个可绘制数组

  public static final int[] im_smiley_drawable_smile_old = {
                    R.drawable.im_smiley_happy, R.drawable.im_smiley_sad,
                    R.drawable.im_smiley_winking,
                    R.drawable.im_smiley_tongue_sticking_out,
                    R.drawable.im_smiley_surprised, R.drawable.im_smiley_kissing,
                    R.drawable.im_smiley_yelling, R.drawable.im_smiley_cool}

将随机数发送到该数组,您将获得随机图像

【讨论】:

    【解决方案2】:
    int[] i = {R.drawable.drawable1, R.drawable.drawable2, R.drawable.drawable2,      R.drawable.drawable4, R.drawable.drawable5,};
    Random rand = new Random();
    int  n = rand.nextInt(5);
    imageView.setBackground(getResources().getDrawable(i[n]));
    

    【讨论】:

      【解决方案3】:

      将您的 int 值视为 int random 并且您的随机数由数字 1 到 5 组成。 那么你就可以这样做了。

      if(random==1){
      
      imageview.setImageBitmap(yourbitmap);
      
      //here imageview is your imageview. and then bitmap you want when the number generated is 1.
      
      }
      

      【讨论】:

        【解决方案4】:

        这绝对是可能的,但是有很多方法可以做到这一点。我认为您想要采用的主要方法是生成一个随机数,并根据该数字从您的资源中选择一个图像。您可以像这样检索资源:

        context.getResources().getIdentifier(i.getIcon(), "drawable", context.getPackageName())
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-06-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-28
          • 2019-02-17
          相关资源
          最近更新 更多