【问题标题】:Display Random image on each start up android application在每次启动 android 应用程序时显示随机图像
【发布时间】:2014-05-07 13:46:03
【问题描述】:

有人可以帮助我使用代码示例来随机显示我的绘图文件夹中的图片吗?我是新开发人员,所以我不知道该怎么做。谢谢!

我的要求是:显示随机图像(图像应在每次启动时更改

【问题讨论】:

  • 这不是 SO 的工作方式。首先,您可以在谷歌上搜索一些想法,尝试一下,如果仍然不起作用,请回来告诉我们您做了什么。

标签: android image random webview startup


【解决方案1】:

考虑到您在 drawable 中有 10 张图片,名称格式为 your_image_1, your_image_2, .... 直到 your_image_10 您可以使用下面的代码在每次应用程序启动时将随机图像设置为ImageView

public void onCreate(Bundle instance){
//....
     Random r = new Random();
     int randomNumber = r.nextInt(10 - 1) + 1;

     ImageView image = (ImageView) findViewById(R.id.image);
     String imageName = "your_image_" + randomNumber;
     image_ID = getResources().getIdentifier(imageName, "drawable", getPackageName());

     image.setBackgroundResource(image_ID);
}

【讨论】:

    【解决方案2】:

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

    布局(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 开始

    【讨论】:

    • 完美!非常感谢@vyger
    【解决方案3】:

    您可以像@Saqib 提到的那样在启动时显示随机图像,或者您可以循环显示图像,即在第一次启动时您可以显示第一张图像,第二张显示第二张图像,然后重复循环。为此,您需要做的就是优先存储一个整数,并在每次启动应用程序时递增整数的值,并优先存储更新后的值

    【讨论】:

      猜你喜欢
      • 2012-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多