我需要一个ImageView,可以触摸,放大/缩小和平底锅。每次我触摸ImageView时,我都需要知道源图像的触摸位置。例如,图像分辨率为1280*720,即使ImageView中的图像被放大,我仍然确切地知道图像的触摸位置(不是ImageView的触摸位置)。
谢谢。
发布于 2016-11-29 10:27:49
最后,我使用了这个开源代码https://github.com/sephiroth74/ImageViewZoom。
我修改了代码,但是在这个开源项目中仍然有很多bug,我已经修复了其中的几个。
下面的函数被添加到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;
}发布于 2016-11-26 01:59:32
为什么不跳过所有这些,使用现有的库呢?
试试这个: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();https://stackoverflow.com/questions/40814127
复制相似问题