[英]how to get exect height and width of any images to used in imageView in android
[英]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.