How To Display Random images on image view

前端 未结 1 511
执念已碎
执念已碎 2020-12-10 21:14

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 app

1条回答
  •  無奈伤痛
    2020-12-10 22:10

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

    Layout (res/layout/rnd_images.xml):

    
        
    
    

    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.

    0 讨论(0)
提交回复
热议问题