![](/img/trans.png)
[英]How to get width and height of resized custom view , before it is drawn
[英]How to get ImageView Height and Width every time, before it is drawn to the UI (in a Fragment)
我想將外部存儲中的照片設置為(圓形)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
我找到了解決我的問題的方法。 我在OnPreDrawListener
找到了onPreDraw
方法。 每次更新ImageView
都會運行此偵聽器。 因此我添加了一個 if-Statement 來節省性能而不是每次都重繪。
@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;
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.