是否有一种通用的方法来显示大图,并使用户能够放大、缩小和平移图像?
到目前为止,我找到了两种方法:
覆盖ImageView,对于这样一个常见的问题,这似乎有点太多了。 使用webview,但对整体布局的控制较少等等。
是否有一种通用的方法来显示大图,并使用户能够放大、缩小和平移图像?
到目前为止,我找到了两种方法:
覆盖ImageView,对于这样一个常见的问题,这似乎有点太多了。 使用webview,但对整体布局的控制较少等等。
当前回答
This is a very late addition to this thread but I've been working on an image view that supports zoom and pan and has a couple of features I haven't found elsewhere. This started out as a way of displaying very large images without causing OutOfMemoryErrors, by subsampling the image when zoomed out and loading higher resolution tiles when zoomed in. It now supports use in a ViewPager, rotation manually or using EXIF information (90° stops), override of selected touch events using OnClickListener or your own GestureDetector or OnTouchListener, subclassing to add overlays, pan while zooming, and fling momentum.
它不打算作为ImageView的一般使用替代品,因此不扩展它,并且不支持显示来自资源的图像,只支持资产和外部文件。它需要SDK 10。
源代码在GitHub上,有一个示例说明了在ViewPager中的使用。
https://github.com/davemorrissey/subsampling-scale-image-view
其他回答
你可以尝试使用LayoutParams
public void zoom(boolean flag){
if(flag){
int width=40;
int height=40;
}
else{
int width=20;
int height=20;
}
RelativeLayout.LayoutParams param=new RelativeLayout.LayoutParams(width,height); //use the parent layout of the ImageView;
imageView.setLayoutParams(param); //imageView is the view which needs zooming.
}
ZoomIn = zoom(true); zoom = zoom(false);
This is a very late addition to this thread but I've been working on an image view that supports zoom and pan and has a couple of features I haven't found elsewhere. This started out as a way of displaying very large images without causing OutOfMemoryErrors, by subsampling the image when zoomed out and loading higher resolution tiles when zoomed in. It now supports use in a ViewPager, rotation manually or using EXIF information (90° stops), override of selected touch events using OnClickListener or your own GestureDetector or OnTouchListener, subclassing to add overlays, pan while zooming, and fling momentum.
它不打算作为ImageView的一般使用替代品,因此不扩展它,并且不支持显示来自资源的图像,只支持资产和外部文件。它需要SDK 10。
源代码在GitHub上,有一个示例说明了在ViewPager中的使用。
https://github.com/davemorrissey/subsampling-scale-image-view
我刚刚整合了Robert Foss的TouchImageView:它完美地开箱即用!谢谢!
我只是修改了一些代码,所以我可以从我的layout.xml实例化它。
只需添加两个构造函数
public TouchImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public TouchImageView(Context context) {
super(context);
init(context);
}
并将旧的构造函数转换为init方法:
private void init(Context context){
//...old code ofconstructor of Robert Moss's code
}
我使用WebView和加载图像从内存通过
webview.loadUrl("file://...")
WebView处理所有的平移、缩放和滚动。如果你使用wrap_content, webview不会比图像大,也不会显示白色区域。 WebView更好的ImageView;)
更新
我刚刚给TouchImageView一个新的更新。它现在包括双击缩放和抛除平移和捏缩放。下面的代码非常过时。你可以查看github项目以获得最新的代码。
使用
在项目中放置TouchImageView.java。这样就可以像 ImageView。例子:
TouchImageView img = (TouchImageView) findViewById(R.id.img);
如果你在xml中使用TouchImageView,那么你必须提供完整的包 名称,因为它是一个自定义视图。例子:
<com.example.touch.TouchImageView
android:id="@+id/img”
android:layout_width="match_parent"
android:layout_height="match_parent" />
注意:我已经删除了我之前的答案,其中包括一些非常旧的代码,现在直接链接到github上最新的代码。
ViewPager
如果你有兴趣把TouchImageView放在ViewPager中,请参考这个答案。