1

I'm trying to produce an animation with delay less than 1 milli second.

Based on my research, I found some answers about ScheduledThreadPoolExecutor.

Unfortunately, I applied the following code but it's not working as I expected..

public class MainActivity extends Activity {

ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);
private Handler mHandler = new Handler();

public void buttonClicked(View v){

    if(v.getId() == R.id.start_animation)
    {
        //Case1
        mHandler.post(animateImage);
        //Case2
        //startEffect();)
    }
}

private Runnable animateImage = new Runnable() {

    @Override
    public void run() {
        runOnUiThread(
        new Runnable()
        {
            public void run()
            {
                doTheAnimation1();
            }
        });
    }
};

private void doTheAnimation1() {
    doFlipImage();
}

private void startEffect()
{

    long delay = 1000; //the delay between the termination of one execution and the commencement of the next

    exec.scheduleAtFixedRate(animateImage, 0, delay, TimeUnit.MICROSECONDS);
}
}

According to the code, once the button is clicked the mHandler will call the animateImage, animateImage will doFlipImage which will create a Bitmap and assign it to the canvas and I start drawing over that canvas, and this bitmap will be used to invalidate an imageview.

if I'm using mHandler then everythings works fine, but if I'm using ScheduledThreadPoolExecutor (so I will call startEffect method instead of mHandler.post), then the imageview appears white after the drawings happened as I guess, How could I solve this issue.

Prem
  • 4,823
  • 4
  • 31
  • 63
ManKeer
  • 543
  • 2
  • 6
  • 27
  • 1
    "How could I solve this issue" -- don't use any of this for your animations. You are not going to get millisecond-level precision using Java and the `View` system, due to the nature of the main application thread. You will have to write this the way most games are written, whether directly using C/C++ and OpenGL, or by using some game development framework that in turn uses C/C++. – CommonsWare Jan 04 '15 at 15:07

0 Answers0