1

I have the code below that saves a user's drawing. It's a part of my paint app. Now, the canvas' background color is white and I want it to be an image.

For example I have an image of a house and I want to draw something on top of it.

@Override
public void run() {
    Canvas canvas = null;
    while (_run){
        if(isDrawing == true){
            try{
                canvas = mSurfaceHolder.lockCanvas(null);
                if(mBitmap == null){
                    mBitmap =  Bitmap.createBitmap (1, 1, Bitmap.Config.ARGB_8888);
                }
                final Canvas c = new Canvas (mBitmap);

                c.drawColor(0, PorterDuff.Mode.CLEAR);
                canvas.drawColor(0, PorterDuff.Mode.CLEAR);
                canvas.drawColor(0xffffffff);

                commandManager.executeAll(c,previewDoneHandler);
                previewPath.draw(c);

                canvas.drawBitmap (mBitmap, 0,  0,null);
            } finally {
                mSurfaceHolder.unlockCanvasAndPost(canvas);
            }


        }

    }

}

Thanks a lot for any help! :)

Mico
  • 489
  • 4
  • 9
  • 26
  • I'm not really getting your question, you want to draw an image on the Canvas? It seems like you already are drawing an image on the canvas. In the line `canvas.drawBitmap(mbBitmap, 0, 0, null);` you're already drawing it. If you want the image to be the background, just make sure that it's the first thing that's drawn and everything else gets drawn above it. – Brian Sep 16 '11 at 21:17
  • Thanks for your response @AeroDroid. "If you want the image to be the background, just make sure that it's the first thing that's drawn and everything else gets drawn above it." - You're right this is what I want. any ideas how to do that? I'm able to draw on the canvas but unable to make an image as its background... – Mico Sep 21 '11 at 17:30
  • Oh, what I meant was put `canvas.drawBitmap(mBitmap, 0, 0, null)` after the `if` statement. I'm not sure if this is exactly what you're trying to achieve, but worth a try. – Brian Sep 22 '11 at 04:21
  • if I put `canvas.drawBitmap(mBitmap, 0, 0, null)` after the `if` statement, it would mean that `mBitmap` will be a url of the background image? and then the second `canvas.drawBitmap (mBitmap, 0, 0,null);`, its `mBitmap` will be the user's drawing? – Mico Oct 07 '11 at 03:26

2 Answers2

2

To change the background of the canvas, type this line in onCreate() of your mainActivity.java

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
    drawView.setBackground(getResources().getDrawable(R.drawable.background));
}

Also, you can try doing this in your onDraw() however the performance will greatly reduce

canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.bg1),0,0,null)
Fabian N.
  • 3,807
  • 2
  • 23
  • 46
onexf
  • 3,674
  • 3
  • 22
  • 36
1

I think your looking for this:

Drawable image on a canvas

Community
  • 1
  • 1
zwebie
  • 2,349
  • 1
  • 27
  • 43