Application error when drawing to SurfaceView

Posted by DKDiveDude on Stack Overflow See other posts from Stack Overflow or by DKDiveDude
Published on 2011-01-07T14:50:26Z Indexed on 2011/01/07 14:54 UTC
Read the original article Hit count: 244

Filed under:
|
|
|
|

I'm am doing a simple coding attempt trying to draw on a SurfaceView created on my main.xml layout. I can change background color and display an icon fine, but when I try to draw I get an error. I am a newbie so obvious I am missing something, please lent a helping hint, thanks!

main.xml

<?xml version="1.0" encoding="utf-8"?>

    <SurfaceView android:id="@+id/Paper"
        android:layout_height="fill_parent" 
        android:layout_width="fill_parent">
    </SurfaceView>

and code here;

package com.example.SurfaceViewTest;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class SurfaceViewTest extends Activity implements SurfaceHolder.Callback {

    private SurfaceView mSurfaceView;
    private SurfaceHolder mSurfaceHolder;
    private Paint paint;
    private Canvas canvas;
    Bitmap mDrawing;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        mSurfaceView = (SurfaceView) this.findViewById(R.id.Paper);

        mSurfaceHolder = mSurfaceView.getHolder();

        mSurfaceHolder.addCallback(this);

        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // TODO Auto-generated method stub
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mSurfaceView.setBackgroundColor(Color.rgb(0, 255, 0));
        //mSurfaceView.setBackgroundResource(R.drawable.icon);

        canvas = holder.lockCanvas(null);

        mDrawing = Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565);

        canvas.setBitmap(mDrawing);

        paint = new Paint();
        paint.setColor(Color.rgb(255, 255,255));

        canvas.drawLine(1,1,200,300, paint);

        holder.unlockCanvasAndPost(canvas);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
    }

}

© Stack Overflow or respective owner

Related posts about android

Related posts about Xml