Surface View仅在断点调试中绘制

时间:2017-07-01 12:14:12

标签: android canvas surfaceview surfaceholder android-graphics

我想控制来自MainActivity的Surface View上的绘图。 (用户)项目的Github链接在帖子的末尾给出。

  

MainActivity在SurfaceView实例上调用draw(Drawing)方法。

我有以下代码,表面视图仅在我启用断点调试时绘制。

我已访问过这些链接,未找到解决方案:

Android SurfaceView empty

Drawing using the Surfaceview creates blank screen

Android drawing on surfaceview and canvas

我下载的教程:

Drawing Triangles, Rhombuses and Other Custom Shapes on an Android Canvas

Android Coding - Drawing on SurfaceView

我认为这是一个视图创建过程中的问题所以我添加了创建检查:

public void draw(Drawing drawing) {    

        this.drawing = drawing;    

        if (surfaceHolder.isCreating()) {

            surfaceHolder.addCallback(new SurfaceHolder.Callback() {
                @Override
                public void surfaceCreated(SurfaceHolder holder) {   

                    thread = new Thread(LushSurfaceView.this);
                    thread.start();    
                }    

            });


        } else {


            thread = new Thread(this);
            thread.start();   

        }    
    }

这是主题:

 @Override
    public void run() {
        if (drawing != null) {


            switch (drawing) {


                case RECT:
                    drawRectShape();
                    break;
            }


        }
    }

另外,我故意添加延迟等待表面视图初始化。

private void drawRectShape() {

        if (surfaceHolder.getSurface().isValid()) {


            Canvas canvas = surfaceHolder.lockCanvas();
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if (canvas != null && !surfaceHolder.isCreating()) {
                Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
                paint.setStyle(Paint.Style.STROKE);
                paint.setStrokeWidth(30);


                Random random = new Random();

                int w = canvas.getWidth();
                int h = canvas.getHeight();
                int left = Math.round(w/2);
                int top = Math.round(h/2);
                int right = 200;
                int bottom =200;

                int r = random.nextInt(255);
                int g = random.nextInt(255);
                int b = random.nextInt(255);
                paint.setColor(0xff000000 + (r << 16) + (g << 8) + b);
                canvas.drawRect(new Rect(left, top, right, bottom), paint);

                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }

    }
  • 代码在以下情况下有效:我启用了断点调试。
  • 简单调试不会显示任何内容。
  • 我多次重新创建这个场景。

我正在使用Nexus 6(OS 7.1.1)作为测试设备。 (不是模仿者)。

项目来源github

0 个答案:

没有答案