سؤال

I build a android application in which i want display image one by one after a random time. means

image 1 show

after 10 sec

image 2 show

after 30 sec

image 3 show

after 50 sec

image 4 show

i have code for displaying image but it shows images continuously every 10 sec while i want image show after a random time.

public class MainActivity extends Activity {
  ImageView imageView;


 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     imageView = (ImageView) findViewById(R.id.imageView1);
     final int []imageArray=
       {R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e};


       final Handler handler = new Handler();
        Runnable runnable = new Runnable() {
        int i=0;
        public void run() {
            imageView.setImageResource(imageArray[i]);
            i++;
            if(i>imageArray.length-1)
            {
            i=0;    
            }
            handler.postDelayed(this, 10000);  //for interval...
         }

       };
      handler.postDelayed(runnable, 10000); //for initial delay..

      }

xml file is-

     <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"
tools:context=".MainActivity" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

  </RelativeLayout>
هل كانت مفيدة؟

المحلول 3

You need to use a random number generator if you want there to be a random amount of time between the images. For example:

Random r = new Random();
int timer = r.nextInt(60000-30000) + 30000;

Then you can just use handler.postDelayed(this, timer); in your run function, and any time you want to generate a new random number, just call r.nextInt again.

More information on random numbers here: How can I generate random number in specific range in Android?

نصائح أخرى

We can also use modular arithmetic concept for repetition of images.

int cimi;    
int img[] ={R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e,R.drawable.f};
// inside onCreate()..
i = (ImageView)findViewById(R.id.iv1);
Runnable runn = new Runnable() {
        @Override
        public void run() {
            i.setImageResource(img[cimi]);
            cimi++;
            cimi = cimi%img.length;
            i.postDelayed(this,2000);
        }
    };
    i.postDelayed(runn,2000);

Set up your min and max intervals:

static final float MIN_INTERVAL = 10000;
static final float MAX_INTERVAL = 20000;

Then for the interval:

handler.postDelayed(this, Math.rand()*(MAX_INTERVAL-MIN_INTERVAL) + MIN_INTERVAL);

Try as it follows:

AnimationDrawable animation = new AnimationDrawable();
animation.addFrame(getResources().getDrawable(R.mipmap.download), 1000);
animation.addFrame(getResources().getDrawable(R.mipmap.downloada), 5000);
animation.addFrame(getResources().getDrawable(R.mipmap.ic_launcher), 3000);
animation.setOneShot(false);

ImageView imageAnim =  (ImageView) findViewById(R.id.img);
imageAnim.setBackgroundDrawable(animation);

// start the animation!
animation.start();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top