postDelay vs ScheduledThreadPoolExecutor

时间:2015-01-04 14:56:45

标签: java android handler scheduler postdelayed

我试图制作延迟小于1毫秒的动画。

根据我的研究,我找到了一些关于ScheduledThreadPoolExecutor的答案。

不幸的是,我应用了以下代码,但它没有像我预期的那样工作..

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);
}
}

根据代码,一旦单击按钮,mHandler将调用animateImage,animateImage将执行doFlipImage,它将创建一个Bitmap并将其分配给画布,然后我开始绘制该画布,这个位图将用于无效一个imageview。

如果我使用mHandler,那么每一件事都可以正常工作,但是如果我使用ScheduledThreadPoolExecutor(所以我将调用startEffect方法而不是mHandler.post),那么在图纸发生后,imageview会显示为白色我猜怎么能解决这个问题。

0 个答案:

没有答案
相关问题