gpt4 book ai didi

android - 使用包装内容可绘制图像获取 ImageView 位置

转载 作者:行者123 更新时间:2023-11-30 02:37:36 25 4
gpt4 key购买 nike

我在获取 ImageView 顶部和左侧位置时遇到问题。

我的 ImageView 设置在相对布局的中心。当我使用 getleft()getTop() 时,我的结果是 0 和 0。

我的布局文件是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rlMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="center"
android:src="@drawable/ic_launcher" />

</RelativeLayout>

这对我来说真的很紧急。

我的问题:

我写了一个移动和缩放 ImageView 的代码,它工作正常,但在第一次点击时, ImageView 位置更改为 0、0,我想获得默认位置以防止第一次点击发生变化。

最佳答案

不确定我是否明白你的意思,但你可以试试:

ImageView img = (ImageView) findViewById(R.id.image_view);
img.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener(){

@Override
public void onGlobalLayout() {

int left = img.getLeft();
int top = img.getTop();
//Toast the height and top...
}

});

由于在 onCreate 中图像还没有渲染到屏幕上,因此当您调用 getLeft()getTop()< 时,您将始终得到 0/

更多解释here

编辑

设置imageview的布局宽高为wrap_content

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rlMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="center"
android:src="@drawable/ic_launcher" />

</RelativeLayout>

关于android - 使用包装内容可绘制图像获取 ImageView 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26250569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com