Centering a scaled bitmap in a canvas
<p>I'm trying to scale a large image into a smaller canvas. I would like to keep the center point the same as I pinch to zoom (mScaleFactor). </p>

<p>When I initially start off, I have a low mScaleFactor so I guarantee that is is taking up the entire area. </p>

<p>For some reason when I zoom in a lot, the bitmap goes offscreen. This is also why I don't want to use canvas.scale(scaleX, scaleY, translateX, translateY) since I want to make sure that the bitmap full renders the canvas bounds in the future. </p>

<pre><code>@Override
protected void onDraw(Canvas canvas) {

    canvas.save();

    canvas.scale(mScaleFactor, mScaleFactor);

        float deltaX = 0, deltaY = 0;

        float currentWidth = ORIGINAL_WIDTH * mScaleFactor;
        float currentHeight = ORIGINAL_HEIGHT * mScaleFactor;

        if (currentWidth &gt; mCanvasWidth)
            deltaX = mCanvasWidth - currentWidth;
        if (currentHeight &gt; mCanvasHeight)
            deltaY = mCanvasHeight - currentHeight;

        canvas.translate(deltaX / 2, deltaY / 2);


    if (mBitmap != null)
        canvas.drawBitmap(mBitmap, 0, 0, null);

    canvas.restore();
}
</code></pre>
