我有一个横向展示的画廊,其中包含很多张图片,使用了一个图像适配器。我希望这些图片能够在水平方向上拉伸到整个屏幕,并保持纵向的宽高比例不变,直至拉伸完成。
我尝试使用FIT_XY属性,但是它并不能完美地适应水平方向,而且也无法在垂直方向上保持宽高比例(图片会被压缩)
请问该如何实现呢?以下是自定义图像适配器:
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
int counter = 0;
private final Context mContext;
public String[] mImageIds;
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
public void insert(String string) {
mImageIds[counter] = string;
counter++;
}
@Override
public int getCount() {
return mImageIds.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageBitmap(BitmapFactory.decodeFile(mImageIds[position]));
i.setLayoutParams(new Gallery.LayoutParams(WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.FILL_PARENT));
i.setScaleType(ImageView.ScaleType.MATRIX);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
}