Android:将ImageView放在屏幕上的确切位置 [英] Android: placing an ImageView on an exact location on the screen

查看:1658
本文介绍了Android:将ImageView放在屏幕上的确切位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 getLocationOnScreen 来获取ImageView的位置,然后我想将另一个ImageView准确地放在那个地方。

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.

假设两者的布局相同。当应用程序启动时,只有 imgv1 可见。用户可以移动和旋转该图像。然后用户可以按一个按钮和第二个图像, imgv2 应该完全放在 imgv1 之上,所以它涵盖了它。 imgv1 imgv2 的大小相同。

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.

例如,假设我有 imgv1 imgv2 as:

For example, assume I have imgv1 and imgv2 as:

ImageView imgv1, imgv2;

int[] img_coordinates = new int[2];
imgv1.getLocationOnScreen(img_coordinates);

我想使用类似的东西:

imgv2.setX(img_coordinates[0]);
imgv2.setY(img_coordinates[2]);

但这不能做我需要做的事情,那就是放置左上角 imgv2 位于 imgv1 的左上角。

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.

**更新**

这是我有的布局:

<?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:放 imgv1 in FrameLayout

步骤2:放 imgv2 在同一个 FrameLayout 中, android:visibility =gone

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

步骤3:当用户按下按钮时,调用 imgv2.setVisibility(View.VISIBLE)

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>

缺少 FrameLayout的大小/定位规则,这可能是你目前用于 imgv1 的起始条件的任何东西,大概是。

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

或者,有一个 ImageView ,而不是两个,并在按钮点击上更改图像。例如,您可以使用 LayerDrawable (或等效资源)将两个drawable层叠在一起,并显示出来。

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.

这篇关于Android:将ImageView放在屏幕上的确切位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆