我正在构建一个执行逐帧动画的动态壁纸应用程序。当我测试应用程序时,我的动画会出现在屏幕上,但在绘制新帧之前帧并不总是清晰。
我打算让动画向前绘制到最后一帧,然后向后绘制到第一帧。但有时,动画似乎在继续正确的顺序之前向前或向后绘制一帧或两帧。
我以这种方式将资源ID加载到我的图像:
private int[] res_id = {R.drawable.flag_1, R.drawable.flag_2, R.drawable.flag_3, R.drawable.flag_4, R.drawable.flag_5,
R.drawable.flag_6, R.drawable.flag_7, R.drawable.flag_8, R.drawable.flag_9, R.drawable.flag_10,
R.drawable.flag_11, R.drawable.flag_12, R.drawable.flag_13, R.drawable.flag_14, R.drawable.flag_15,
R.drawable.flag_16, R.drawable.flag_17, R.drawable.flag_18, R.drawable.flag_19, R.drawable.flag_20,
R.drawable.flag_21, R.drawable.flag_22, R.drawable.flag_23, R.drawable.flag_24, R.drawable.flag_25,
R.drawable.flag_26, R.drawable.flag_27, R.drawable.flag_28, R.drawable.flag_29, R.drawable.flag_30};
我循环浏览图像并像这样绘制它们:
void drawFlag(Canvas cFlagCanvas)
{
Bitmap bFlagFrame;
long time = 0;
if(bStartPlaying == true)
{
iFrameCount = 0;
bStartPlaying = false;
bIsPlaying = true;
//Load the first frame, decode it, and scale it to the screen.
bLenguaFrame = decodeLLResource(mContext.getResources(), res_id[iFrameCount], 400, 300);
cFlagCanvas.drawBitmap(bFlagFrame, iCenterX, iCenterY, null);
last_tick = System.currentTimeMillis();
iFrameCount++;
bLastFrame = bFlagFrame;
bFlagFrame.recycle();
}
else if (bIsPlaying == true)
{
if(iFrameCount == 0 || iFrameCount == 29)
{
iDelay = FirstLastDelay; //set to 2000 milli-seconds
}
else
{
iDelay = TweenDelay; //set to 120 milli-seconds
}
try
{
bFlagFrame = decodeResource(mContext.getResources(), res_id[iFrameCount], 400, 300);
time = (System.currentTimeMillis() - last_tick);
//Draw the next frame only if the delay time has passed.
if (time >= iDelay)
{
cFlagCanvas.drawBitmap(bFlagFrame, iCenterX, iCenterY, null);
last_tick = System.currentTimeMillis();
bLastFrame = bFlagFrame;
bFlagFrame.recycle();
//Adjust the frame count
if(bPlayingForward == true)
{
if(iFrameCount < 29)
{
iFrameCount++;
bPlayingForward = true;
}
else if(iFrameCount == 29)
{
iFrameCount--;
bPlayingForward = false;
}
}
else
{
if(iFrameCount > 0)
{
iFrameCount--;
bPlayingForward = false;
}
else if(iFrameCount == 0)
{
iFrameCount++;
bPlayingForward = true;
}
}
}
//Delay not expired. Redraw the last frame.
else
{
cFlagCanvas.drawBitmap(bLastFrame, iCenterX, iCenterY, null);
}
}
catch(Exception e)
{
}
}
}
我得到的是一个可以加载和播放的动画,而不会增加我的记忆力。但是,在绘制新图像之前,不会从屏幕上清除较旧的图像。如何确保画布一次只显示一个帧?
提前感谢您提供的任何帮助。
答案 0 :(得分:0)
在绘制每个帧之前,只需使用Canvas.drawColor()以您想要的任何颜色清除画布(http://developer.android.com/reference/android/graphics/Canvas.html#drawColor(int))