我有一个扩展的ImageView活动,其中我有一个位图,我通过onTouchEvent触摸感兴趣的点,检索触摸点的坐标,然后围绕该点绘制一个矩形。所以这部分代码完美无缺。
public class RectActivityView extends ImageView {
private Point point;
private Paint paint = new Paint();
private static final String FILE_SUFFIX_CSV = ".csv";
public RectActivityView(Context context) {
super(context);
}
public RectActivityView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RectActivityView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
point = new Point(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
point.set(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
//case MotionEvent.ACTION_CANCEL:
point = new Point(x, y);
invalidate();
//save last touched point
final String timeStamp2 = new SimpleDateFormat("yyyyMMdd").format(new Date());
String timeStamp3 = new SimpleDateFormat("HHmmss").format(new Date());
File direct = new File(Environment.getExternalStorageDirectory() + "/detectTracks/Setup");
if (!direct.exists()) {
direct.mkdirs();}
String entry = timeStamp2 + "," + timeStamp3 + "," + point.x + "," + point.y + "\n";
File dataFile = new File(new File("/sdcard/detectTracks/Setup"), timeStamp2 + FILE_SUFFIX_CSV);
try {
FileWriter logWriter = new FileWriter(dataFile, true);
BufferedWriter bw = new BufferedWriter(logWriter);
PrintWriter out = new PrintWriter(bw);
out.write(entry);
out.flush();
out.close();
//Toaster.show(this, R.string.export_data_succeeded);
} catch (Exception e) {
e.printStackTrace();}
break;
}return true;
}
@Override
protected void onDraw(@NonNull Canvas canvas) {
super.onDraw(canvas);
int size = 50;
if (point != null) {
Rect myROIouter = new Rect(point.x - size, point.y - size,point.x + size, point.y + size);
paint.setColor(Color.TRANSPARENT);
paint.setStyle(Paint.Style.FILL);
paint.setAlpha(80);
canvas.drawRect(myROIouter, paint);
// BORDER
paint.setStrokeWidth(5);
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
canvas.drawRect(myROIouter, paint);
}
}
}
然后我将这些坐标传递给另一个活动,我想在同一个位图顶部的坐标周围画一个圆圈,就像我在扩展的ImageView中那样。我已经用位图和imageView的宽高比校正了高度和宽度,但是钢铁不能在我之前的活动中触及的确切点上绘制圆圈。这里有什么我想念的吗?
第二项活动的部分代码:
//retrieved coordinates from .csv file
double xROIA = Double.parseDouble(dataA[2]);
double yROIA = Double.parseDouble(dataA[3]);
int bmpWidth = 3036;
int bmpHeight = 4048;
int widthView = 1328;
int heightView = 1770;
double aspectWidth = bmpWidth / widthView;
double aspectHeight = bmpHeight / heightView;
int coorX = (int) (xROIA * aspectWidth);
int coorY = (int) (yROIA * aspectHeight);
Imgproc.circle(image, point, size2/2, new Scalar(255,0,0),2);
Bitmap imageBitmap = Bitmap.createBitmap(image.cols(), image.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(image, imageBitmap);
BitmapHelper.showBitmap(this, imageBitmap, imageView);