Android测量图像中物体大小,android – 加载图像后测量ImageView
我試圖找到一種方法來測量ImageView后使用Glide或Picasso(或其他任何東西)加載圖像.基本上,我試圖在某些位置在圖像頂部布局其他視圖,但需要最終的ImageViews尺寸才能準確地完成.
我不知道用于嘗試這樣做的最佳布局是什么,但我目前正在使用這個:
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:id="@+id/viewingImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
并將圖像加載到viewingImageView中.這些都在根FrameLayout內部,但我不認為這很重要.
這是我最近的嘗試,但是作為評論,在ImageView上使用.getWidth()和.getHeight()返回0.資源的寬度/高度返回原始圖像的大小.
滑行
.with(this)
.load(entry.getImageUrl())
.asBitmap()
.into(new SimpleTarget() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation super Bitmap> glideAnimation) {
mImageView.setImageBitmap(resource);
int width = mImageView.getMaxWidth(); //prints 0
int height = mImageView.getMaxHeight(); //prints 0
int resw = resource.getWidth(); //returns original image width
}
});
那么如何加載圖像然后在加載圖像后測量ImageView(或其包裝FrameLayout)?或者如果可能的話,測量最終布局圖像的尺寸會更好,因為我知道圖像并不總是根據比例類型填充整個ImageView.我對任何解決方案持開放態度,以上只是我迄今為止所嘗試的內容.
解決方法:
您應該等待繪制視圖,您可以像這樣使用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();
}
});
標簽:android-glide,android,android-layout,picasso,imageview
來源: https://codeday.me/bug/20190824/1706670.html
總結
以上是生活随笔為你收集整理的Android测量图像中物体大小,android – 加载图像后测量ImageView的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用友t3显示无法解析服务器名称,用友T3
- 下一篇: android动画超出圆角,Androi