我需要一个可以触摸放大/缩小和平移的ImageView。每当我触摸ImageView时,我都需要知道源图像的触摸位置。例如,图像分辨率为1280 * 720,即使ImageView中的图像被放大,我仍然确切地知道图像的触摸位置(不是ImageView的触摸位置)
感谢。
答案 0 :(得分: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();
答案 1 :(得分:0)
最后,我使用了这个开源代码 https://github.com/sephiroth74/ImageViewZoom
我修改了代码,但是在这个开源项目中仍然存在很多错误,我修复了其中的几个。
以下函数已添加到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;
}