我有ListView
,每行显示一张图片和一些文字。使用Universal Image Loader
在运行时动态加载图片。它的工作原理,但问题是图片没有正确缩放。我希望它们被缩放以保持其原始宽高比,但是它们被拉伸以填充其容器的方形尺寸,从而扭曲了许多图像。
listview适配器是:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent">
<ImageView
android:id="@+id/image"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="6dp"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:src="@drawable/my_img"/>
<TextView
android:layout_toRightOf="@+id/image"
android:textSize="16sp"
android:id="@+id/coach_name"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
图像在运行时从服务器加载。这是适配器类:
public class CoachAdapter extends ArrayAdapter<String> {
private static Context context;
private ArrayList<String> coach_list;
private DisplayImageOptions doption=null;
private ImageLoadingListener animateFirstListener =null;
public CoachAdapter(Context context, int textViewResourceId, ArrayList<String> coach_list) {
super(context, textViewResourceId, coach_list);
this.context = context;
this.coach_list = coach_list;
doption=new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.my_img)
.showImageOnFail(R.drawable.my_img)
.showImageOnLoading(R.drawable.my_img)
.cacheInMemory(true)
.cacheOnDisk(true)
.displayer(new RoundedBitmapDisplayer(8))
.imageScaleType(ImageScaleType.EXACTLY)
.postProcessor(new BitmapProcessor() {
@Override
public Bitmap process(Bitmap bmp) {
return Bitmap.createScaledBitmap(bmp, 32, 32, false);
}
})
.build();
animateFirstListener = new AnimateFirstDisplayListener();
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
View row = convertView;
final ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.row_coach, parent, false);
holder = new ViewHolder();
holder.text = (TextView) row.findViewById(R.id.coach_name);
holder.image = (ImageView) row.findViewById(R.id.image);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
String coach_name = coach_list.get(position);
// get the url string from the database
Coach this_coach = myDB.getCoachFromName(coach_name);
String coach_picture_url = this_coach.getCoachPictureURL();
holder.text.setText(coach_name);
holder.image.setScaleType(ImageView.ScaleType.FIT_CENTER);
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(context));
imageLoader.displayImage(url, holder.image, doption, animateFirstListener);
return row;
}
private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {
static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
if (loadedImage != null) {
ImageView imageView = (ImageView) view;
boolean firstDisplay = !displayedImages.contains(imageUri);
if (firstDisplay) {
FadeInBitmapDisplayer.animate(imageView, 500);
displayedImages.add(imageUri);
}
}
}
}
private class ViewHolder {
public TextView text;
public ImageView image;
}
}
当我阅读UniversalImageLoader文档时,应该根据XML文件中指定的内容对图像进行缩放。我已经尝试了fitCenter
和fitXY
。无论我指定什么,图像总是方形的。我还验证了许多原始图像的尺寸不是方形的,所以我知道缩放失真发生在这里。
任何人都能看到我做错了什么?谢谢!