简体   繁体   English

如何从可绘制文件夹中插入随机图像,使其在加载时随机出现在屏幕上?

[英]How do I insert random images from my drawable folder to randomly appear on the screen when loaded?

I have 15 Images in my drawable folder and want 6 of these random images from this folder to be inserted into given locations when I open this page. 我的可绘制文件夹中有15张图像,并且当我打开此页面时,希望将来自此文件夹的6张随机图像插入到给定位置。
Any help will be greatly appreciated. 任何帮助将不胜感激。 Below is my xml 下面是我的xml

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="30dp"
    android:layout_marginLeft="65dp"
    android:src="@drawable/circle" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/imageView1"
    android:layout_marginRight="65dp"
    android:src="@drawable/eclipse" />

<ImageView
    android:id="@+id/imageView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_below="@+id/imageView1"
    android:layout_marginTop="52dp"
    android:src="@drawable/invertedtriangle" />

    <ImageView
    android:id="@+id/imageView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/imageView2"
    android:layout_below="@+id/imageView1"
    android:layout_marginTop="52dp"
    android:src="@drawable/square" />

    <ImageView
    android:id="@+id/imageView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_below="@+id/imageView3"
    android:layout_marginTop="52dp"
    android:src="@drawable/star" />

    <ImageView
    android:id="@+id/imageView6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView2"
    android:layout_below="@+id/imageView3"
    android:layout_marginTop="52dp"
    android:src="@drawable/polygon" />

 </RelativeLayout> 

Thanks. 谢谢。

Here you are: 这个给你:

First of, you need to import the namespace that contains the random object: 首先,您需要导入包含随机对象的名称空间:

import java.util.Random;

Then you have to take an object out of it 那你必须从里面拿出一个物体

final Random rnd = new Random();

Now, all the action happens in your onCreate method, after you set your contentView: 现在,在设置contentView之后,所有操作都将在onCreate方法中进行:

// Reference the images
ImageView img1 = (ImageView) findViewById(R.id.imageView1);
ImageView img2 = (ImageView) findViewById(R.id.imageView2);
ImageView img3 = (ImageView) findViewById(R.id.imageView3);
ImageView img4 = (ImageView) findViewById(R.id.imageView4);
ImageView img5 = (ImageView) findViewById(R.id.imageView5);
ImageView img6 = (ImageView) findViewById(R.id.imageView6);

// Your images are named img_0.png to img_14.png

// For image 1
final String str = "img_" + rnd.nextInt(14);
img1.setImageDrawable
(
    getResources().getDrawable(getResourceID(str, "drawable",
        getApplicationContext()))
);

// For image 2
str = "img_" + rnd.nextInt(14);
img2.setImageDrawable
(
    getResources().getDrawable(getResourceID(str, "drawable",
        getApplicationContext()))
);

// ...

Now, where is the trick? 现在,诀窍在哪里?
In this method: 在这种方法中:

protected final static int getResourceID
(final String resName, final String resType, final Context ctx)
{
    final int ResourceID =
        ctx.getResources().getIdentifier(resName, resType,
            ctx.getApplicationInfo().packageName);
    if (ResourceID == 0)
    {
        throw new IllegalArgumentException
        (
            "No resource string found with name " + resName
            );
    }
    else
    {
        return ResourceID;
    }
}

I left out the lines of code for image 3 to 6, but they are the very same as for image 2. 我省略了图像3至6的代码行,但它们与图像2的相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从res / drawable文件夹中的imageview和gridview中随机设置图像 - How can I set images randomly in the imageview and gridview from the res/drawable folder 如何使用 android studio 4 将图像插入或加载到资源文件夹中的可绘制文件夹? - How do I insert or load images to the drawable folder in the resources folder, using android studio 4? 如何使用文件名访问可绘制文件夹中的图像? - How do I access my images in my drawable folder using the name of file? 我将数据解析为ListView,现在如何在每行的左侧添加图像? (来自可绘制文件夹) - I parsed data into a ListView, how do I now add images to the left of each row? (From drawable folder) 如何从可绘制文件夹而非url添加图像? - How do I add images from drawable folder, instead of from url ? 将图像从我的可绘制文件夹导入列表 - Implement the images from my drawable folder to List 如何从可绘制文件夹动态获取图像? - How can i get images dynamically from drawable folder? 如何访问可绘制文件夹中的图像? - How to accès images from drawable folder? 如何将可绘制文件夹中的图像添加到 FrescoImageViewer? - How to add images from drawable folder to FrescoImageViewer? 如何将图像从drawable传输到SD卡中的文件夹? - How to transfer images from drawable to a folder in sdcard?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM