Android Scaling Canvas Bitmap

24,334

Solution 1

canvas.drawBitmap(out, null, thumbnailRectF, thumbCanvasPaint); 

should change to

canvas.drawBitmap(out, new Rect(0,0,mBitmap.getWidht, mBitmap.getheight), thumbnailRectF, thumbCanvasPaint);

There is no need for

Bitmap out = Bitmap.createScaledBitmap(mBitmap, (int) thumbWidth, (int)....

Also check that mScaled is true all the time when zoom is greater than 1

Solution 2

Scale bitmap by Bitmap.createScaledBitmap then draw will not work

The solution for scale the canvas bitmap is use this function (from the docs)

void drawBitmap (Bitmap bitmap,  Rect src, Rect dst, Paint paint)
// dst : Rect: The rectangle that the bitmap will be scaled/translated to fit into

so by changing the size of dst, your bitmap size will change

Here is example if I want to draw a bitmap at top-left and scale it to 100px x 120px

Bitmap bitmap = BitmapFactory.decodeResource(...);//bitmap to be drawn

float left = 0;
float top = 0;
RectF dst = new RectF(left, top, left + 100, top + 120); // width=100, height=120

canvas.drawBitmap(bitmap, null, dst, null);
Share:
24,334
Unpossible
Author by

Unpossible

Updated on July 09, 2022

Comments

  • Unpossible
    Unpossible almost 2 years

    I have a drawing app that allows the user to draw to a blank canvas. I am attempting to draw a scaled 'thumbnail' of the current bitmap so that when the user has scaled in the View, they can reference the thumbnail to get a sense as to where they are in the overall draw canvas. I have the scaling working, and am displaying the thumbnail in the correct location, but it appears that the thumbnail is not being updated on subsequent onDraws when new lines/shapes are added.

    So that I have access to the underlying bitmap for this view (to show the thumbnail, be able to easily save the bitmap to a file, etc) I do the following in onSizeChanged() for the View:

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    
        // set the canvas height, panning, etc, based on the now inflated View
        mWidth      = getWidth();
        mHeight     = getHeight();
        mAspectRatio    = mWidth / mHeight;
        mPanX       = 0;
        mPanY       = 0;
    
        // create the Bitmap, set it to the canvas
    mBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
    mCanvas.setBitmap(mBitmap);
    draw(mCanvas);
    }
    

    Then, when the user draws and invalidate() is called, I do the following in onDraw() to generate the thumbnail:

    @Override
    protected void onDraw(Canvas canvas) {
        <snipped code that draws paths, shapes to canvas>
    
        if (mScaled) {
            Bitmap out = Bitmap.createScaledBitmap(mBitmap, (int) thumbWidth, (int) thumbHeight, false);
            canvas.drawBitmap(out, null, thumbnailRectF, thumbCanvasPaint);
        }
    }
    

    The thumbnail gets displayed in the space defined by thumbnailRectF using the thumbCanvasPaint, but in subsequent onDraw() calls, the scaled bitmap has not changed from it's original state, even though the full-sized active canvas shows all of the drawings, etc. Based on some testing, it seems to me that while I am setting the Bitmap with the initial call to draw(mCanvas);, subsequent onDraws are writing to the underlying Bitmap rather than the one specified in onSizeChanged().

    So, I guess I am trying to figure out how I tie the onDraw canvas to a Bitmap that I can readliy access to perform re-sizes, save, etc. Looking at this question, I thought that the draw(mCanvas); call would tie the onDraw to the bitmap specified in the mCanvas (in my case, mBitmap), but in practice, it doesn't seem to be working, in so far as updats to the canvas are concerned.

    Thanks,

    Paul

  • Unpossible
    Unpossible about 13 years
    Wow, yes, that linked program is constructed almost 100% the same, except for my issue with tying the bitmap to the canvas, etc. Trying this solution now.