ImageView上的随机图像 [英] Random image on ImageView

查看:135
本文介绍了ImageView上的随机图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Eclipse的新手,到目前为止只使用它一天,而且之前没有Java经验,虽然我不太了解我仍然明白它是怎么回事。

I am very new to Eclipse, and have been using it only for a day so far, and also have no previous experience with Java, although I don't know much I still understand how it goes.

有人可以帮我一个代码,从我的图纸文件夹中随机生成一张图片吗?
我的android应用程序空白并且在全屏模式下,我在 imageView1 中添加了一张图片, fitCenter '编辑它,它确实很好,我清除了沿途发生的所有错误。

Could someone please help me out with a code to randomly generate a picture from my drawings folder? I got my android application blank and on full screen mode, I have added a picture in imageView1, fitCenter'ed it and it really works good, I cleaned out all the errors that occurred along the way also.

这是我的 ImageView 代码到目前为止:

This is how my ImageView code looks so far:

<ImageView
        android:id="@+id/imageView1"
        android:contentDescription="@string/desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:scaleType="fitCenter"
        android:src="@drawable/img1" />

现在我真正需要的是我的应用程序只显示一个随机图像,每次我运行应用程序显示不同的图像,并且一旦在应用程序中,我可以在图像上单击然后它显示不同的随机图像。

Now what I really need is that my application only shows a random image, and every time I run the app it shows a different image, and also that once in application, I can On Click on the image and then it shows a different random image.

我想我必须添加一些字符串或其他东西,我真的很新,并且还不知道。

I guess I have to add some strings or something, I'm really new to this and don't know much yet.

真的如果你能在这里帮助我,我将不胜感激:)。

Really appreciate if you could help me out here :).

推荐答案

试试这个

int[] photos={R.drawable.image1, R.drawable.image2,R.drawable.image3,R.drawable.image4};

ImageView image = (ImageView) findViewById(R.id.imageview1);

Random ran=new Random();
int i=ran.nextInt(photos.length);
image.setImageResource(photos[i]);
image.setOnClickListener(new View.onClickListener()
{
    public void onClick(View v)
    {
      int k=ran.nextInt(photos.length);
      image.setImageResource(photos[k]);
    }
}
);

注意:如果您找到任何语法,我没有在Eclipse或任何java编辑器中输入此内容错误自己纠正。

Note: I haven't typed this in Eclipse or in any java editor, if you find any syntax error correct it by yourself.

这篇关于ImageView上的随机图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆