gpt4 book ai didi

java - 无法使用 View 树观察器获取 View 大小

转载 作者:行者123 更新时间:2023-12-01 12:13:11 25 4
gpt4 key购买 nike

我试图使用 ViewTreeObserver 获取适配器中 View 的宽度和高度,而 View 尚未完全膨胀,但我总是得到零值。宽度和高度将在调整适配器 ListView 中 ImageView 位图大小的方法中使用。这是我的代码:

public View getView(int position, View convertView, ViewGroup parent) {

newView = convertView;
ViewHolder holder;

if (null == convertView) {
holder = new ViewHolder();
newView = inflater.inflate(R.layout.selfie_list_view, null);
holder.selfieView = (ImageView) newView.findViewById(R.id.selfie_bitmap);
holder.selfieName = (TextView) newView.findViewById(R.id.selfie_name);
newView.setTag(holder);

} else {
holder = (ViewHolder) newView.getTag();
}

SelfieRecord curr = list.get(position);
String selfieName = curr.getBitmapName();
String selfiePath = curr.getBitmapPath();
Bitmap selfie = setPic(newView, selfiePath);

holder.selfieView.setImageBitmap(selfie);
holder.selfieName.setText(selfieName);

return newView;
}


private Bitmap setPic(final View view, String path) {

ViewTreeObserver vto = view.getViewTreeObserver();
if (vto.isAlive()) {
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

@Override
public void onGlobalLayout() {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
targetW = view.getWidth();
targetH = view.getHeight();
Log.i(TAG, "view_width = " + targetW + " view_height =" + targetH);

}
});
}

// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;

// Determine how much to scale down the image
int scaleFactor = 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(path, bmOptions);

return bitmap;
}

任何想法将不胜感激。

最佳答案

好吧,我使用 onPreDrawListener 解决了这个问题,并将位图解码和 ImageView 放入其中。这是解决方案,以防有人需要。不管怎样还是谢谢你:

public View getView(int position, View convertView, ViewGroup parent) {

newView = convertView;
final ViewHolder holder;

if (null == convertView) {
holder = new ViewHolder();
newView = inflater.inflate(R.layout.selfie_list_view, null);
holder.selfieView = (ImageView) newView.findViewById(R.id.selfie_bitmap);
holder.selfieName = (TextView) newView.findViewById(R.id.selfie_name);
newView.setTag(holder);

} else {
holder = (ViewHolder) newView.getTag();
}

SelfieRecord curr = list.get(position);
String selfieName = curr.getBitmapName();
final String selfiePath = curr.getBitmapPath();
//Bitmap selfie = setPic(newView, selfiePath);

ViewTreeObserver vto = holder.selfieView.getViewTreeObserver();
vto.addOnPreDrawListener(new OnPreDrawListener() {

@Override
public boolean onPreDraw() {
holder.selfieView.getViewTreeObserver().removeOnPreDrawListener(this);
int width = holder.selfieView.getMeasuredWidth();
int height = holder.selfieView.getMeasuredHeight();
Log.i(TAG, "view_width = " + width + " view_height =" + height);

BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(selfiePath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
Log.i(TAG, "photo_width = " + photoW + " view_height =" + photoH);

// Determine how much to scale down the image
int scaleFactor = Math.min(photoW/width, photoH/height);

// 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(selfiePath, bmOptions);

holder.selfieView.setImageBitmap(bitmap);

return true;
}
});

holder.selfieName.setText(selfieName);

return newView;
}

关于java - 无法使用 View 树观察器获取 View 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27167997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com