0
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 帧跳过..
请帮助

4

1 回答 1

1

嗯,这不是我的编码风格,所以我决定你需要稍微简化一下。您使用了 android api 指南,但它们很烂。

观看以下视频,您应该会没事的。

http://www.youtube.com/watch?v=wUmId0rwsBQ&list=SP2F07DBCDCC01493A&index=67

http://www.youtube.com/watch?v=0wy907WZFiA&list=SP2F07DBCDCC01493A

http://www.youtube.com/watch?v=ZMcYbf9Hhe4&list=SP2F07DBCDCC01493A

http://www.youtube.com/watch?v=yowNavIDzzE&list=SP2F07DBCDCC01493A

于 2013-07-21T04:53:45.560 回答