首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从ImageView获取实际的图片宽/高?

如何从ImageView获取实际的图片宽/高?
EN

Stack Overflow用户
提问于 2018-03-13 18:01:51
回答 3查看 3.6K关注 0票数 2
代码语言:javascript
复制
<?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我得到了图像视图的高度。

我的获取图像和图像视图高度的代码:

代码语言:javascript
复制
 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

EN

回答 3

Stack Overflow用户

发布于 2018-03-13 18:06:20

为我工作

代码语言:javascript
复制
    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();
            }
        });
票数 2
EN

Stack Overflow用户

发布于 2018-03-13 18:06:16

要获取图像视图中图像的高度,您可以简单地执行以下操作:

代码语言:javascript
复制
int width = imgView.getDrawable().getIntrinsicWidth();
int height = imgView.getDrawable().getIntrinsicHeight();

这将返回高度和宽度的“内在”值。(这些值是在没有任何缩放的情况下加载到图像视图中的图像的值。您可以调整这些值以匹配您可能已应用的任何缩放)要调整缩放的值(假设您知道其处于横向模式),应执行以下操作:

代码语言:javascript
复制
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是显示图像的高度。

票数 1
EN

Stack Overflow用户

发布于 2018-03-13 18:11:47

ViewTreeObserver中使用下面的代码

代码语言:javascript
复制
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;
    }
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49253225

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档