简体   繁体   English

如何获得图像视图的宽度和高度

[英]How to get the width and heigh of an imageview

I have this XML code: 我有以下XML代码:

<LinearLayout
 android:id="@+id/linearLayoutInner"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@layout/gallery_image_background"
/>

Then this code: 然后这段代码:

LinearLayout linearLayoutInner = (LinearLayout) findViewById(R.id.linearLayoutInner);
ImageView imageView = new ImageView(thisActivityContext);
imageView.setImageResource(R.drawable.example);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(lp);
linearLayoutInner.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
linearLayoutInner.addView(imageView);

I then call an own function that is meant to scale the bitmap image until one of the sides reach the edge (ie so if the original bitmat is twice as wide as tall, it will keep the proportion inside the imageview, something that is apparently not supported by any scaletype setting): 然后,我调用一个自己的函数,该函数用于缩放位图图像,直到侧面之一达到边缘为止(即,因此,如果原始位块的宽度是其两倍高,它将保持比例在imageview内,这显然不是任何scaletype设置支持):

SharedCode.sharedUtilScaleImage(imageView);

And here comes the problem. 问题来了。 That function needs to know the size of the view containing the bitmap drawable. 该函数需要知道包含可绘制位图的视图的大小。 if imageView behaves correctly, it should use MATCH_PARENT and hence give the width/height of the linearLayoutInner . 如果ImageView的行为正确,它应该使用MATCH_PARENT并因此得到linearLayoutInner的宽度/高度。 However, the following code returns zero: 但是,以下代码返回零:

int heightParent = max(imageView.getLayoutParams().height, imageView.getHeight());      
int widthParent = max(imageView.getLayoutParams().width, imageView.getWidth());

How do I solve this? 我该如何解决? And why am I returned 0 instead of the correct height/width? 为什么我返回0而不是正确的高度/宽度?

You're probably calling the code too early, before the View's onMeasure() has been called. 您可能在调用View的onMeasure()之前就太早调用了代码。 Until that happens, its size is unknown. 在此之前,它的大小是未知的。

final ImageView iv....
ViewTreeObserver vto = iv.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        //Measure
        iv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
    }
});
final ImageView imageView = (ImageView )findViewById(R.id.image_test);
    ViewTreeObserver vto = imageView.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            imageView .getViewTreeObserver().removeGlobalOnLayoutListener(this);
            imageView.getHeight(); // This will return actual height.
            imageView.getWidth(); // This will return actual width.
        }
    });    

Seem like you might be calling from onCreate() . 似乎您可能正在从onCreate()调用。 u need to wait for activity window to attached and then call getWidth() and getHeight() on imageView .You can try calling getWidth() and getHeight() from onWindowFocusChanged() method of your activity. ü需要等待活动的窗口,连接,然后调用getWidth()getHeight()imageView 。您可以尝试调用getWidth()getHeight()onWindowFocusChanged()的活动的方法。

Edit 编辑

@Override
public void onWindowFocusChanged(boolean hasFocus){
    int width=imageView.getWidth();
    int height=imageView.getHeight();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM