簡體   English   中英

如何獲取任何給定的imageView的寬度和高度?

[英]how to get the width and height of any given imageView?

我正在嘗試縮放和旋轉圖像。 而作為法Bitmap.createScaledBitmap()要求指定所需的寬度和高度,我想設置所需的寬度和所需的高度是完全一樣的我imageView的寬度和高度。

現在,如何獲取給定imageView的寬度和高度? 假設imageView最初為空,但僅使用findViewById()定義

您必須等待,直到視圖布局具有尺寸。 您可以使用ViewTreeObserver實現此ViewTreeObserver

final ImageView view = ...;
view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
    @Override
    @SuppressLint("NewApi")
    @SuppressWarnings("deprecation")
    public void onGlobalLayout()
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
            getViewTreeObserver().removeOnGlobalLayoutListener(this);
        else
            getViewTreeObserver().removeGlobalOnLayoutListener(this);

        // width and height are available here.
    }
});

另一種選擇是讓自己的類從ImageView派生並重寫onLayout()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM