Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android drawing on surfaceview and canvas

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 :(

like image 343
ChHaupt Avatar asked Jun 07 '12 12:06

ChHaupt


People also ask

Can we draw directly on canvas in Android studio?

To draw onto a canvas in Android, you will need four things: A bitmap or a view — to hold the pixels where the canvas will be drawn. Canvas — to run the drawing commands on. Drawing commands — to indicate to the canvas what to draw.

What is canvas drawing in Android?

Canvas API is a drawing framework that is provided in Android, with the help of which we can create custom shapes like rectangle, circle, and many more in our UI design. With the help of this API, we can draw any type of shape for our app. The drawing of the different shapes is done using Bitmap.

When should I use SurfaceView?

By default, the newly created surface is placed behind the app UI surface. You can override the default Z-ordering to put the new surface on top. Rendering with SurfaceView is beneficial in cases where you need to render to a separate surface, such as when you render with the Camera API or an OpenGL ES context.

What is the use of canvas method in Android?

The Canvas class holds the "draw" calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, Path, text, Bitmap), and a paint (to describe the colors and styles for the drawing).


2 Answers

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);
    }
}
like image 190
Vipul Avatar answered Nov 15 '22 12:11

Vipul


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.

like image 32
cescom Avatar answered Nov 15 '22 13:11

cescom