大家好,我在位图和画布上苦苦挣扎。我想要做的是用相机拍照,然后允许用户创建两个矩形(通过滑动手指)并在图像中标记它们(矩形应该被标记,直到按下按钮,照片没有保存,它总是在内存中)。因此,基于相机示例,我使用 SurfaceView 进行了布局以包含相机预览,然后我添加了代码以在 onPictureTaken 方法中绘制矩形。我已经搜索了一些有关如何实现它的示例,但当然不起作用。到目前为止,我有这段代码(在 onPictureTaken 内部):
final Bitmap bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0,
arg0.length);
surfaceView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
switch (arg1.getAction()) {
case MotionEvent.ACTION_DOWN:
if (source coordinates of rect1 are not set) {
setSourceCoordinatesForRect1FromArg1();
} else {
setSourceCoordinatesForRect2FromArg1();
}
break;
case MotionEvent.ACTION_UP:
if (end coordinates of rect1 are not set) {
setEndCoordinatesForRect1FromArg1();
} else {
setEndCoordinatesForRect2FromArg1();
}
break;
default:
break;
}
if (coordinates for rect1 are set) {
Paint paint = new Paint();
Bitmap bmOverlay = Bitmap.createBitmap(bitmapPicture.getWidth(), bitmapPicture.getHeight(), bitmapPicture.getConfig());
Canvas canvas = new Canvas(bmOverlay);
paint.setColor(Color.GREEN);
paint.setStrokeWidth(3);
canvas.drawRect(/*all of my source coordinates*/, paint);
} else {
if (coordinates for rect2 are set) {
Paint paint = new Paint();
Bitmap bmOverlay = Bitmap.createBitmap(bitmapPicture.getWidth(), bitmapPicture.getHeight(), bitmapPicture.getConfig());
Canvas canvas = new Canvas(bmOverlay);
paint.setColor(Color.YELLOW);
paint.setStrokeWidth(3);
canvas.drawRect(/*all of my end coordinates*/, paint);
}
}
return true;
}
});
我没有任何例外,但是没有绘制矩形,所以如果有人能告诉我我做错了什么,我将不胜感激。另外,对于我的特定场景,是否适合使用 GestureDetector 而不是创建自定义 OnTouchListener?提前致谢。