android实现触屏点击进行标记(通过动态添加控件的方法)

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






    





    

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;
    }
 }

你可能感兴趣的:(android-opencv)