i want to add image on surfaceview and make it as clickable. I could do it via xml using layout. But i am not able to do it dynamically from code. I even cannot use lockcanvas to draw image on canvas since i set my SurfaceHolder type to SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS.So what i have to do to draw image on surfaceview in my app. Pls guide me?
Asked
Active
Viewed 2.4k times
1 Answers
12
Some thing like that
public class MSurface extends SurfaceView implements SurfaceHolder.Callback {
public MSurface(Context context) {
super(context);
getHolder().addCallback(this);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Bitmap icon = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(icon, 10, 10, new Paint());
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Canvas canvas = null;
try {
canvas = holder.lockCanvas(null);
synchronized (holder) {
onDraw(canvas);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (canvas != null) {
holder.unlockCanvasAndPost(canvas);
}
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}

Nam Vu
- 5,669
- 7
- 58
- 90
-
1how to set the image at the center of surfaceview nad how to strech it to whole surfaceview – Sunishtha Singh Apr 30 '15 at 04:32
-
How to scale the bitmap or fill/streach to cover the entire surfaceView? – TomeeNS Jul 16 '16 at 15:25
-
similar question with above? – kemdo May 09 '17 at 08:19