如何在图像视图上显示随机图像

How To Display Random images on image view(如何在图像视图上显示随机图像)

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

问题描述

有人可以帮我提供一个代码来随机显示我的绘图文件夹中的图片吗?我是java新手,所以我不知道该怎么做.任何帮助将不胜感激.我的要求是:-1.显示随机图像(图像应在每次启动时改变)2.就是这样

Could someone please help me out with a code to randomly display a picture from the my drawings folder? i am new to java so i have no idea how to do it. Any help will be appreciated. My Requirements are:- 1.Display Random image (image should change on each start up) 2.that's all

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

public class MainMenu extends Activity {

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

public void openNewActivity(View view) { 
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);//button to open main
}
public void browser(View view) { 
    Intent intent = new Intent(this,Browser.class);
    startActivity(intent);//button to open browser
}


final Random rnd = new Random();

{

    setContentView(R.layout.activity_main_menu);

    final ImageView img = (ImageView) findViewById(R.id.imgRandom);
    // I have 3 images named img_0 to img_2, so...
    final String str = "img_" + rnd.nextInt(9);
    img.setImageDrawable
    (
        getResources().getDrawable(getResourceID(str, "drawable",
            getApplicationContext()))
        );
}

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;
    }


}
}

推荐答案

将一些名为 img_0 到 img_n 的图片放到你的 res/drawable 文件夹中

Put some images named img_0 to img_n in your res/drawable folder

布局(res/layout/rnd_images.xml):

Layout (res/layout/rnd_images.xml):

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    >
    <ImageView
        android:id="@+id/imgRandom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
    />
</RelativeLayout>

代码:

package com.example.app;

import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;

public class MainActivity
extends Activity
{
    final Random rnd = new Random();

    @Override
    protected void onCreate(
        final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.rnd_images);

        final ImageView img = (ImageView) findViewById(R.id.imgRandom);
        // I have 3 images named img_0 to img_2, so...
        final String str = "img_" + rnd.nextInt(2);
        img.setImageDrawable
        (
            getResources().getDrawable(getResourceID(str, "drawable",
                getApplicationContext()))
            );
    }

    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;
        }
    }
}

请注意,您必须将 rnd.nextInt(2) 设置为 rnd.nextInt(Max - 1),因为 rnd 从 0 开始

Note that you have to set rnd.nextInt(2) to rnd.nextInt(Max - 1), since rnd starts from 0

[更新]

布局名称必须与 setContentView 中的名称匹配.
所以,如果你有(为什么?)这个:

The layout name must match that in setContentView.
So, if you have (why?) this:

setContentView(R.layout.activity_main);

在您的 MainActivity.java/onCreate 中,然后将布局重命名为activity_main.xml"

in your MainActivity.java/onCreate, then rename the layout "activity_main.xml"

或者,更好的是,按原样使用我的代码.

OR, better, USE MY CODE AS IS.

无需修改即可工作.

[更新]

检查这一行:

final Random rnd = new Random();

它需要以下导入:

import java.util.Random;

我的代码按原样工作.我在给你之前测试了它.
只需将我的布局放在 res/layout 中,将图像放在 res/drawable 和 MainActivity.java 中即可替换默认的.

My code works as is. I tested it before giving it to you.
Just place my layout in res/layout, the images in res/drawable and the MainActivity.java to replace the default one.

请注意,图片名称必须是img_#",其中 # 是一个数字.
此数字必须为 0 到 (max - 1).

Please, notice that the images names MUST be "img_#" where # is a number.
This number must be 0 to (max - 1).

或者给出诸如my_city_#"之类的名称.
但随后您必须更新 java 代码以匹配这些名称.

Or give names like "my_city_#" or whatever.
But then you must update the java code to match these names.

这篇关于如何在图像视图上显示随机图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在图像视图上显示随机图像

基础教程推荐