3

I am trying to find the center of a view. For non-rotated view the value is correct, but for rotated view it's not correct as the centre(0,0) is rotating. In the case of scaling the width and height is keep constant even after zooming

I am using the following formula to calculate the center.

 int[] location = new int[2];
 childView.getLocationOnScreen(location);
 int xLocation = location[0] + childView.getWidth() / 2;
 int yLocation = location[1] + childView.getHeight() / 2 

from getGlobalVisibleRect i get the rect of visible part only

DKV
  • 1,767
  • 3
  • 28
  • 49
  • 1
    what actually do you want to achieve? what zooming do you mean? – pskink Jan 02 '17 at 08:56
  • Possible duplicate of [How can i get center x,y of my view in android?](http://stackoverflow.com/questions/13758654/how-can-i-get-center-x-y-of-my-view-in-android) – Yurii Tsap Jan 02 '17 at 09:00
  • With out scaling or rotation the above is correct – DKV Jan 02 '17 at 09:01
  • so call `View#getMatrix()` and use that `Matrix` for mapping any points you want (not only center point) – pskink Jan 02 '17 at 09:09
  • How to get center from matrix – DKV Jan 02 '17 at 09:14
  • `Matrix` has many "mapping" methods, read the documentation – pskink Jan 02 '17 at 09:32
  • @pskink : i checked the mapping method , but none of them gave me the correct value. getGlobalVisibleRect gaves the coorect value but when some part of the view is not visibile is the value get changing – DKV Jan 02 '17 at 12:58
  • so what is your code? – pskink Jan 02 '17 at 12:59
  • @pskink i just need the center of a scaled rotated view. i tried so may ways – DKV Jan 02 '17 at 13:01
  • so what is your code? how did you use a `Matrix`? – pskink Jan 02 '17 at 13:01
  • @pskink : there is no way in Rect from Matrix. only to apply Rect using matrix – DKV Jan 02 '17 at 13:11
  • what have you tried with a `Matrix`? did you call `View#getMatrix()`? what have you done with that `Matrix` then? – pskink Jan 02 '17 at 13:13
  • @pskink : i didn't find anything after that just angle and scale factor. Translation based on orgin – DKV Jan 02 '17 at 13:16
  • so what `Matrix` "mapping" methods did you try? (`i checked the mapping method , but none of them gave me the correct value`) – pskink Jan 02 '17 at 13:19
  • mapRect(RectF dst, RectF src) , mapRadius(float radius), mapPoints(float[] dst, float[] src) – DKV Jan 02 '17 at 13:54
  • ok and `mapPoints` does not work? what was your code? what float array did you pass to that method? – pskink Jan 02 '17 at 13:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/132105/discussion-between-v-v-and-pskink). – DKV Jan 02 '17 at 14:01

1 Answers1

1

https://developer.android.com/reference/android/view/View#getHitRect(android.graphics.Rect)

This method will return you Rect(considering rotation and scale) within its parent view