【问题标题】:Original image size VS displayed image size原始图像尺寸 VS 显示图像尺寸
【发布时间】:2012-12-26 18:50:20
【问题描述】:

我正在尝试获取屏幕上显示的图像大小,而不是图像的原始大小。

我已经对此进行了一些研究,并且在未解决的 post 两岁时发现了相同的问题,所以我想也许现在有一种新的方法可以做到这一点。

我尝试了这 3 个解决方案:

//Get the same result for all my images (whereas images have difference sizes)
imageView.getWidth() + imageView.getHeight()

//Get the original size of the images  
imageView.getDrawable().getIntrinsicWidth() + imageView.getDrawable().getIntrinsicHeight()
bitMap.getWidth() + bitMap.getHeight()

我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="1dip" >

<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="@string/descr_image"
android:layout_alignParentBottom="true" />

<ProgressBar
android:id="@+id/loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone" />

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>

更新 1

ViewTreeObserver viewTreeObserver = imageView.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
     viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
       @SuppressWarnings("deprecation")
       public void onGlobalLayout() {      
           imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this)               
           System.out.println("TEST :" + imageView.getWidth() + " " + imageView.getHeight());
        }
    });
}

【问题讨论】:

    标签: java android


    【解决方案1】:

    在您的ImageView 上,您将宽度和高度设置为

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    

    图像视图将填充父视图,getWidth()getHeight() 将为您提供这些值。

    您需要以某种方式包装您的 ImageView 以使其正确显示,但请提供 ImageView 宽度/高度值,例如

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    

    ImageView bitmap scale dimensions 上也有一个答案似乎也解决了这个问题。

    来自https://stackoverflow.com/users/321697/kcoppock的回答:

    ImageView iv = (ImageView)findViewById(R.id.imageview);
    int scaledHeight, scaledWidth;
    iv.getViewTreeObserver().addOnPreDrawListener(
        new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            Rect rect = iv.getDrawable().getBounds();
            scaledHeight = rect.height();
            scaledWidth = rect.width();
            iv.getViewTreeObserver().removeOnPreDrawListener(this);
            return true;
        }
    });
    

    【讨论】:

    • 谢谢它有效,我不知道为什么,但我必须使用 ViewTreeObserver 否则大小为 w:1 h:1。但是使用“wrap_content”我的图像以“丑陋的方式”显示,是否有可能使用 fill_parent 参数获取大小?
    • 我添加了一些可能对您有所帮助的代码。查看修改后的答案。
    猜你喜欢
    • 2018-08-03
    • 1970-01-01
    • 2023-02-04
    • 2013-04-17
    • 2019-02-02
    • 1970-01-01
    • 2020-04-19
    • 1970-01-01
    • 2014-11-19
    相关资源
    最近更新 更多