I would like to know about the function of mapRect api available under Matrix class in Android. If I have a sample Matrix A and Rectangle R, then for
RectF R = new RectF(t1,t2,t3,t4);
A.mapRect(R);
what kind of transformation is likely to happen to R. It would be more helpful if someone can illustrate the mapRect() api with some suitable examples.
Here's a very simple example:
Let's take a matrix:
Matrix matrix = new Matrix();
Set that matrix to scale everything twice as large:
matrix.setScale(2.0F, 2.0F);
Create a rectangle that is 10x10 with origin in upper left corner:
RectF rect = new RectF(0F, 0F, 10F, 10F);
So when we call
matrix.mapRect(rect);
the input rectangle we created is replaced with the output rectangle, which is the result of transforming the input:
rect.left = 0F;
rect.top = 0F;
rect.right = 20F;
rect.bottom = 20F;
There is another version of the method
matrix.mapRect(RectF dst, RectF src);
that does the same transform without affecting the input rectangle.
What is a matrix?
Consider a mirror. The mirror takes your image and creates a horizontally flipped version of your image.
Consider a microphone and an amplifier. They take your voice and create a louder version of your voice.
That's what a matrix is. It's a transformer. It takes an input and creates an output that is based on the input. So a matrix can transform a point, a rectangle, a circle, a polygon...
For more info, see my answer How does Matrix.postScale( sx, sy, px, py) work?
Also check out Affine transformations | Wikipedia. There is an awesome graphic that shows the different affine transforms and their effects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With