3

I have been searching online on how to turn what is on a canvas into a Bitmap. I have attempted multiple ways to do this like saving the drawing cache to a bitmap, but the end result is the background bitmap flashing on for a moment, then turning into a black screen. The test image is displayed over the background, but doesn't get overlaid by the background from the next calling of OnDraw.

MainThreading{

                        ...

              if(notuptodate == true){

                        //call readyBackground to create the background bitmap
                        this.mainPanel.readyBackground(canvas);
                        //post the canvas
                        surfaceHolder.unlockCanvasAndPost(canvas);
                        //clean the Canvas
                        canvas = null;
                        //ready new Canvas
                        canvas = this.surfaceHolder.lockCanvas();//lock the canvas to enable editing






                    }//if not true

                    if (MainThreading.notuptodate == false){

                        mainPanel.onDraw(canvas);


                    }//if false
...
                 }//mainthreading

    //this method is run first to create the Bitmap that on draw will use
    public void readyBackGround(Canvas canvas){


        if (MainThreading.notuptodate){
            //method used to draw multiple Bitmaps onto the canvas
            renderBackground(canvas);

            //private Bitmap, this is the supposed proper size of the bitmap
            backgroundBitmap=Bitmap.createBitmap(240, 320, Config.ARGB_8888);


            //set the canvas to turn whats on its self onto the bitmap.
            canvas.setBitmap(backgroundBitmap);

            //set boolean flag to false so renderBackground won't be called
            //untill it needs to be updated again
            MainThreading.notUpToDate = false;

        }

        //this method is called when notUpToDate = true
         @Override
    protected void onDraw(Canvas canvas){


        if (this.threado.getBackground() != null){
            canvas.drawBitmap(backgroundBitmap, 0, 0, null);

            }

            //this if statement is never activated
            if (this.threado.getBackground() == null){
                Log.d("background nonexistant", "Importante!");
            }
            //draw test object onto background
            test.DrawObject(canvas);

                //when MainThreading.notUpToDate = true isn't commented out, the
                //screen consistantly shows the background, but the test object isn't
                //drawn

        //MainThreading.notUpToDate = true;





        }
Chillie
  • 1,030
  • 10
  • 28
Andrew Raappana
  • 33
  • 1
  • 1
  • 3

2 Answers2

6

Try it this way...

- Create a bitmap of the correct size using Bitmap.createBitmap()

- Create a canvas instance pointing that this bitmap using Canvas(Bitmap) constructor

- Draw to the canvas

- Use the bitmap

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • At first that didn't appear to solve my problem but I found the error causing this. In the OnDraw Method you'll see that theres some if statements that were referencing some old code I forgot to remove. Thank you for your answer, it is greatly appreciated. – Andrew Raappana Oct 21 '12 at 06:41
  • 5
    copy/paste ... http://stackoverflow.com/questions/4013725/converting-a-canvas-into-bitmap-image-in-android – AlexAndro Aug 25 '14 at 08:32
1
public class CanvasToBitmap extends View {

    Paint paint = new Paint();
    Rect mRect = new Rect();
    Bitmap bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);

    public myCanvas( Context context ) {
        super(context);
        Canvas canvas = new Canvas(bitmap);
        draw(canvas);
    }

    @Override
    public void onDraw(Canvas canvas) {
        
        mRect.set(0, 0, 200, 200);
        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawRect(mRect, paint);
        canvas.setBitmap(bitmap);

        ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
        try{
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, mByteArrayOutputStream);

            bitmap = BitmapFactory.decodeStream(new ByteArrayInputStream(mByteArrayOutputStream.toByteArray()));
            mByteArrayOutputStream.close();
       } catch (Exception e) {e.printStackTrace();}
    }
}

Create a new instance:

CanvasToBitmap canvasToBitmap = new CanvasToBitmap(getBaseContext());

Get bitmap of canvasToBitmap:

Bitmap bitmap = canvasToBitmap.bitmap;
amiron
  • 721
  • 9
  • 11