Android: placing an ImageView on an exact location on the screen

我们两清 提交于 2019-12-04 06:52:06

问题


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>

回答1:


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




回答2:


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.




回答3:


Step #1: Put imgv1 in a FrameLayout

Step #2: Put imgv2 in that same FrameLayout, with android:visibility="gone"

Step #3: When the user presses the button, call imgv2.setVisibility(View.VISIBLE)

<FrameLayout android:id="combined">
  <ImageView android:id="imgv1" android:layout_width="match_parent" android:layout_height="match_parent" />
  <ImageView android:id="imgv2" android:visibility="gone" android:layout_width="match_parent" android:layout_height="match_parent"  />
</FrameLayout>

Missing are sizing/positioning rules for the FrameLayout, which would be whatever you are presently using for your starting conditions for imgv1, presumably.


Alternatively, have one ImageView, rather than two, and change the image on the button click. For example, you could use a LayerDrawable (or the equivalent resource) to layer two drawables on top of each other, and show that.



来源:https://stackoverflow.com/questions/50263528/android-placing-an-imageview-on-an-exact-location-on-the-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!