Android:图库干扰 ImageView 的比例设置
我有一些高度为 48 像素(或更大)、宽度为 48 像素(或更大)的图像。所以基本上我的图像要么是 48 X 大于 48,要么大于 48 x 48。我想从这些图像中制作一些缩略图,这些缩略图是中心裁剪的,而不是缩放的,以便它们保持图像质量。我不能使用:
setScaleType(ScaleType.CENTER_CROP)
因为虽然它实现了我正在寻找的裁剪,但不幸的是它缩放了图像。我试图通过裁剪实现的是,它要么切断左侧,要么切断左侧。宽度大于 48 像素的图像的右侧,或顶部和右侧高度大于 48 像素的图像的底部。
我在 XML 布局中尝试了 ScaleType.CENTER ,它似乎正是我想要的。我在图库中使用这些图像,因此在我的图库适配器中我这样做:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView thumb = new ImageView(mContext);
thumb.setScaleType(ScaleType.CENTER);
thumb.setLayoutParams(new LayoutParams(THUMB_SIZE, THUMB_SIZE));
if (mPhotos != null) {
String imageUrl = mPhotos.get(position).getPhotoHref();
imageUrl = imageUrl.replaceAll(mPattern, mFormat);
ImageManager.instance().displayImage(imageUrl, mContext, thumb, DefaultThumbId);
} else {
thumb.setImageResource(DefaultThumbId);
}
return thumb;
}
但由于某种原因,显示了整个图像,但按比例缩小了。它在 XML 布局中工作得很好,但当我在代码中执行此操作并将其提供给 Gallery
时,效果就不那么好了。什么给?
I have some images that are 48 pixels (or greater) in height, and 48 pixels (or greater) in width. So basically I have images that are either 48 X greater than 48, or greater than 48 x 48. I want to make some thumbnails from these images that are center cropped and NOT scaled so that they maintain image quality. I can't use:
setScaleType(ScaleType.CENTER_CROP)
because although it achieves the cropping I am looking for, it unfortunately scales the image. What I am trying to achieve with the crop is that it either cuts off the left & right of an image that is greater than 48 pixels in width, or the top & bottom of an image that is greater than 48 pixels in height.
I tried ScaleType.CENTER
in an XML layout, and it seems to be exactly what I want. I'm using these images in a Gallery, so in my gallery Adapter I do:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView thumb = new ImageView(mContext);
thumb.setScaleType(ScaleType.CENTER);
thumb.setLayoutParams(new LayoutParams(THUMB_SIZE, THUMB_SIZE));
if (mPhotos != null) {
String imageUrl = mPhotos.get(position).getPhotoHref();
imageUrl = imageUrl.replaceAll(mPattern, mFormat);
ImageManager.instance().displayImage(imageUrl, mContext, thumb, DefaultThumbId);
} else {
thumb.setImageResource(DefaultThumbId);
}
return thumb;
}
but for some reason, the entire image is shown, but scaled way down. It works perfect in an XML layout, but not when I do this in code and feed it to a Gallery
. What gives?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这实际上不是画廊问题,我下载的图像没有正确缩放,因此它们的最大宽度/高度与视图大小相同,而不是最小宽度/高度来实现我想要这里。我现在以正确的尺寸下载,并且 setScaleType(ScaleType.CENTER_CROP) 工作正常。
This was actually not a Gallery problem, the images I was downloading weren't being scaled properly, so they were coming in with a max width/height that was the same size as the view, instead of a min width/height to achieve what I wanted here. I download at the proper size now and setScaleType(ScaleType.CENTER_CROP) works properly.