在Android中,我有一个缩放和居中的自定义imageview。 在imageview上,通过画布绘制图像,并将imageview缩放到缩放的位图。 我想在这个自定义视图中移动一个Rect。 问题是,移动Rectangle仅适用于原始事件,因此rect在到达底部之前消失。 (因为imageview认为touchevent在外面)
我覆盖了onDraw方法:
@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);
}
缩放图像和自定义图像视图:
protected void scaleBitmapToImageViewSizeWithAntiAliashing(Bitmap bitmap) {
getLayoutParams().width = scaledWidth;
getLayoutParams().height = scaledHeight;
refreshDrawableState();
mImageBitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true);
mImageCanvas = new Canvas(mImageBitmap);
}
我还有一个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;
}
是否可以仅使用onTouchEvent的自定义imageview内的imageview坐标?如何在Imageview中使用OnTouchEvent? 我没有通过谷歌找到任何解决方案,我真的很沮丧。
答案 0 :(得分:1)
问题是: 如果你想从基于像素的View coords映射到ImageView绘制coords,那么使用getImageMatrix()invert()它,然后使用mapPoints()在倒置矩阵上 - 感谢pskink