图库和全屏 ImageView,将它们放在一起的问题
我会尽量说得清楚。
这里我正在尝试做
一个图像查看器,其中图像将在设备屏幕上尽可能大地显示。 为了这个目的,为了更好的用户体验,我想到了一个 Gallery。 直到一切都OK!
问题
问题是,在我的适配器的 getView 函数中,它仅使用我的图库中第一个图像的 Gallery.LayoutParams 。这意味着,如果第一张图片是风景,第二张图片是肖像,则第二张图片将显示为风景,尺寸与第一张图片相同。我确实重置了 Gallery.LayoutParams 但没关系,它仍然具有第一个 ImageView 的 LayoutParams 。
代码
public View getView(int position, View convertView, ViewGroup parent) {
ImageView im = new ImageView(mContext);
Bitmap bm = BitmapFactory.decodeByteArray(gallery.get(position).mContent, 0, gallery.get(position).mContent.length);
im.setImageBitmap(bm);
int width = 0;
int height = 0;
if (bm.getHeight() < bm.getWidth()) {
width = getWindowManager().getDefaultDisplay().getWidth();
height = bm.getHeight() * width / bm.getWidth();
}
else {
height = getWindowManager().getDefaultDisplay().getHeight();
width = bm.getWidth() * height/ bm.getHeight();
}
Gallery.LayoutParams lp = new Gallery.LayoutParams(width, height);
im.setLayoutParams(lp);
return im;
}
如果你们中有人明白为什么我真的很高兴知道答案,
I will try to be as clear as possible.
Here What I am trying to do
An Image Viewer, where Image would be display as big as they can on the device screen.
For this purspose and for a better user experience, I thought of a Gallery.
Untill there everything is OK!
The problem
The problem is that in the function getView of my Adapter, it uses only the Gallery.LayoutParams of the first Image I have in my Gallery. Which means that if the first picture is a landscape and the second one a portrait the second one will be display as landscape, with the same dimension as the first one. I do reset the Gallery.LayoutParams but it does not matter, it still have the LayoutParams of the first ImageView.
The code
public View getView(int position, View convertView, ViewGroup parent) {
ImageView im = new ImageView(mContext);
Bitmap bm = BitmapFactory.decodeByteArray(gallery.get(position).mContent, 0, gallery.get(position).mContent.length);
im.setImageBitmap(bm);
int width = 0;
int height = 0;
if (bm.getHeight() < bm.getWidth()) {
width = getWindowManager().getDefaultDisplay().getWidth();
height = bm.getHeight() * width / bm.getWidth();
}
else {
height = getWindowManager().getDefaultDisplay().getHeight();
width = bm.getWidth() * height/ bm.getHeight();
}
Gallery.LayoutParams lp = new Gallery.LayoutParams(width, height);
im.setLayoutParams(lp);
return im;
}
If any of you see why I would be really please to know the answer,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
画廊强制所有孩子都具有相同的尺寸。另外,每次调用 getView() 时都会创建一个新视图,这是一个糟糕的主意。当它不为空时,您应该使用convertView。目前 Gallery 中缺少一个功能,因此 ConvertView 始终为 null,但为了在 Android 的未来版本中受益,请无论如何使用 ConvertView。这也是一个很好的做法,您必须在编写的每个适配器中应用它。
Gallery forces all children to have the same size. Also, you are creating a new View every time getView() is called and it's a terrible idea. You should use the convertView when it's not null. There's currently a missing feature in Gallery so convertView is always null, but to benefit from it in a future version of Android, use the convertView anyway. It's also a good practice that you must apply in every Adapter you write.