Android Get Local X, Y Coordinates of a view

443 views Asked by At

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;
            };
0

There are 0 answers