Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

android - How To Display Random images on image view

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


}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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>

Code:

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

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

[UPDATE]

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

setContentView(R.layout.activity_main);

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

OR, better, USE MY CODE AS IS.

It works without modifications.

[UPDATE]

Check this line:

final Random rnd = new Random();

It requires the following import:

import java.util.Random;

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.

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...