Skip to content Skip to sidebar Skip to footer

Creating Unique Postdelayed Runnables In A Loop

I'm emulating a Frame Animation; I have it all working spare one issue. I have a for loop in which, on every iteration, it changes the Image of an ImageView after a delay. for(int

Solution 1:

Step #1: Define the Runnable as a data member of your activity (or wherever this code resides)

Step #2: Dump the Handler, as you don't need it -- postDelayed() is implemented on View as well

Step #3: Create a helper method that does the postDelayed() call -- I'll refer to that method as foo() here -- and call foo() where you right not call postDelayed()

Step #4: In run() of the Runnable, call foo() again to reschedule the Runnable for another delay period

Solution 2:

Give this a try, if it doesnt work then ill try somthing else:

for(inti=1; i <13; i++)
{
    if (stop== false)
    {
        StringimgName="b"+ Integer.toString(i);
        id = getResources().getIdentifier(imgName, "drawable", getPackageName());
        HandlerhandlerTimer=newHandler();
        handlerTimer.postDelayed(newRunnable(){
            publicvoidrun() {
                view.setImageDrawable((getResources().getDrawable(id)));
                view.invalidate();            
                }}, 300);

    }
}

invalidate() should cause your view to be repainted and you should get the desired effect. Hope this helps!

This is the Thread example:

publicclassMainimplementsRunnable
{
    publicMain()
    {
        Threadthread=newThread(this);
        thread.start();
        //new Thread starts at run()
    }

    publicvoidrun()
    {
        StringimgName="b"+ Integer.toString(i);
        id = getResources().getIdentifier(imgName, "drawable", getPackageName());
        try
        {
            for(inti=1; i <13&!stop; i++)
            {
                view.setImageDrawable((getResources().getDrawable(id)));
                Thread.sleep(300);
            }
        }catch(Exception e){e.printStackTrace();}
    }

    publicstaticvoidmain(String args[]){newMain();}
}

if you need anything else at all, just let me know!

Post a Comment for "Creating Unique Postdelayed Runnables In A Loop"