<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/myImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:background="@android:color/black"
android:src="@drawable/image2" />
<View
android:id="@+id/view_clickable_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#aa000000" />
</RelativeLayout>我想使图像视图(MyImage)中的一小部分图像在获得坐标后可单击。它对我来说工作得很好。现在我想在这张图片上实现缩放功能。当我放大时,图像的可点击区域也会相应地缩放。
我的问题是,我想在ImageView中获得图像的高度(不包括顶部和底部的黑色空间)。由于ImageView设置为与父级匹配,因此图像将以横向模式显示在屏幕中央)。EveryTime我得到了图像视图的高度。
我的获取图像和图像视图高度的代码:
int imagWidth = imageView.getWidth();
int h = imageView.getHeight();
int w = imageView.getDrawable().getIntrinsicWidth();
int imagHeight = imageView.getDrawable().getIntrinsicHeight();
Log.e("height", "Image height" + imagHeight + "\n" + "Imageview height" + h);因此根据计算: Image height= 882 Imageview height = 672
发布于 2018-03-13 18:06:20
为我工作
imgView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// Ensure you call it only once :
imgView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
// Here you can get the size :)
width = imgView.getWidth();
height = imgView.getHeight();
}
});发布于 2018-03-13 18:06:16
要获取图像视图中图像的高度,您可以简单地执行以下操作:
int width = imgView.getDrawable().getIntrinsicWidth();
int height = imgView.getDrawable().getIntrinsicHeight();这将返回高度和宽度的“内在”值。(这些值是在没有任何缩放的情况下加载到图像视图中的图像的值。您可以调整这些值以匹配您可能已应用的任何缩放)要调整缩放的值(假设您知道其处于横向模式),应执行以下操作:
int width = imgView.getDrawable().getIntrinsicWidth();
int height = imgView.getDrawable().getIntrinsicHeight();
double scale = ((double)imgeView.getWidth())/width
int final_height = Math.toIntExact(Math.round(scale * height));现在final_height是显示图像的高度。
发布于 2018-03-13 18:11:47
在ViewTreeObserver中使用下面的代码
int yourImageViewWidth , yourImageViewHeight;
ImageView y = (ImageView)findViewById(R.id.scaled_image);
ViewTreeObserver vto = yourImageView.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
yourImageView.getViewTreeObserver().removeOnPreDrawListener(this);
yourImageViewHeight = yourImageView.getMeasuredHeight();
yourImageViewWidth = yourImageView.getMeasuredWidth();
Log.d("Height: " + yourImageViewHeight + " Width: " + yourImageViewWidth);
return true;
}
});https://stackoverflow.com/questions/49253225
复制相似问题