1

I am trying to get the local coordinates of the view and then later I want to perform Matrix PostTranslate to an image view. I have already seen multiple posts for example How to get the absolute coordinates of a view I am unable to get the correct coordinates.

I have set the gravity of layout to Bottom in to which I am adding elements.

 <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".4">
        <ImageView
            android:src="@drawable/Pointer"
            android:visibility="visible"
            android:scaleType="matrix"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/PostTranslateImage"/>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:id="@+id/OverviewMarkerContainer"/>

    </FrameLayout>

I am adding multiple entries into the linear layout and then I want to set the pointer image view top to the top of the top most element in the linear layout.

Layout

Problem: I want the Y value of the view relative to 40% size linear layout. Get Y does not return correct value because gravity has been applied. Get Y will return 0 but instead because of Gravity the position has been changed. I want to get the Y where the view is shown, so I can apply matrix to an image and move the image to that y.

If I get GetLocalVisibleRect or GetGlobalVisibleRect they provide value which is not local to this view. for example GetGlobalVisibleRect provides value 2004 on Samsung Galaxy S10, but if I manually set the matrix Y to 700, I can barely see the image, so setting this value does not show the image.

layout.ViewTreeObserver.GlobalLayout += (sender, args) =>
            {
                var matrix = waves.ImageMatrix;
                var scaleRatio = Resources.DisplayMetrics.WidthPixels / (float)waves.Drawable.IntrinsicWidth;
                matrix.SetScale(scaleRatio, scaleRatio);                

                Rect rectf = new Rect();
                var point = view3.GetGlobalVisibleRect(rectf);
                matrix.PostTranslate(0, point.TOP); //returns 0

                waves.ImageMatrix = matrix;
            };
AZ_
  • 21,688
  • 25
  • 143
  • 191
  • I am not sure what exactly do you want from us! – FreakyAli Mar 12 '20 at 09:27
  • `view3.GetY()`? It's not clear what the issue is, or how the matrix translation and those percentages relate. – Mike M. Mar 12 '20 at 09:29
  • I want the Y value of the view relative to 40% size linear layout. Get Y does not return correct value because gravity has been applied. Get Y will return 0 but instead because of Gravity the position has been changed. I want to get the Y where the view is shown, so I can apply matrix to an image and move the image to that y. – AZ_ Mar 12 '20 at 09:36
  • No, gravity does not "zero out" the y-coordinate. I think you might want to focus on why you are getting zero for that. – Mike M. Mar 12 '20 at 09:51
  • 1
    Oh, I just noticed. I'm still trying to wrap my head around what you're trying to do, exactly, but your `LinearLayout` has `wrap_content` height, so the topmost `View` will always have a y-coordinate of zero. Did you mean for that to be `match_parent` instead, to fill the `FrameLayout`? – Mike M. Mar 12 '20 at 10:24
  • @MikeM.Thanks for your time. I am adding some views in the OverviewMarkerContainer and I want to set the top of the PostTranslateImage to the top of the top most view in OverviewMarkerContainer. I don't want to do it Match_Parent because then Gravity have no effect. so it should take 40% of the screen height but it should add the very first element at the bottom and then the other one at the top. – AZ_ Mar 12 '20 at 10:43
  • Now GetY() returns Zero is kind of correct because it is relative to this OverviewMarkerContainer but because of the gravity the view is not shown at the top of the layout but at the bottom of the layout. If I get global coordinate then it gives a very high value, which is invalid for setting the matrix because it needs local value within the layout. I can manually add 500 but then it is different on each mobile with different density. Problem: Get Y should not return Zero but should return value according to the gravity set. – AZ_ Mar 12 '20 at 10:47
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/209514/discussion-between-az-and-mike-m). – AZ_ Mar 12 '20 at 10:53
  • @MikeM.I have solved the problem by getting the Y coordinate of the OverviewMarkerContainer layout. because inside views x, y will start from 0,0 – AZ_ Mar 12 '20 at 10:55
  • 1
    Yeah, I _think_ I might see what you want, finally. If so, you want the `LinearLayout` to have `match_parent` height, and `gravity` bottom, not `layout_gravity`. Then `GetY()` will be relative to the top of the `FrameLayout`'s and `LinearLayout`'s top, and the translation will move the image to the top of `view3`. Might need to adjust for the image's own height, depending on what you're going for. I just threw together a quick, ugly test: https://i.stack.imgur.com/CVcoa.jpg. – Mike M. Mar 12 '20 at 10:57
  • 1
    Oh, I missed your previous comments; my notifications didn't fire. Anyhoo, if you got something working, glad to hear it. Cheers! – Mike M. Mar 12 '20 at 10:59
  • @MikeM.Thanks a lot for your time, please add your solution to the answer so that I can make it accepted answer it might help other people as well. please consider adding an answer (Y) thanks. – AZ_ Mar 12 '20 at 11:38
  • 1
    If you have solved this issue, please post your answer, it will help others who have similar issue. – Leon Mar 24 '20 at 08:25
  • @LeonLu-MSFT I have solved the issue by taking the top 60% layout bottom. The only problem with the given code point.TOP gives Zero. This was my solution. – AZ_ Mar 30 '20 at 11:07
  • Thanks for your sharing. – Leon Mar 31 '20 at 06:29

0 Answers0