表面视图不绘制anthing ...框架跳过消息

时间:2013-07-21 04:43:41

标签: android surfaceview

public class CannonView extends SurfaceView implements SurfaceHolder.Callback {

    CannonThread cannonThread;
    private Paint blockerPaint;

    public CannonView(Context context, AttributeSet attrs) {
        super(context, attrs);
        blockerPaint = new Paint();
        blockerPaint.setStrokeWidth(10.0f);
        getHolder().addCallback(this);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

        cannonThread = new CannonThread(holder);
        cannonThread.running(true);
        cannonThread.start();

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        boolean retry = true;
        cannonThread.running(false);
        while (retry) {

            try {
                cannonThread.join();
                retry = false;
            } catch (InterruptedException e) {

            }

        }

    }

    private class CannonThread extends Thread {

        boolean setRunning;
        SurfaceHolder surfaceHolder;

        public CannonThread(SurfaceHolder holder) {
            setRunning = true;
            surfaceHolder = holder;

        }

        public void running(boolean isRunning) {
            setRunning = isRunning;
        }

        @Override
        public void run() {
            Canvas canvas = null;

            while (setRunning) {

                try {

                    canvas = surfaceHolder.lockCanvas();


                        synchronized (surfaceHolder) {

                            canvas.drawLine(0, 0, 100, 100, blockerPaint);

                        }
                } finally {
                    if (canvas != null)
                        surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }

        }

    }
}

虽然上面的代码非常简单  但它没有在我的活动上画任何东西..
Logcat说我在主线程上做了太多工作..62帧跳过..
请帮忙

1 个答案:

答案 0 :(得分:1)