开发者

How to draw a line in an IF statement for java

开发者 https://www.devze.com 2023-03-08 08:41 出处:网络
I am trying to program an androd application where if there is input on two places of the screen in sucsession then it will draw a line between the two points. I have already set up \"X\" and \"Y\" va

I am trying to program an androd application where if there is input on two places of the screen in sucsession then it will draw a line between the two points. I have already set up "X" and "Y" values that work and columns and rows are defined by the "X" and "Y" values. After those i have an IF statement that needs to draw a line between the two points. Say if column one and row two are selected and then colum one and row three are selcted I want a line to be drawn between the two points. Also I am not totally sure how to use the MotionEvent stuff or how to put the touch actions into the IF statement.

     final View touchView = findViewById(R.id.touchView);
touchView.setOnTouchListener(new View.OnTouchListener() {
    @Override         
    public boolean onTouch(View v, MotionEvent event) { 
            String.valueOf(event.getX() + String.valueOf(event.getY()));
        double c = event.getX();
        double column = Math.floor(event.getX()/(480/12));
        double r = event.getY();
        double row = Math.floor(event.getY()/(630/12));


    if (column == 0 && row == 2 //there should be more stuff here 
                   ) {
                  //I dont know how to draw a line in here, please help
    }
        retu开发者_高级运维rn true;     
        }
    });   
}


Rather than explain the details here, I'll point you to these pieces of sample code from the ApiDemos sample project that comes with the SDK, that probably do exactly what you want:

  • DrawPoints.java
  • TouchPaint.java

The basic idea is to store X and Y coordinates in your touch event handler, invalidate the View, and then draw the lines in the onDraw method using Canvas operations such as drawLine.


You do neew to have a tool to draw the line, most suitable for you it seems to be Canvas. If you don't know anything about Canvas together with Android yet i suggest you to look into some examles that Android leaves us. Ones you have done that, this is going to be a simple task.

0

精彩评论

暂无评论...
验证码 换一张
取 消