我正在尝试制作延迟小于 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 它将创建一个位图并将其分配给画布,然后我开始在该画布上绘图,该位图将用于使图像视图无效。
如果我使用的是 mHandler,那么一切都可以正常工作,但是如果我使用的是 ScheduledThreadPoolExecutor(所以我将调用 startEffect 方法而不是 mHandler.post),那么我猜想在绘图发生后图像视图会显示为白色,我该如何解决这个问题问题。