2016-01-29 2 views
1

Итак, у меня есть ImageView, где я устанавливаю png в качестве фона. Предположим, что png-изображение - это круг, поэтому он не занимает все пространство ImageView. Мне нужно поймать событие, щелкнув только содержимое ImageView (круг), а не оставшуюся пустую область. Возможно ли это вообще или с любым другим элементом управления Android?Координаты содержания ImageView

Редактировать: Я до сих пор не могу заставить это работать. Так, чтобы быть более точным у меня есть эта картина яйца, которые я поставил в ImageView ЦСИ

enter image description here

И я хочу, чтобы иметь возможность только нажать на него. Например, если я слегка щелкнул за пределами яйца, я хочу знать это и предотвратить код внутри события клика ImageView.

Я попробовал этот код, но я не имею ни малейшего представления, как это помогает мне:

ImageView imageView = (ImageView)findViewById(R.id.imageview); 
Drawable drawable = imageView.getDrawable(); 
Rect imageBounds = drawable.getBounds(); 

//original height and width of the bitmap 
int intrinsicHeight = drawable.getIntrinsicHeight(); 
int intrinsicWidth = drawable.getIntrinsicWidth(); 

//height and width of the visible (scaled) image 
int scaledHeight = imageBounds.height(); 
int scaledWidth = imageBounds.width(); 

//Find the ratio of the original image to the scaled image 
//Should normally be equal unless a disproportionate scaling 
//(e.g. fitXY) is used. 
float heightRatio = intrinsicHeight/scaledHeight; 
float widthRatio = intrinsicWidth/scaledWidth; 

//do whatever magic to get your touch point 
//MotionEvent event; 

//get the distance from the left and top of the image bounds 
int scaledImageOffsetX = event.getX() - imageBounds.left; 
int scaledImageOffsetY = event.getY() - imageBounds.top; 

//scale these distances according to the ratio of your scaling 
//For example, if the original image is 1.5x the size of the scaled 
//image, and your offset is (10, 20), your original image offset 
//values should be (15, 30). 
int originalImageOffsetX = scaledImageOffsetX * widthRatio; 
int originalImageOffsetY = scaledImageOffsetY * heightRatio; 
+1

Проверьте ссылку HTTP://stackoverflow.com/questions/11334984/correspondence-between-imageview-coordinates-and-bitmap-pixels-android и http://stackoverflow.com/questions/11311337/select-content-inside-imageview-android, это поможет вам –

ответ

1

вот как вы получите событие Click:

int[] viewCoords = new int[2]; 
imageView.getLocationOnScreen(viewCoords); 
//From this and the touch coordinates you can calculate the point inside the ImageView: 

int touchX = (int) event.getX(); 
int touchY = (int) event.getY(); 

int imageX = touchX - viewCoords[0]; // viewCoords[0] is the X coordinate 
int imageY = touchY - viewCoords[1]; // viewCoords[1] is the y coordinate 
+0

Я не могу в полной мере получить его. Используя эту логику, как я могу узнать, щелкнул ли я внутри области изображение в изображении? Есть ли какое-либо условие для этого? – Binev

Смежные вопросы