动态壁纸,帧动画,滞后

时间:2013-12-12 18:42:19

标签: java android animation frame lag

我的画面动画滞后。为什么会滞后?

代码:

public class LiveWallpaperService extends WallpaperService {

    Bitmap mPic;
    private final Handler mHandler = new Handler();

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public Engine onCreateEngine() {
        return new CubeEngine();
    }

    class CubeEngine extends Engine {

        private float mPosY, mPosX;

        private Matrix mMatrix = new Matrix();
        private int k = 1;
        private boolean mAnime = true;

        private boolean test;
        private static final int NUM_RES = 30;

        private final Runnable mDrawAnim = new Runnable() {
            public void run() {

                for (int i = 0; i < 31; i++) {
                    k++;
                    drawFrame();

                    if (i == 30) {
                        k = 1;
                    }

                }

            }

        };

        private boolean mVisible;

        CubeEngine() {

            Resources res = getResources();
            for (int i = 0; i < NUM_RES; i++) {
                int id = res.getIdentifier("boot_00" + (100 + (i + 1)),
                        "drawable", "com.example.dxd1");
                mPic = BitmapFactory.decodeResource(res, id);

            }

        }

        @Override
        public void onCreate(SurfaceHolder surfaceHolder) {

            super.onCreate(surfaceHolder);

            setTouchEventsEnabled(false);
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            mHandler.removeCallbacks(mDrawAnim);
        }

        @Override
        public void onVisibilityChanged(boolean visible) {
            mVisible = visible;
            if (visible) {
            drawFrame();
            }else {
                mHandler.removeCallbacks(mDrawAnim);
            }
        }

        @Override
        public void onSurfaceChanged(SurfaceHolder holder, int format,
                int width, int height) {
            super.onSurfaceChanged(holder, format, width, height);

            float w = mPic.getWidth();
            float h = mPic.getHeight();
            float s = width / (float) w;
            float s2 = height / (float) h;

            mMatrix.reset();
            mMatrix.setScale(s, s2);


        }

        @Override
        public void onSurfaceCreated(SurfaceHolder holder) {
            super.onSurfaceCreated(holder);

        }

        @Override
        public void onSurfaceDestroyed(SurfaceHolder holder) {
            super.onSurfaceDestroyed(holder);
            mVisible = false;
            mHandler.removeCallbacks(mDrawAnim);
        }

        public void updateBG() {

            if (k == 1) {
                mPic = BitmapFactory.decodeResource(getResources(),
                        R.drawable.boot_00101);

            }
            if (k == 2) {
                mPic = BitmapFactory.decodeResource(getResources(),
                        R.drawable.boot_00102);

            }
            if (k == 3) {
                mPic = BitmapFactory.decodeResource(getResources(),
                        R.drawable.boot_00103);

            }
            if (k == 4) {
                mPic = BitmapFactory.decodeResource(getResources(),
                        R.drawable.boot_00104);

            }
            if (k == 5) {
                mPic = BitmapFactory.decodeResource(getResources(),
                        R.drawable.boot_00105);

            }
            if (k == 6) {
                mPic = BitmapFactory.decodeResource(getResources(),
                        R.drawable.boot_00106);

            }
            if (k == 7) {
                mPic = BitmapFactory.decodeResource(getResources(),
                        R.drawable.boot_00107);

            }
            if (k == 8) {
                mPic = BitmapFactory.decodeResource(getResources(),
                        R.drawable.boot_00108);

            }
            if (k == 9) {
                mPic = BitmapFactory.decodeResource(getResources(),
                        R.drawable.boot_00109);

            }
            if (k == 10) {
                mPic = BitmapFactory.decodeResource(getResources(),
                        R.drawable.boot_00110);

            }
            if (k == 11) {
                mPic = BitmapFactory.decodeResource(getResources(),
                        R.drawable.boot_00111);

            }
            if (k == 12) {
                mPic = BitmapFactory.decodeResource(getResources(),
                        R.drawable.boot_00112);

            }
            if (k == 13) {
                mPic = BitmapFactory.decodeResource(getResources(),
                        R.drawable.boot_00113);

            }
            if (k == 14) {
                mPic = BitmapFactory.decodeResource(getResources(),
                        R.drawable.boot_00114);

            }

            if (k == 15) {
                mPic = BitmapFactory.decodeResource(getResources(),
                        R.drawable.boot_00115);

            }
            if (k == 16) {
                mPic = BitmapFactory.decodeResource(getResources(),
                        R.drawable.boot_00116);

            }
            if (k == 17) {
                mPic = BitmapFactory.decodeResource(getResources(),
                        R.drawable.boot_00117);

            }
            if (k == 18) {
                mPic = BitmapFactory.decodeResource(getResources(),
                        R.drawable.boot_00118);

            }
            if (k == 19) {
                mPic = BitmapFactory.decodeResource(getResources(),
                        R.drawable.boot_00119);

            }
            if (k == 20) {
                mPic = BitmapFactory.decodeResource(getResources(),
                        R.drawable.boot_00120);

            }
            if (k == 21) {
                mPic = BitmapFactory.decodeResource(getResources(),
                        R.drawable.boot_00121);

            }
            if (k == 22) {
                mPic = BitmapFactory.decodeResource(getResources(),
                        R.drawable.boot_00122);

            }
            if (k == 23) {
                mPic = BitmapFactory.decodeResource(getResources(),
                        R.drawable.boot_00123);

            }
            if (k == 24) {
                mPic = BitmapFactory.decodeResource(getResources(),
                        R.drawable.boot_00124);

            }
            if (k == 25) {
                mPic = BitmapFactory.decodeResource(getResources(),
                        R.drawable.boot_00125);

            }
            if (k == 26) {
                mPic = BitmapFactory.decodeResource(getResources(),
                        R.drawable.boot_00126);

            }
            if (k == 27) {
                mPic = BitmapFactory.decodeResource(getResources(),
                        R.drawable.boot_00127);

            }
            if (k == 28) {
                mPic = BitmapFactory.decodeResource(getResources(),
                        R.drawable.boot_00128);

            }
            if (k == 29) {
                mPic = BitmapFactory.decodeResource(getResources(),
                        R.drawable.boot_00129);

            }
            if (k == 30) {
                mPic = BitmapFactory.decodeResource(getResources(),
                        R.drawable.boot_00130);

            }

        }

        void drawFrame() {
            final SurfaceHolder holder = getSurfaceHolder();

            Canvas c = null;

            c = holder.lockCanvas();
            if (c != null) {

                drawAnim(c);

            }

            if (c != null) {
                holder.unlockCanvasAndPost(c);
            }
            // Reschedule the next redraw
            mHandler.removeCallbacks(mDrawAnim);
            if (mVisible && mAnime) {
                mHandler.postDelayed(mDrawAnim, 50);
            }

        }

        void drawAnim(Canvas c) {

            updateBG();

            c.drawBitmap(mPic, mMatrix, null);

        }

    }

1 个答案:

答案 0 :(得分:1)

在绘制线程上解码位图非常重,肯定会导致延迟。您不能只为每个帧绘制带有全屏位图的动画。您将不得不缓存它们(不太可能,因为您将用完全屏幕位图的RAM),或者您必须以编程方式绘制内容(所以动态生成的图像)​​或仅使用部分位图/图层(不是他们叫精灵?)并移动这些部分。

我为客户开发了一个带有位图的全屏动画介绍的应用程序,在大多数设备上它不会达到5fps,而(原始)iphone版本达到了更多。我只使用了h264编码的电影(其他兼容性问题)。所以,这种方式不起作用。

所以,主要的问题是,为什么你使用30位图来绘制东西?你不能使用图层,所以你使用较小的位图?也许你自己动画一下?为什么不使用电影而不是手动动画?你在使用全屏图像吗?

如果您不使用全屏图像,可能会实现某种预缓存,因此您在渲染之前已经将2个图像解码为位图,同时保持缓存更新。