簡體   English   中英

試圖從ImageView的onTouchListener獲取Bitmap像素

[英]Attempting to get Bitmap pixel from an ImageView's onTouchListener

我在ImageView上顯示一個位圖。 當點擊ImageView時,我想獲得點擊它的Bitmap的像素x / y坐標。 我已經注冊了ImageView的onTouchListener,在onTouch方法中,我使用getX和getY來獲取觸摸的位置。 問題是ImageView中的圖像可能比視圖本身大,因此按比例縮小以適應屏幕。 然后,返回的x和y坐標是視圖的坐標,但不一定是位圖上相應像素的坐標。

我可以獲得某種比例因子來了解它的調整大小嗎? 或者如果沒有,有人可以建議我如何獲得我需要的信息嗎? 最重要的是我可以獲得像素坐標 - 如果我必須更改視圖類型,那就沒問題了。

此外,有時位圖比屏幕小,因此可以將其縮放。 在這種情況下,從MotionEvent接收的x和y可能超出實際位圖的邊界。

我可以跨越同一個案例。 該線程已有一年多的歷史,但認為它可能對某人有用。 我必須在設置到ImageView之前縮放位圖。

這是代碼:

//Convert Uri to Bitmap
inputBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);

//Scale the bitmap to fit in the ImageView
Bitmap bmpScaled = Bitmap.createScaledBitmap(inputBitmap, img.getWidth(), img.getHeight(),true);

//Set the scaled bitmap to the ImageView
img.setImageBitmap(bmpScaled);

此后,觸摸的坐標與位圖的坐標相同。

檢查ScaleType屬性。 你必須根據getScaleType()的結果對原始圖像進行數學運算,但值得一試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM