11

I have a "simple" problem. I try to draw on surfaceview. Layout-XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:keepScreenOn="true"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <SurfaceView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/imagesurface"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:background="#00ff00">
    </SurfaceView>

</LinearLayout>

The Activity is a SurfaceHolder.Callback:

public class OpenCvonAndroidGTDforHOGActivity extends Activity implements SurfaceHolder.Callback{

    private SurfaceHolder _surfaceHolder;
    private SurfaceView _surfaceView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        _surfaceView = (SurfaceView)findViewById(R.id.imagesurface);
        _surfaceHolder = _surfaceView.getHolder();
        _surfaceHolder.addCallback(this);
        _surfaceView.setWillNotDraw(false);

    }

    protected void onDraw(Canvas canvas) {
        canvas.drawRGB(255, 0, 255);            
    }

    public void surfaceCreated(SurfaceHolder holder) {
        Canvas canvas = null;
        try {
            canvas = holder.lockCanvas();
            synchronized(holder) {
                onDraw(canvas);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (canvas != null) {
                holder.unlockCanvasAndPost(canvas);
            }
        }
    }


    public void showToast(String msg) {
        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
    }
}

If i call

_surfaceview.setBackground(Color.RED) in onDraw(...) it works. But

canvas.drawRGB(255, 0, 255) doesn't work :(

Zaur Nasibov
  • 21,620
  • 11
  • 51
  • 80
ChHaupt
  • 343
  • 3
  • 6
  • 17

3 Answers3

24

Following Snippet will help you.

public class SurfaceDemo extends Activity implements SurfaceHolder.Callback {

    private static final String TAG = "Svetlin SurfaceView";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SurfaceView view = new SurfaceView(this);
        setContentView(view);
        view.getHolder().addCallback(this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        tryDrawing(holder);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int frmt, int w, int h) { 
        tryDrawing(holder);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {}

    private void tryDrawing(SurfaceHolder holder) {
        Log.i(TAG, "Trying to draw...");

        Canvas canvas = holder.lockCanvas();
        if (canvas == null) {
            Log.e(TAG, "Cannot draw onto the canvas as it's null");
        } else {
            drawMyStuff(canvas);
            holder.unlockCanvasAndPost(canvas);
        }
    }

    private void drawMyStuff(final Canvas canvas) {
        Random random = new Random();
        Log.i(TAG, "Drawing...");
        canvas.drawRGB(255, 128, 128);
    }
}
Vipul
  • 31,716
  • 7
  • 69
  • 86
  • It works. But why doesn't work with surfaceview from the xml-layout? – ChHaupt Jun 07 '12 at 12:58
  • 2
    its because you have mentioned android:background="#00ff00".If you remove it then your code will work.although i dont know what is reason. – Vipul Jun 07 '12 at 13:11
  • Thx. It works without android:background="#00ff00". No matter why. – ChHaupt Jun 07 '12 at 13:18
  • Adding later to the post its because u are just adding color on the background of a color – Terril Thomas Aug 28 '13 at 11:51
  • 1
    why do you need string `Random random = new Random();` in void `drawMyStuff(final Canvas canvas)` ?) – nick Jan 22 '17 at 09:28
  • Im pretty sure that "Random random = new Random();" is just there for example code that you can add you yourself. You can remove it if you wish. – Diarmaid O Cualain Jun 15 '18 at 08:50
  • The Randon code can be used to fill the canvas with a random colour. canvas.drawRGB(random.nextInt(255), random.nextInt(255), random.nextInt(255)); – eatyourgreens Aug 31 '18 at 09:27
3

SurfaceView is composed by a View in your current layout and a surface under your layout. If you set a background to the view, you won't see anything of what is happening on the surface.

Reaz Murshed
  • 22,528
  • 12
  • 75
  • 92
cescom
  • 106
  • 6
-2

Did you try

yourHolder.unlockCanvassAndPost(your_canvas);
Kartoch
  • 7,426
  • 9
  • 38
  • 66
Aytunc MATRAC
  • 95
  • 1
  • 8