1.先自定义画个小球。
public class DrawView extends View {
//定义并创建画笔
Paint p = new Paint();
public DrawView(Context context) {
super(context);
}
public DrawView(Context context, AttributeSet set) {
super(context, set);
}
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (flag == 1) {
p.setColor(Color.RED);
} else if (flag == 2) {
p.setColor(Color.GREEN);
} else if (flag == 3) {
p.setColor(Color.BLUE);
} else if (flag == 4) {
p.setColor(Color.YELLOW);
}
canvas.drawCircle(currentX, currentY, 6, p);
}
}
2.在activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:opencv="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout
android:id="@+id/relative"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="10">
<org.opencv.android.JavaCameraView
android:id="@+id/camera_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
opencv:camera_id="any"
opencv:show_fps="true" />
</RelativeLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical">
<Button
android:id="@+id/setting_point_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置点" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
3.在MainActivity上 部分代码
初始化
private RelativeLayout mRelativeLayout;
private DrawView mDrawView;
private JavaCameraView mCVCamera;
mCVCamera.setOnTouchListener(new mOnTouch());
class touch implements View.OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX() - (mCVCamera.getWidth() - mCols) / 2; // getX是获取相对当前控件的坐标
int y = (int) event.getY() - (mCVCamera.getHeight() - mRows) / 2; // getRawX是获取相对屏幕左上角的坐标
switch (event.getAction()) {
// ACTION_DOWN 按下
// ACTION_MOVE 在屏幕上移动
// ACTION_UP 离开屏幕
case MotionEvent.ACTION_DOWN:
mDrawView = new DrawView(MainActivity.this);
mDrawView.currentY = (int)event.getY();
mDrawView.currentX = (int)event.getX();
mDrawView.flag = 1;
//添加标记(控件)
mRelativeLayout.addView(mDrawView);
//添加标记(删除)
//mRelativeLayout.removeView(mDrawView);
}
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
}