Android - User paints lines with their finger

不想你离开。 提交于 2019-12-06 04:39:47

To store the lines drawn, and enable the user to manipulate them, is only moderately complex, in my opinion.

Create a Line class. Include the start and end co-ordinates, colour etc as fields of the class and methods to delete, move etc. Also add a method which takes a MotionEvent argument. In this method, you use the MotionEvent to determine if this line has been touched and adjust it's position as you need.

Store references to each line drawn (created as an instance of the Line class) in a collection or list of some sort in your extended View class, ArrayList should do. Then, in the onTouch event, call the touch detect method of each line and pass the MotionEvent to the methods.

Finally, override the onDraw callback of the View to draw each line by iterating through the collection of references to the Line instances.

You could also add more gesture such as long press for delete etc. Here's a (totally untested, written from memory) skeleton of such an approach.

class Line

    public float x1, x2, y1, y2;

    public void detectTouch(MotionEvent motionEvent){

         // code to detect a touch and adjust the x and ys accordingly

    }

class DrawView extends View{

    private ArrayList<Line> lines = new ArrayList<Lines>();

    ....
    ....

@Override
public void onDraw(Canvas canvas) {

    super.onDraw();

    for (Line line : lines){
         canvas.drawLine(line.x1,line.y1,line.x2,line.y2);
    }
}

@Override
public boolean onTouch(View view, MotionEvent event) {

    ....
    ....

    for (Line line : lines){
        line.detectTouch(MotionEvent);
    }


}

Have fun!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!