2016-11-26 66 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中會照顧變焦的,如果你雙擊圖像將在該位置放大,如果你需要知道我想你可以從庫中獲取這些值的位置,但你會需要改變一些事情 –