具有任意长宽比的 android Gallery
我希望有一个 Android 图库来存放不同宽高比的图像。我想要的是画廊中的图像的 CENTER_CROP 之类的东西。但是,当我将图像比例类型设置为此时,图像超出了图库图像边界。
当然,FIT_XY 会导致图像被压扁/变平。 CENTER 会在图库图像边框内产生水平或垂直的黑色空间。
有什么想法如何实现这一点?我能找到的每个示例都使用 FIT_XY 和固定大小的图像。我想我可以自己裁剪图像,但我不愿意。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv = (ImageView) convertView;
if (iv == null) {
iv = new ImageView(context);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setBackgroundResource(galleryItemBackground);
iv.setLayoutParams(new Gallery.LayoutParams(200, 200));
}
InputStream is;
try {
is = getInputStream(position);
} catch (IOException ioe) {
// TODO?
throw new RuntimeException(ioe);
}
Bitmap bm = BitmapFactory.decodeStream(is);
iv.setImageBitmap(bm);
/*
* if (bitmaps[position] != null) { bitmaps[position].recycle();
* bitmaps[position] = null; } bitmaps[position] = bm;
*/
return iv;
}
i wish to have an android gallery that will host images of varying aspect ratios. what i'd like is something like CENTER_CROP for the images in the gallery. however, when i set the image scale type to this, the images overrun the gallery image border.
of course, FIT_XY results in squished / flattened images. CENTER results in horizontal or vertical black space inside the gallery image border.
any ideas how to accomplish this? every example i can find uses FIT_XY with fixed size images. i suppose i could crop the images myself but i'd rather not.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv = (ImageView) convertView;
if (iv == null) {
iv = new ImageView(context);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setBackgroundResource(galleryItemBackground);
iv.setLayoutParams(new Gallery.LayoutParams(200, 200));
}
InputStream is;
try {
is = getInputStream(position);
} catch (IOException ioe) {
// TODO?
throw new RuntimeException(ioe);
}
Bitmap bm = BitmapFactory.decodeStream(is);
iv.setImageBitmap(bm);
/*
* if (bitmaps[position] != null) { bitmaps[position].recycle();
* bitmaps[position] = null; } bitmaps[position] = bm;
*/
return iv;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了和你一样的问题,查看 ScaleType 文档,我发现它可以使用 ScaleType.MATRIX 来完成,例如:
这个解决方案使事情变得有点复杂。如果你想滚动和缩放 ImageView,你还需要使用矩阵缩放。所以我有兴趣了解任何可能的替代方案。
I had the same problem as you and looking at ScaleType documentation I found it could be done using ScaleType.MATRIX, for example:
This solution complicates things a little bit too much though. If you want to scroll and zoom the ImageView you need to use matrix scaling as well. So I'd be interested in knowing any possible alternative.
对于此类任务,我使用这个简单的类。它适合正确缩放图像的高度或宽度(这取决于哪个尺寸较小)。执行此操作后,它将图像置于 ImageView 边界的中心。
For this kind of tasks I use this simple class. It fits height or width scaling the image properly (it depends on which is the smaller dimension) . After this operation it centers the image in the ImageView bounds.
我最终只是自己将位图修剪成正方形,因为,
I ended up just trimming the bitmap to a square myself, as,