Hello) I am struggling with the matrices in adroid. I used to work on opengl, everything is normal. What sequence did everything, and it will be ... And then ... some! post pre set ...

I need:

  1. First move the image to the center.
  2. Dump it to fit the screen
  3. Twirl

Code:

m.postTranslate(cxCompass - ((float)compass.getWidth()/2), cyCompass - ((float)compass.getHeight()/2)); if(getMeasuredWidth()<getMeasuredHeight()) m.preScale((float) getMeasuredWidth()/((float)compass.getWidth()),(float) getMeasuredWidth()/((float)compass.getWidth())); else m.preScale((float) getMeasuredHeight()/((float)compass.getHeight()),(float) getMeasuredHeight()/((float)compass.getHeight())); m.preRotate((float) ((-direction)), (float) compass.getWidth() / 2, (float) compass.getHeight() / 2); 

If I only do postTranslate everything is fine, but when I try to somehow scan, for example, what is now in the code. Everything is shifted slightly to the right and down. Why? How to properly and in what sequence to do to get what you want? And explain, please, prefixes to funk. matrices - post set pre ... Thank you!

    2 answers 2

    Everything is shifted slightly to the right and down. Why?

    And everything is very simple. When you do resizing, there must be a “fulcrum” - as to what everything changes. Similarly, when turning.

    It remains to find out where this point is. The answer is simple - at the origin (0,0). But here's the catch - in OpenGL this will be accurate in the center, and in 2D mode (where you actually “fight”), the point is at the top left. That is why, when you enlarge the picture, it will shift down-right. (and in openGL it will expand in different directions relative to the center. In a sense, it looks "more natural."). Therefore, when turning, consider this and everything will be ok.

      The docks say what is different

       pre: M' = M * S(sx, sy) post: M' = S(sx, sy) * M 

      Those. from which end a new transformation matrix will be added to the multiplicative sequence. I think you just need to come to uniformity and write post everywhere. (Well, or vice versa pre)

      • I thought so too, but when I do everything pre, for some reason not in the center but a little to the right and below the center - Justix
      • That's how he can shift when scaling in general, it's like this is possible. There it is not at all such a vector multiplied that at least some kind of displacement was - Justix
      • @JustixLoL can shift if the center of the axes is not in the center of the picture. And if you put all the post? - IronVbif
      • if all Post is not spinning around its point but around the point behind its image. already. "can shift if the center of the axes is not in the center of the picture" - Well, since the center may not be there, if, for example, without a scale, is the picture exactly in the center? - Justix
      • I supplemented preScale with two more arguments of the center of the picture (width / 2 height / 2) And I did everything pre. It seems to work.