0

我正在构建一个执行逐帧动画的动态壁纸应用程序。当我测试应用程序时,我的动画出现在屏幕上,但在绘制新帧之前帧并不总是清除。

我打算让动画向前绘制到最后一帧,然后向后绘制到第一帧。但是,有时,动画似乎会在以正确的顺序继续之前向前或向后绘制一两帧。

我以这种方式将资源 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)
            {

            }
        }                                      
    }

我得到的是一个动画,它可以加载和播放而不会占用我的记忆。但是,在绘制新图像之前,旧图像不会从屏幕上清除。如何确保画布一次只显示一帧?

提前感谢您提供的任何帮助。

4

1 回答 1

0

Just use Canvas.drawColor() to clear the canvas with whatever color you want before you draw each frame (http://developer.android.com/reference/android/graphics/Canvas.html#drawColor(int))

于 2013-07-02T22:35:44.037 回答