java - imageView 中图像的新尺寸

标签 java android android-imageview

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

我试过这个:

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>

最佳答案

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

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
      }
});
}

关于java - imageView 中图像的新尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13998671/

相关文章:

android - 列表和图片在同一个 ScrollView 中

android-fragments - android.widget.ImageView 无法转换为 android.view.ViewGroup

java - 自动从 png sprite 表中查找帧尺寸

java - 为什么Java有replaceAll(regex, replacement)而不是replaceAll(regex, regex)?

android - 如何更改对话框 fragment 中的 fragment

android - 如何使用mobileservice从sql azure获取时间戳(行版本)到android

android - onclicklistener 不在方法内部工作

java - ImageView 数组不保持纵横比

java - 以编程方式解决数独

java - Intellij 内存不足错误 : PermGenSpace with Scala