简体   繁体   中英

Unable to draw on top of bitmap to which i set BG from resource

I am trying to set a background ti bitmap and then to be able to draw on top of it so later i will be able to save the bitmap to gallery.

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    paint.setStrokeWidth(changeWidth());
        if (bitmap == null)
               {            
                bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_4444);                   
               }

              //Boolean parameters allowing to change bg in case preference changed
            if(prefs_grid)
                {
                    bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.grid_view);

                }
            if(prefs_ruler)
                {
                    bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.rulers_view);
                }


            canvas = new Canvas();
            try {
                canvas.setBitmap(bitmap);
            } catch (Exception e) {
                e.printStackTrace();
                Log.w("setBitmap", "unable to set Bitmap");
            }
            super.onSizeChanged(w, h, oldw, oldh);
}

This is where i change the bitmap. The bitmap does change but the line: canvas.setBitmap(bitmap); doesn't work and i cant get it why? Can someone consult me on this issue.

Thanks,

try adding this

canvas = this.surfaceHolder.lockCanvas();

// try locking the canvas for exclusive pixel editing

// in the surface

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM