Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scale & rotate Bitmap using Matrix in Android

I'm trying to scale and rotate in single operation before creting the final bitmap but the preRotate, postConcat doesn't seem to work.

Bitmap bmp = ... original image ...  Matrix m = new Matrix() m.setScale(x, y); m.preRotate(degrees, (float) width / 2, (float) height / 2);  Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), m, true); 

It only applies the scale and not rotation.

like image 461
Taranfx Avatar asked Jan 04 '12 05:01

Taranfx


People also ask

What do you means by scale?

A scale is a set of levels or numbers which are used in a particular system of measuring things or are used when comparing things. ... an earthquake measuring five-point-five on the Richter scale. The patient rates the therapies on a scale of zero to ten.

What is the meaning of scale in Oxford dictionary?

[singular, uncountable] the size or extent of something, especially when compared with something else.

What is called scale in psychology?

a system for ordering test responses in a progressive series, so as to measure a trait, ability, attitude, or the like.

What do you mean by scale of an instrument?

Weighing scale, an instrument used to measure mass. Scale (ratio), the ratio of the linear dimension of the model to the same dimension of the original. Spatial scale, a classification of sizes. Scale ruler, a tool for measuring lengths and transferring measurements at a fixed ratio of length.


1 Answers

The answer was given, but to make things more clear to anyone reading this:

1) if you wish to perform ONE transformation in your bitmap, you CAN use SET (setRotate, setScale etc).

But note that any call to a "set" method OVERRIDES other transformations. It's like a new matrix. That's why OP's rotation was not working. These calls are not performed line by line. It's like they are scheduled to be done at runtime by the GPU when the new bitmap is being drawn. It's like when resolving your matrix, GPU rotated it, but then, created a scaled new one, ignoring previous matrix.

2) if you wish to perform more then one transformation, then you MUST use "pre" or "post" methods.

And what is the difference between a postRotate and a preRotate, for example? Well, this matrix math stuff is not my strength, but what I know is that the graphic cards make these transformations using matrix multiplication. It seems to be way more efficient. And as far as I remember from school, when multiplicating matrices the order IS important. A X B != B X A. So, scale a matrix and then rotate it is different from rotate and then scale it.

BUUUUT, as far as the final result in the screen is the same, we high level programmers usually do not need to know these differences. The GPU does.

Well, in that rare cases when you are performing really complicated matrix operations, and results are not what you expected or the performance is terrible and you need to deeply understand these methods to fix your code, well, then android documentation can not be of much help anyway. Instead, a good Linear Algebra book would be your best friend. ;)

like image 153
PFROLIM Avatar answered Sep 30 '22 05:09

PFROLIM