是否有一种通用的方法来显示大图,并使用户能够放大、缩小和平移图像?
到目前为止,我找到了两种方法:
覆盖ImageView,对于这样一个常见的问题,这似乎有点太多了。 使用webview,但对整体布局的控制较少等等。
是否有一种通用的方法来显示大图,并使用户能够放大、缩小和平移图像?
到目前为止,我找到了两种方法:
覆盖ImageView,对于这样一个常见的问题,这似乎有点太多了。 使用webview,但对整体布局的控制较少等等。
当前回答
如下所示就可以了。
@Override public boolean onTouch(View v,MotionEvent e)
{
tap=tap2=drag=pinch=none;
int mask=e.getActionMasked();
posx=e.getX();posy=e.getY();
float midx= img.getWidth()/2f;
float midy=img.getHeight()/2f;
int fingers=e.getPointerCount();
switch(mask)
{
case MotionEvent.ACTION_POINTER_UP:
tap2=1;break;
case MotionEvent.ACTION_UP:
tap=1;break;
case MotionEvent.ACTION_MOVE:
drag=1;
}
if(fingers==2){nowsp=Math.abs(e.getX(0)-e.getX(1));}
if((fingers==2)&&(drag==0)){ tap2=1;tap=0;drag=0;}
if((fingers==2)&&(drag==1)){ tap2=0;tap=0;drag=0;pinch=1;}
if(pinch==1)
{
if(nowsp>oldsp)scale+=0.1;
if(nowsp<oldsp)scale-=0.1;
tap2=tap=drag=0;
}
if(tap2==1)
{
scale-=0.1;
tap=0;drag=0;
}
if(tap==1)
{
tap2=0;drag=0;
scale+=0.1;
}
if(drag==1)
{
movx=posx-oldx;
movy=posy-oldy;
x+=movx;
y+=movy;
tap=0;tap2=0;
}
m.setTranslate(x,y);
m.postScale(scale,scale,midx,midy);
img.setImageMatrix(m);img.invalidate();
tap=tap2=drag=none;
oldx=posx;oldy=posy;
oldsp=nowsp;
return true;
}
public void onCreate(Bundle b)
{
super.onCreate(b);
img=new ImageView(this);
img.setScaleType(ImageView.ScaleType.MATRIX);
img.setOnTouchListener(this);
path=Environment.getExternalStorageDirectory().getPath();
path=path+"/DCIM"+"/behala.jpg";
byte[] bytes;
bytes=null;
try{
FileInputStream fis;
fis=new FileInputStream(path);
BufferedInputStream bis;
bis=new BufferedInputStream(fis);
bytes=new byte[bis.available()];
bis.read(bytes);
if(bis!=null)bis.close();
if(fis!=null)fis.close();
}
catch(Exception e)
{
ret="Nothing";
}
Bitmap bmp=BitmapFactory.decodeByteArray(bytes,0,bytes.length);
img.setImageBitmap(bmp);
setContentView(img);
}
为查看完整的程序看这里:程序放大图像在android
其他回答
@Robert Foss, @Mike Ortiz,非常感谢你们的工作。我合并了你的工作,并完成了罗伯特的android> 2.0课程与迈克的额外工作。
由于我的工作,我提出了Android触摸画廊,基于ViewPager和使用修改的TouchImageView。图片通过URL加载,你可以缩放和拖动它们。你可以在这里找到它https://github.com/Dreddik/AndroidTouchGallery
更新
我刚刚给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中,请参考这个答案。
我使用WebView和加载图像从内存通过
webview.loadUrl("file://...")
WebView处理所有的平移、缩放和滚动。如果你使用wrap_content, webview不会比图像大,也不会显示白色区域。 WebView更好的ImageView;)
尝试使用ZoomView缩放任何其他视图。
http://code.google.com/p/android-zoom-view/它很容易,免费和有趣的使用!
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