Android getBounds返回高度&;宽度0

Android getBounds返回高度&;宽度0,android,image,Android,Image,这里的scaledHeight和scaledWidth为零。但我得到了正确的图像大小 photo = (ImageView) findViewById(R.id.image1); photo.setImageBitmap(pic); Drawable drawable = photo.getDrawable(); imageBounds = drawable.getBounds(); int scaledHeight = imageBounds

这里的scaledHeight和scaledWidth为零。但我得到了正确的图像大小

    photo = (ImageView) findViewById(R.id.image1);
    photo.setImageBitmap(pic);      
    Drawable drawable = photo.getDrawable();
    imageBounds = drawable.getBounds();
    int scaledHeight = imageBounds.height();
    int scaledWidth = imageBounds.width();

imageBounds.height()有什么问题

我认为您在onCreate方法中使用了“photo”。在这里,ImageView维度还没有定义,我建议您将代码放在onWindowFocusChanged方法中,当“hasFocus”为true时。

我认为您在onCreate方法中使用了“photo”。在这里,ImageView维度尚未定义,我建议您在“hasFocus”为true时,将代码放入onWindowFocusChanged方法中。

谢谢@Zappescu。这很好用。但我希望scaledHeight是屏幕上显示的图像的实际高度,它总是小于屏幕高度(对于我来说是320px)。但它与图像的实际大小相同。我试过了,左边也是。但它也返回了图像的实际大小。谢谢@Zappescu。这很好用。但我希望scaledHeight是屏幕上显示的图像的实际高度,它总是小于屏幕高度(对于我来说是320px)。但它与图像的实际大小相同。我试过了,左边也是。但它也返回了图像的实际大小。
    int intrinsicHeight = drawable.getIntrinsicHeight();
    int intrinsicWidth = drawable.getIntrinsicWidth();