I have a gallery in landscape mode with a lot of images with an imageadapter. I want that the images stretch to the whole screen Horizontally, and keep the aspect ratio and stretch as much as needed vertically..
I tried to use FIT_XY, it doesnt fit Horizontally, but it doesnt keep the ratio vertically ( the images become crushed) How can I do this? Here is the custom image adapter: 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
pub开发者_运维技巧lic 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;
}
}
when I do this in android I'm using a fit scaling (fitCenter, fitStart, fitEnd) together with adjust view bounds the xml attributes would be e.g.
android:scaleType="fitCenter" android:adjustViewBounds="true"
You can compute the pixels for width and height and send them to setLayoutParams
:
int width = getWindowManager().getDefaultDisplay().getWidth();
int height = width * bitmapHeight / bitmapWidth;
imageView.setLayoutParams(new Gallery.LayoutParams(width, height));
imageView.setPadding(6, 0, 6, 0); // set this to what you like
// remove this line:
//imageView.setScaleType(ImageView.ScaleType.MATRIX);
Setting the scale type won’t do anything (except FitXY will stretch horizontally to the bounds, but leave the height as something else, as you mentioned). Also FILL_PARENT does not seem to work as expected. The other layouts seem to affect the bitmap size (or in my case, Drawable).
In your layout xml file, you might have to set fill_parent:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
This worked for me downloading and streaming into a Drawable, so I think it will work for your bitmap.
精彩评论