如何解决如何在绘制到UI之前在片段中每次获取ImageView的高度和宽度
我想将外部存储中的照片设置为(Circular)ImageView。因此,我有一个函数,要求ImageView的“高”和“宽”来裁剪图片,然后将其设置为“位图”。
我的问题是,在加载UI之前会调用此函数。我遇到的解决方案是,我第一次在onViewCreated中调用此函数,在其中我向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;
}
});
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。