LKB
LKB

Reputation: 1040

How to draw on a SurfaceView in Android

I'm wanting to draw a Rect onto a SurfaceView when a user touches the screen. When they move their finger, it moves the Rect along with it. The user can also size the Rect. Basically it's a selection tool.

So far I have the following code:

private void selectPoints(){
    //retrieve X and Y values from touch
    surfaceView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent pos) {
            int eventAction = pos.getAction();
            switch (eventAction) { 
                case MotionEvent.ACTION_DOWN:
                    Log.d("X",String.valueOf(pos.getX()));
                    Log.d("Y",String.valueOf(pos.getY()));  
                    break; 

                case MotionEvent.ACTION_UP: 
                    Log.d("X",String.valueOf(pos.getX()));
                    Log.d("Y",String.valueOf(pos.getY()));
                    break;
            }       

            return true;                        
        }
    });     
}

However I'm unsure of how to draw onto the SurfaceView. Any advice/help?

Thank you!

Upvotes: 0

Views: 1778

Answers (2)

Raj
Raj

Reputation: 1148

Got it. I am using SurfaceView for my custom camera in Android. I am not sure whether this will help you or not. But this is how I do it:

public class CameraPreview extends SurfaceView{

  private SurfaceHolder mHolder;
  private Camera mCamera;      

  public CameraPreview(Context context, Camera camera) {
     super(context);
     // Rest of the code for initialization
  }

  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
     // set preview size and make any resize, rotate or
    // reformatting changes here

    // start preview with new settings
    try {
        mCamera.setPreviewDisplay(mHolder);
        mCamera.startPreview();

    } catch (Exception e){
        Log.e("CameraPreview : surfaceChanged()", "Error starting camera preview: " + e.getMessage(),e);
    }
  }

  @Override
  public void surfaceCreated(SurfaceHolder holder) {
    try {
        if (mCamera != null) {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        }
    } catch (IOException e) {
        Log.e("CameraPreview : surfaceCreated()", "Error setting camera preview: " + e.getMessage(), e);
    }
  }

  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
  }

Then in my activity I initialize the SurfaceView as follows:

cameraPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);

See if there is any useful extract from this piece of code.

Upvotes: 1

Raj
Raj

Reputation: 1148

Implement a renderer from GLSurfaceView.Renderer interface and provide implementation for the following methods:

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    // TDOD
}

public void onSurfaceChanged(GL10 gl, int w, int h) {
    // TDOD
}

public void onDrawFrame(GL10 gl) {      
    // You would draw here
}

The onDrawFrame() method can be called by your surfaceView.requestRender();

Upvotes: 1

Related Questions