1

I have a list of animated gifs in drawable resource folder in my android project. I need these gifs played one by one, wait for seconds, and then play the next gif. but when I run the app, all gifs load very very fast so I can just see the last gif. I used Glide for playing gifs and this is my code

Handler handler1 = new Handler();        

for (int a = 1; a<=lines.size() ;a++) {
    handler1.postDelayed(new Runnable() {

        @Override
        public void run() {
            String resName = "";
            int duration = 0;
            resName = "wo_" + (lines.get(slideIndex).split(",")[0]).toLowerCase().trim().replace(".gif", "");
            duration = Integer.parseInt(lines.get(slideIndex++).split(",")[2].trim());
            int resourceId = getResId(resName, R.drawable.class);//this.getResources().getIdentifier(resName, "drawable", this.getPackageName());

            Glide.with(WorkoutActivity.this).asGif().load(resourceId).into(imageView);
        }
    }, 5000);
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
MH2538
  • 169
  • 2
  • 4
  • 11
  • You can write `Thread.sleep(5000);` instead of Handler – Shreeya Chhatrala Jan 02 '20 at 06:49
  • @AndroidPlayer_Shree no, using sleep make UI get freeze – MH2538 Jan 02 '20 at 06:51
  • @MH2538: To achieve same need to post next Runnable when first is end currently you are posting in `parallel` – ρяσѕρєя K Jan 02 '20 at 06:57
  • @ρяσѕρєяK can you give me a sample, please – MH2538 Jan 02 '20 at 07:05
  • as this mention in handler ,as your code,it will all delay for 5 second ( or just few milisecond of for loops) cause as mention in docs **Causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses. The runnable will be run on the thread to which this handler is attached. The time-base is SystemClock.uptimeMillis(). Time spent in deep sleep will add an additional delay to execution.** – shadow Jan 02 '20 at 07:05
  • @MH2538: you can do it without for-loop also. using temp ArrayList and `ListIterator` for `lines` – ρяσѕρєя K Jan 02 '20 at 07:11

3 Answers3

1

You can use Timer for this

   // Declare globally
private int position = -1;

/**
 * This timer will call each of the seconds.
 */
Timer mTimer = new Timer();
mTimer.schedule(new TimerTask() {
    @Override
    public void run() {
        // As timer is not a Main/UI thread need to do all UI task on runOnUiThread
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                    // increase your position so new image will show
                position++;
                // check whether position increased to length then set it to 0
                // so it will show images in circuler
                if (position >= imageArray.length)
                    position = 0;
                // Set Image
                MyImageView.setImageResource(imageArray[position]);
            }
        });
    }
}, 0, 5000);
Sandeep dhiman
  • 1,863
  • 2
  • 17
  • 22
0

I think you expect your each gif played in interval 5000

so multiply your interval with a so you can place 5000,10000,15000

Handler handler1 = new Handler();
        for (int a = 1; a<=lines.size() ;a++) {
            handler1.postDelayed(new Runnable() {

                @Override
                public void run() {
                    String resName = "";
                    int duration = 0;
                    resName = "wo_" + (lines.get(slideIndex).split(",")[0]).toLowerCase().trim().replace(".gif", "");
                    duration = Integer.parseInt(lines.get(slideIndex++).split(",")[2].trim());
                    int resourceId = getResId(resName, R.drawable.class);//this.getResources().getIdentifier(resName, "drawable", this.getPackageName());

                    Glide.with(WorkoutActivity.this).asGif().load(resourceId).into(imageView);
                }
            }, a * 5000);
        }
Rajasekaran M
  • 2,478
  • 2
  • 20
  • 30
-1

but when I run the app, all gifs load very very fast so I can just see the last gif

Because you are posting all Runnable's in parallel.

Do it without for-loop:

  1. Create Handler Instance :

    Handler handler1 = new Handler();
    
  2. Create Runnable for Handler:

    Runnable mRunnable=new Runnable() {
            @Override
            public void run() {
                ListIterator<?> iter = tempLinesArray.listIterator();
                if(iter.hasNext()){
                    ? str = iter.next(); // item from lIst
                    // do your work here
                    ....
                    iter.remove(); // remove item from list.
                    Glide.with(WorkoutActivity.this).asGif().load(resourceId).listener(new RequestListener<GifDrawable>() {
                        ...
                        @Override
                        public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
                            handler1.postDelayed(mRunnable,5000);
                            return false;
                        }
                    }).into(imageView);
    
                }
            }
        };
    
  3. Finally call handler1.postDelayed :

    ArrayList<?> tempLinesArray=new ArrayList<>();
    tempLinesArray.addAll(lines);
    handler1.postDelayed(mRunnable,5000);
    

Make sure you are removing all callbacks from Handler when component is pause or stop:

handler1.removeCallbacks(null);
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213