java - 如何在每次绘制到 UI 之前获取 ImageView 的高度和宽度(在片段中)
问题描述
我想将外部存储中的照片设置为(圆形)ImageView。因此我有一个函数,它要求 ImageView 的高度和宽度,以裁剪图片,然后将其设置为位图。
我的问题是,在加载 UI 之前调用了这个函数。我遇到了解决方案,我第一次在 onViewCreated 中调用了这个函数,我在其中添加了一个 Listener 到 addOnWindowFocusChangeListener。这是第一次。
该片段位于 ViewPager 内,如果我转到左侧的片段,然后从中间到右侧,它又消失了。
我对 StackOverflow 完全陌生。我感谢每一个提示,我可以做得更好。
//This works for the First Time
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
View m = getView();
ViewTreeObserver n = m.getViewTreeObserver();
n.addOnWindowFocusChangeListener(new ViewTreeObserver.OnWindowFocusChangeListener() {
@Override
public void onWindowFocusChanged(final boolean hasFocus) {
// do your stuff here
ImageUtil.setPic(binding.photoRoundProfile, mCurrentPhotoPath);
}
});
}
//The function where I set the Picture into the ImageView
static public void setPic(ImageView mImageView, String pPath) {
try {
if (pPath == null) return;
File f = new File(pPath);
if (!f.exists() || f.isDirectory()) return;
// Get the dimensions of the View
int targetW = mImageView.getWidth();
if (targetW == 0) targetW = mImageView.getMeasuredWidth();
int targetH = mImageView.getHeight();
if (targetH == 0) targetH = mImageView.getMeasuredHeight();
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(pPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = photoW / targetW; //Math.min(photoW/targetW, photoH/targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(pPath, bmOptions);
Bitmap orientedBitmap = ExifUtil.rotateBitmap(pPath, bitmap);
mImageView.setImageBitmap(orientedBitmap);
//mImageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
mImageView.setAdjustViewBounds(true);
mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} catch (Exception e) {
e.printStackTrace();
}
}
有一个视频,为了更好的理解:https ://www.loom.com/share/05c4174562d74c9bba8ff02c9072c866
解决方案
我找到了解决我的问题的方法。我onPreDraw
在OnPreDrawListener
. 每次ImageView
更新时都会运行此侦听器。因此我添加了一个 if 语句来节省性能而不是每次都重绘。
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ImageView iv= binding.photoRoundProfile;
ViewTreeObserver vto = iv.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
private int mFinalWidth = 0;
private int mFinalHeight = 0;
public boolean onPreDraw() {
if(mFinalHeight != 0 || mFinalWidth != 0)
return true;
mFinalHeight = iv.getHeight();
mFinalWidth = iv.getWidth();
Log.d("hilength","Height: " + mFinalHeight + " Width: " + mFinalWidth);
ImageUtil.setPic(binding.photoRoundProfile, mCurrentPhotoPath);
return true;
}
});
}
推荐阅读
- c# - 在其他机器上得到 404,为什么?
- python-3.x - Python 3.x 如何从字符串中提取数字的字母表示
- api - 如何在没有 /products.json 的情况下从 Shopify api 获取产品
- amazon-web-services - Lambda 使用 JavaScript 中的 AWS 开发工具包 copyobject 方法将对象从一个存储桶复制到不同账户中的另一个存储桶
- javascript - 如何从 Django 后端添加自定义(或动态)Javascript?
- excel - Excel AND 和 NOT
- django - 日期不会显示在 django 管理面板中
- python - 如何根据 pandas Dataframe 上的条件打印值?
- sql - 通过 3 个表格进行计算、求和和分组
- bash - 仅打印具有值的变量