2016-11-26 65 views
0

我需要可触及/缩小和平移缩放的ImageView的。每次当我触碰ImageView的时间,我需要知道的源图像的触摸位置。例如图像分辨率为1280 * 720,即使ImageView中的图像被放大,我仍然确切知道图像的触摸位置(不是触摸ImageView的位置)。任何Android开源ImageView的?

谢谢。

回答

0

最后,我用这个开放的源代码 https://github.com/sephiroth74/ImageViewZoom

我修改了代码,但仍有在这个开源亲了很多的bug JECT,我有固定的几个人。

将以下函数添加到ImageViewTouch.GestureListener类中,该类用于将实际图像中的接触点(在屏幕中)转换为点(无论图像是放大/缩小)。

private PointF calculatePositionInSourceImage(PointF touchPointF) { 
     //point relative to imageRect 
     PointF touchPointRelativeToImageRect = new PointF(); 
     RectF imageRect = getBitmapRect(); 
     touchPointRelativeToImageRect.set(touchPointF.x - imageRect.left, 
       touchPointF.y - imageRect.top); 

     //real image resolution 
     int imageWidth = getDrawable().getIntrinsicWidth(); 
     int imageHeight = getDrawable().getIntrinsicHeight(); 

     //touch point in image 
     PointF touchPointRelativeToImage = new PointF(); 
     touchPointRelativeToImage.set(touchPointRelativeToImageRect.x/imageRect.width() * imageWidth, 
       touchPointRelativeToImageRect.y/imageRect.height() * imageHeight); 

     if(touchPointRelativeToImage.x < 0 || touchPointRelativeToImage.y < 0) 
      touchPointRelativeToImage.set(0,0); 

     return touchPointRelativeToImage; 
    } 
0

你为什么不跳过所有并使用现有的库?

试试这个:PhotoView作品完美的我在API 23,只用几行代码,你将能够放大/缩小和平移

compile 'com.github.chrisbanes:PhotoView:1.3.0' 

repositories { 
    ... 
    maven { url "https://jitpack.io" } 
} 

ImageView mImageView; 
PhotoViewAttacher mAttacher; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 

// Any implementation of ImageView can be used! 
mImageView = (ImageView) findViewById(R.id.iv_photo); 

// Set the Drawable displayed 
Drawable bitmap = getResources().getDrawable(R.drawable.wallpaper); 
mImageView.setImageDrawable(bitmap); 

// Attach a PhotoViewAttacher, which takes care of all of the zooming functionality. 
// (not needed unless you are going to change the drawable later) 
mAttacher = new PhotoViewAttacher(mImageView); 
} 

// If you later call mImageView.setImageDrawable/setImageBitmap/setImageResource/etc then you just need to call 
mAttacher.update(); 
+0

看起来不错,我可以在ImageView/PhotoView中获取图像的触摸位置吗? – xhsoldier

+0

PhotoView中会照顾变焦的,如果你双击图像将在该位置放大,如果你需要知道我想你可以从库中获取这些值的位置,但你会需要改变一些事情 –