Domanda

I use canvas.drawText on a SurfaceView and the output looks correct on the emulator but when I deploy the app to my device (a Samsung Galaxy S3) the text is written from top to bottom like this:

T
E
s
t

Wrong output on Device Samsung Galaxy S3

It looks like a line-break is added after each character of the text.

It doesn't matter if the device is landscape or not, it just never works and I can't figure out why.

What am I doing wrong?

Emulator shows output correctly

In AndroidManifest.xml I use:

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

The code I'm using:

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
                }
            }
        }
    }
}

Thanks for your help! Stephoid

È stato utile?

Soluzione

Your use of SurfaceHolder is pretty weird. I'm thinking the unintended behavior is due to that. This is how I've used SurfaceView in the past:

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);
            }                
        }
    }

Note your missing lockCanvas expression to actually assign canvas and pair with unlockCanvasAndPost

Altri suggerimenti

The Problem was that I initialized Paint test wrong!

WRONG:

Paint test = new Paint(Color.YELLOW);

Correct:

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

A very small thing but had big effect!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top