Android ViewPager 与可缩放 ImageView
我有一个由 ImageViews 制作的 ViewPager,实现如下:
@Override
public Object instantiateItem(View collection, int position) {
ImageView iv = new ImageView(ctx);
if (pageList.size() > position)
iv.setImageBitmap(pageList.get(position));
else
iv.setImageResource(R.drawable.loading);
iv.setOnTouchListener(this);
((ViewPager) collection).addView(iv, 0);
System.out.println("POS: " + position);
return iv;
}
我有机会通过双击(并滑动图像)或捏缩放来缩放 ImageView 吗?
I've got a ViewPager made by ImageViews implemented like this:
@Override
public Object instantiateItem(View collection, int position) {
ImageView iv = new ImageView(ctx);
if (pageList.size() > position)
iv.setImageBitmap(pageList.get(position));
else
iv.setImageResource(R.drawable.loading);
iv.setOnTouchListener(this);
((ViewPager) collection).addView(iv, 0);
System.out.println("POS: " + position);
return iv;
}
Any chance i can have that ImageView zoomable by double tap (and swipe the image) or pinch zoomable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是有可能的,我就是这样做的。
我的顶级布局元素是 FrameLayout,它允许多个视图彼此堆叠。
FrameLayout
有两个子项:ViewPager
和ImageView
,我将用它来显示放大的图片。ImageView 通常是隐藏的。我监听 ViewPager 内图片的触摸事件,并根据这些事件显示、隐藏和平移 ImageView。它运作得相当好。如果需要的话我可以提供更多细节。
It's possible, here's how I did it.
My top level layout element is
FrameLayout
which allows several views to be stacked on top of each other. TheFrameLayout
has two children: theViewPager
and anImageView
that I'll use for showing zoomed-in picture.The ImageView is normally hidden. I listen for touch events on pictures inside the
ViewPager
and based on those, show and hide and pan the ImageView. It works fairly well. I can give more details if need be.我使用了 Mike Ortiz 的 TouchImageView 类。它并不完美。但这很酷,因为您可以轻松地将其放入!双击缩放动画可能很有趣。捏缩放可以通过双击来触发,这很奇怪。
如果您直接将其放入 ViewPager(通过适配器类)中,它确实可以很好地工作。放大时,您可以自由地左右移动,而不会触发页面更改除非您就在边缘,这真的很酷!
https://github.com/MikeOrtiz/TouchImageView /blob/master/src/com/ortiz/touch/TouchImageView.java
I used the TouchImageView class by Mike Ortiz. It's not perfect. But it's cool because you can drop it in really easily! The double-tap zoom animation can be funny. The pinch zoom can be triggered via double-taps which is weird.
It does work really well if you drop it straight into a ViewPager (via an adapter class). When zoomed in you can freely move left and right without triggering a page change unless you are right on the edge, which is really cool!
https://github.com/MikeOrtiz/TouchImageView/blob/master/src/com/ortiz/touch/TouchImageView.java
经过大量搜索,在许多过时和已删除的库中,我发现这个库是最新的,并且在 Viewpagers 中也像一个魅力一样:
https://github.com/davemorrissey/subsampling-scale-image-view
这是它的工作原理:
After searching a lot and among a lot of outdated and deleted libraries I have found this one which is up to date and also works like a charm inside Viewpagers:
https://github.com/davemorrissey/subsampling-scale-image-view
here's how it works: