In Android I have an scaled and centered custom imageview. On the imageview an image is drawn via canvas and the imageview is scaled to the scaled bitmap. I want to move an Rect inside this custom view. The problem is, that moving the Rectangle only works with raw events and so the rect disappears before the bottom is reached. (because the imageview thinks that the touchevent is outside)
i overided the onDraw method:
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mImageBitmap, 0, 0, mImagePaint);
canvas.setMatrix(matrix);
Rect rt = new Rect((int)rawEventX-50, (int)rawEventY-50, (int) rawEventX+50, (int) rawEventY + 50);
// works, but disappears before bottom of imageview is reached.. (because the imageview thinks that the touchevent is outside)
//Rect rt = new Rect((int)eventX-50, (int)eventY-50, (int) eventX+50, (int) eventY + 50);
// This rect is draw over and left of the finger :-(
canvas.drawRect(rt, paintText);
}
The scaling of image and custom imageview:
protected void scaleBitmapToImageViewSizeWithAntiAliashing(Bitmap bitmap) {
getLayoutParams().width = scaledWidth;
getLayoutParams().height = scaledHeight;
refreshDrawableState();
mImageBitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true);
mImageCanvas = new Canvas(mImageBitmap);
}
I also have an onTouchEvent:
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
eventX = event.getX();
eventY = event.getY();
rawEventX = event.getRawX();
rawEventY = event.getRawY();
savedMatrix.set(matrix);
//start.set(event.getRawX(), event.getRawY());
start.set(eventX, eventY);
mode = Constants.DRAG;
break;
case MotionEvent.ACTION_MOVE:
matrix.set(savedMatrix);
eventX = event.getX();
eventY = event.getY();
rawEventX = event.getRawX();
rawEventY = event.getRawY();
break;
}
invalidate();
return true;
}
Is it possible to use only the imageview coordinates inside the custom imageview for the onTouchEvent? How the OnTouchEvent is generelly used in an Imageview? I did not found any solution via google and i am really frustrated.