Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid SurfaceView shows black screen
    primarykey
    data
    text
    <p>I am trying to draw on a surface view, but I get a black screen. My xml layout:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"&gt; &lt;com.example.surfaceview.MySurface android:id="@+id/padview" android:layout_width="match_parent" android:layout_height="match_parent" /&gt; &lt;/FrameLayout&gt; </code></pre> <p>Important parts of my code: MySurface.java</p> <p>public class MySurface extends SurfaceView implements SurfaceHolder.Callback {</p> <pre><code>public DrawingThread thread; public MySurface(Context context, AttributeSet attrs) { super(context, attrs); SurfaceHolder surfaceholder = getHolder(); surfaceholder.addCallback(this); thread = new DrawingThread(surfaceholder, context, new Handler() { @Override public void handleMessage(Message m) { // Do some handle thing } }); setFocusable(true); } @Override public void surfaceChanged(SurfaceHolder tholder, int format, int width, int height) { thread.setSurfaceSize(width, height); thread.holder = tholder; } @Override public void surfaceCreated(SurfaceHolder holder) { thread.running = true; thread.start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { boolean retry = true; thread.running = false; while(retry) { try { thread.join(); retry = false; } catch (InterruptedException e) { } } } public Thread getThread() { return thread; } public class DrawingThread extends Thread { private SurfaceHolder holder; private Context context; private Handler handle; private Paint background; private Rect blank; public boolean running; public boolean created; public int canvasheight; public int canvaswidth; public PadThread(SurfaceHolder tholder, Context tcontext, Handler thandler) { holder = tholder; context = tcontext; handle = thandler; // Temporary canvas dimentions canvaswidth = 1; canvasheight = 1; running = false; created = false; background = new Paint(); background.setColor(R.color.white); blank = new Rect(0, 0, canvaswidth, canvasheight); } @Override public void run() { Log.d("SurfaceView Test", "Drawing thread run"); while(running) { Canvas canvas = null; try { canvas = holder.lockCanvas(); synchronized(holder) { // update object states // get user input gestures drawing(canvas); } } finally { if(canvas != null) { holder.unlockCanvasAndPost(canvas); } } } } private void drawing(Canvas canvas) { // Clear screen canvas.drawRect(blank, background); // Draw Things } public void setSurfaceSize(int width, int height) { synchronized(holder) { canvaswidth = width; canvasheight = height; // New background rect blank.set(0, 0, canvaswidth, canvasheight); } } } </code></pre> <p>}</p> <p>The code is based off of Google's Lunar Landar SurfaceView example at <a href="http://developer.android.com/resources/samples/LunarLander/index.html" rel="nofollow">http://developer.android.com/resources/samples/LunarLander/index.html</a></p> <p>I know that all of the code is being reached through logging.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload