Android:MotionEvent.getY()返回压缩值

问题描述 投票:0回答:1

我正在尝试确定用户是否正在触摸ImageView的绘制的位图...

    mCanvasImageView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            //get coordinates
            float x = event.getX();
            float y = event.getY();

            //calculate Bitmap's drawn width and height 
            int drawnBitmapWidth;
            int drawnBitmapHeight;
            int imageViewWidth = mCanvasImageView.getWidth();
            int imageViewHeight = mCanvasImageView.getHeight();
            int bitmapWidth = mCanvasImageView.getDrawable().getIntrinsicWidth();
            int bitmapHeight = mCanvasImageView.getDrawable().getIntrinsicHeight();
            if (imageViewHeight * bitmapWidth <= imageViewWidth * bitmapHeight) {
                drawnBitmapWidth = bitmapWidth * imageViewHeight / bitmapHeight;
                drawnBitmapHeight = imageViewHeight;
            } else {
                drawnBitmapHeight = bitmapHeight * imageViewWidth / bitmapWidth;
                drawnBitmapWidth = imageViewWidth;
            }

            //determine if touch is in active area     
            if ( y > (0 + drawnBitmapHeight/2) && y < (imageViewHeight - drawnBitmapHeight/2) )
                Log.d(TAG, "IN ACTIVE Y REGION");
            else
                Log.d(TAG, "NOT IN ACTIVE Y REGION");

            //width is 1080, height is 1920
            //bitmap drawn width is 1080, height is 1080
            //active start 540 active end 1380

            return true;
        }

    });
}

所有值均已正确计算。 ImageView将填满整个屏幕,分辨率为1080x1920。位图在ImageView中居中放置,分辨率为1080x1080。因此,活动区域(以y表示)为540到1380。正确识别该范围内的触摸事件为活动区域。但是...

enter image description here

当我触摸时,好像活动区域在位图之后开始,在其之前结束。我还注意到,触摸屏幕的绝对顶部或底部将不会返回0或1920,但是会偏移y坐标。据此,我认为getY()和getRawY()有点不准确,但我无法确定原因。另请注意,操作栏是隐藏的,应用程序是全屏的,没有状态栏。

android android-imageview android-touch-event
1个回答
0
投票
假设位图居中,请尝试以下计算:

int bitmapTop = imageViewHeight/2 - drawnBitmapHeight/2 int bitmapBottom = imageViewHeight/2 + drawnBitmapHeight/2 //determine if touch is in active area if ( y >= bitmapTop && y < bitmapBottom ) Log.d(TAG, "IN ACTIVE Y REGION"); else Log.d(TAG, "NOT IN ACTIVE Y REGION"); //width is 1080, height is 1920 //bitmap drawn width is 1080, height is 1080 //active start 540 active end 1380 return true; }

© www.soinside.com 2019 - 2024. All rights reserved.