2

我在 SurfaceView 上使用 canvas.drawText 并且输出在模拟器上看起来是正确的,但是当我将应用程序部署到我的设备(三星 Galaxy S3)时,文本是从上到下写的,如下所示:

T
E
s
t

设备三星 Galaxy S3 上的错误输出

看起来像在文本的每个字符之后添加了一个换行符。

设备是否是横向的并不重要,它永远不会工作,我不知道为什么。

我究竟做错了什么?

模拟器正确显示输出

在 AndroidManifest.xml 我使用:

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="8" />

我正在使用的代码:

public class MainActivity extends Activity {

    DemoView renderView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        renderView = new DemoView(this);
        setContentView(renderView);
    }

    @Override
    public void onResume() {
        super.onResume();
        renderView.resume();
    }

    @Override
    public void onPause() {
        super.onPause();
        renderView.pause();
    }

    private class DemoView extends SurfaceView implements Runnable{
        Thread renderThread = null;
        SurfaceHolder holder;
        volatile boolean running = false;

        public DemoView(Context context){
            super(context);
            this.holder = getHolder();
        }

        public void resume() {
            running = true;
            renderThread = new Thread(this);
            renderThread.start();
        }

        public void run() {
            Canvas canvas;

            while(running) {
                if(!holder.getSurface().isValid())
                    continue;
                Paint test = new Paint(Color.YELLOW);
                test.setColor(Color.YELLOW);
                canvas = holder.lockCanvas();
                canvas.drawText("TEst", 10, 10, test);
                holder.unlockCanvasAndPost(canvas);
            }
        }

        public void pause() {
            running = false;
            while(true) {
                try {
                    renderThread.join();
                    break;
                } catch (InterruptedException e) { // retry
                }
            }
        }
    }
}

谢谢你的帮助!斯蒂芬妮

4

2 回答 2

1

你的使用SurfaceHolder很奇怪。我认为意外的行为是由于这个原因。这就是我过去使用 SurfaceView 的方式:

public void run() {
        Canvas canvas;

        while(running) {
            try {
              if(!holder.getSurface().isValid())
                continue;
              canvas = holder.lockCanvas();
              synchronized (surface) {
                //Code to draw text/etc
              }
            } catch (...) {
            } finally {
              holder.unlockCanvasAndPost(canvas);
            }                
        }
    }

请注意您缺少的lockCanvas表达式以实际分配画布并与unlockCanvasAndPost

于 2013-04-04T16:28:09.620 回答
0

问题是我初始化Paint test错误!

错误的:

Paint test = new Paint(Color.YELLOW);

正确的:

Paint test = new Paint();
test.setColor(Color.YELLOW);

一件很小的事,却有很大的作用!

于 2013-04-06T08:40:46.507 回答