Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Android, What kind of transform does mapRect api of Matrix class performs?

Tags:

android

matrix

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.

like image 279
San Avatar asked Oct 09 '15 17:10

San


1 Answers

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.

like image 196
kris larson Avatar answered Oct 03 '22 00:10

kris larson