我想在适配器中获取视图的宽度和高度,而视图尚未完全膨胀,使用ViewTreeObserver,但我总是得到零值。宽度和高度将用于在适配器的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;
}
任何想法都会非常感激。
答案 0 :(得分:1)
我使用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;
}
答案 1 :(得分:0)
我是这样做的:
int height;
int width;
ViewTreeObserver vto = view.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
height = view.getLayoutParams().height;
width = view.getLayoutParams().width;
}
});