How can I extract the bounds of a bitmap in a canvas from the values in the transformation matrix?

84 views Asked by At

I have a transformation matrix that gets updated when the image inside gets dragged, every time in onDraw:

@Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (!isInitialized) {
            int w = getWidth();
            int h = getHeight();
            position.set(w / 2, h / 2);
            isInitialized = true;
        }

        track.reset();
        track.postScale(scale, scale);
        transform.reset();
        transform.postTranslate(-width / 2.0f, -height / 2.0f);
        transform.postRotate(getDegreesFromRadians(angle));
        transform.postScale(scale, scale);
        transform.postTranslate(position.getX(), position.getY());
        float[] values = new float[9];
        transform.getValues(values);
        Log.d("values","are"+values[0]+values[1]+values[2]);
        canvas.drawBitmap(bitmap, transform);

    }

When I log the values I can determine that values[0] is the scale factor for instance from the original bitmap. The other 8 values change when I drag, rotate, and scale the bitmap.

Now I want to find the bounds of the bitmap. This includes knowing the translation as well as the scale (to determine the size).

So where can I find the equations to calculate the and bottom of the bitmap as well as its position? Also, does this factor in any rotations that may have been applied?

0

There are 0 answers