I would like to use getLocationOnScreen
to get the location of an ImageView, and then I would like to place another ImageView exactly at that place.
Assume they both are in the same layout. When the app starts only imgv1
is visible. The user can move and rotate that image. Then the user can press a button and second image, imgv2
should be placed exactly on top of imgv1
so it covers it. Both imgv1
and imgv2
have the same size.
For example, assume I have imgv1
and imgv2
as:
ImageView imgv1, imgv2;
int[] img_coordinates = new int[2];
imgv1.getLocationOnScreen(img_coordinates);
I wanted to use something like:
imgv2.setX(img_coordinates[0]);
imgv2.setY(img_coordinates[2]);
but this doesn't do what I need to do, which is to place the top left corner of imgv2
on the top left corner of imgv1
.
Any other method that helps me to do so is fine too.
** Update **
This is the layout I have:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/tools_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<ImageView
android:id="@+id/imgv1"
android:layout_width="300dp"
android:layout_height="200dp"
android:layout_gravity="center_vertical|center_horizontal"
android:visibility="gone"
app:cameraCropOutput="true"
app:cameraPlaySounds="false" />
<ImageView
android:id="@+id/imgv2"
android:layout_width="300dp"
android:layout_height="200dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:visibility="gone" />
</FrameLayout>
Step #1: Put
imgv1
in aFrameLayout
Step #2: Put
imgv2
in that sameFrameLayout
, withandroid:visibility="gone"
Step #3: When the user presses the button, call
imgv2.setVisibility(View.VISIBLE)
Missing are sizing/positioning rules for the
FrameLayout
, which would be whatever you are presently using for your starting conditions forimgv1
, presumably.Alternatively, have one
ImageView
, rather than two, and change the image on the button click. For example, you could use aLayerDrawable
(or the equivalent resource) to layer two drawables on top of each other, and show that.The javadoc for view says that setX and setY will offsett the image from it's original location. It looks like what you want to use is setLeft and setTop.
https://developer.android.com/reference/android/view/View#setleft
If i have overlapping views I generally put them in layout and show/hide them. However if you want to dit via code try setting layout params of second image like:
lp.addRule(RelativeLayout.ALIGN_LEFT, image1.getId());
lp.addRule(RelativeLayout.ALIGN_TOP, image1.getId());
...something like it. Positioning depends a lot on parent of Image views. Relative Layout would be correct choice.