“比例类型:FIT_XY”不会拉伸图库中的图像
我有一个横向模式的画廊,其中有很多带有图像适配器的图像。我希望图像水平拉伸到整个屏幕,并保持纵横比并根据需要垂直拉伸..
我尝试使用 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;
}
}
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
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当我在 android 中执行此操作时,我使用适合缩放(fitCenter、fitStart、fitEnd)以及调整视图边界,xml 属性将是例如
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.
您可以计算宽度和高度的像素并将它们发送到
setLayoutParams
:设置缩放类型不会执行任何操作(除了 FitXY 会水平拉伸到边界,但将高度保留为其他值,如下所示你提到过)。另外 FILL_PARENT 似乎没有按预期工作。其他布局似乎会影响位图大小(或者在我的情况下,可绘制)。
在布局 xml 文件中,您可能必须设置 fill_parent:
这对我下载并流式传输到 Drawable 有用,所以我认为它适用于您的位图。
You can compute the pixels for width and height and send them to
setLayoutParams
: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:
This worked for me downloading and streaming into a Drawable, so I think it will work for your bitmap.