【问题标题】:new size of an image in imageViewimageView 中图像的新尺寸
【发布时间】:2012-12-22 08:53:15
【问题描述】:

我想知道在图像视图中设置图像时的新尺寸。

我试过这个:

System.out.println("Imagesize 1 w:" + imageView.getDrawable().getIntrinsicWidth()+" h "+imageView.getDrawable().getIntrinsicHeight());
System.out.println("Imagesize 2  w:" +loadedImage.getWidth()+ " h " +loadedImage.getHeight());

imageView的xml1:

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

我有这个结果

Imagesize 1 w:400 h 577
Imagesize 2 w:400 h 577

imageView的xml2:

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

同样的结果:

Imagesize 1 w:400 h 577
Imagesize 2 w:400 h 577

显然,在 xml1 和 xml2 的情况下,我的图像不会以相同的大小显示在我的屏幕上。我猜当我 getWidth 或 getHeight 时,它是图像的实际大小,但我想要屏幕上显示的图像大小。

有什么想法吗?

更新 1:

我忘了说loadedImage是我图片的位图

更新 2:

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>

【问题讨论】:

    标签: java android android-imageview


    【解决方案1】:

    要在创建屏幕对象后获取它们的大小,您需要像这样使用视图观察器:

    ViewTreeObserver viewTreeObserver = YOURLAYOUT.getViewTreeObserver();
    if (viewTreeObserver.isAlive()) {
    viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
          public void onGlobalLayout() {
             YOURLAYOUT.getViewTreeObserver().removeGlobalOnLayoutListener(this);
             //do some things
             //YOURLAYOUT.getWidth() should give a correct answer
          }
    });
    }
    

    【讨论】:

    • 感谢您的回答,我尝试了您的解决方案,对于我所有不同的图像,我得到了相同的结果 w:536, h:846..
    • 图片的实际尺寸是多少?可能值得研究 ImageView 的 setAdjustViewBounds(boolean adjustViewBounds) 方法。
    • 我有几张不同的图片,大小不同,但我在这篇文章中的示例是:实际尺寸:400*577
    • 你的 imageView 的 xml1 是否有一个宽度和高度为 wrap_content 的父级?
    • 如果我添加这个 setAdjustViewBounds(true) 知道我的图像大小会有什么帮助?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多