Android 加载图像后测量ImageView
我正试图找到一种方法,在使用Glide或毕加索(或任何真正的工具)将图像加载到Android 加载图像后测量ImageView,android,android-layout,imageview,picasso,android-glide,Android,Android Layout,Imageview,Picasso,Android Glide,我正试图找到一种方法,在使用Glide或毕加索(或任何真正的工具)将图像加载到图像视图后,测量它。基本上,我试图在图像顶部的某些位置布局其他视图,但需要最终的ImageViews尺寸才能准确地进行布局 我不确定尝试这样做的最佳布局是什么,但我目前使用的是: <FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <
图像视图后,测量它。基本上,我试图在图像顶部的某些位置布局其他视图,但需要最终的ImageViews尺寸才能准确地进行布局
我不确定尝试这样做的最佳布局是什么,但我目前使用的是:
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/viewingImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
那么,如何加载图像,然后在加载图像后测量ImageView(或其包装框架布局)?或者,如果可能的话,测量最终布局图像的尺寸会更好,因为我知道图像并不总是填充整个图像视图,这取决于比例类型。我对任何解决方案都持开放态度,以上只是我迄今为止所尝试的。创建一个带有图像视图的自定义类。
在图像视图中使用此onMeasure,我们可以获得高度和宽度
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
}您应该等待绘制视图,您可以像这样使用OnGlobalLayoutListener
ViewTreeObserver vto = mImageView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
this.mImageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
// Get the width and height
int width = mImageView.getMeasuredWidth();
int height = mImageView.getMeasuredHeight();
}
});
int height=mImageView.getMeasuredHeight()代码>int width=mImageView.getMeasuredWidth()代码>@Hemanth,它也返回0。你能试试看这个功能吗<代码>mImageView.post(新Runnable(){@Override public void run(){int height=mImageView.getmeasuredhight();int width=mImageView.getMeasuredWidth();}})
See@Hemanth haha我确实试过了,但我用了一个postdayed
,然后等了一两秒钟。这确实得到了imageview的最终大小,但这是一个有点奇怪的解决方案,因为我需要尽快覆盖项目,而不是等待定义的时间段。使用post
,您不必等待获得大小post(Runnable)
导致将Runnable添加到消息队列中。runnable将在UI线程上运行。我自己刚刚遇到这个问题。这实际上对于获得imageviews的最终尺寸非常有效。你知道如何在实际图像绘制后获得其最终尺寸吗?你可能需要获得图像视图中位图的大小,请检查此处的链接。我不确定如何使用此链接。什么是mneedsinialscale
和setInitialScale
?
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (mNeedsInitialScale) { // Set by setImage when a new image is set
// The measured width and height were set by super.onMeasure above
setInitialScale(getMeasuredWidth(), getMeasuredHeight());
mNeedsInitialScale = false;
}
ViewTreeObserver vto = mImageView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
this.mImageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
// Get the width and height
int width = mImageView.getMeasuredWidth();
int height = mImageView.getMeasuredHeight();
}
});