0

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
    }
        return true;     
        }
    });   
}
Pillager225
  • 420
  • 1
  • 3
  • 14
  • see this thread: [How to draw line in android.](http://stackoverflow.com/questions/3616676/how-to-draw-a-line-in-android) – Harry Joy May 24 '11 at 04:02

2 Answers2

2

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:

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.

Roman Nurik
  • 29,417
  • 7
  • 81
  • 82
  • Thank you, but what and where do you get the Graphics Activity that gets extended, but if you dont need that then what do you do. Sorry that I need so much help, but if you cant tell, I am really new. – Pillager225 May 24 '11 at 04:19
  • `GraphicsActivity` is also in the ApiDemos sample app: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/GraphicsActivity.html – Roman Nurik May 24 '11 at 15:47
0

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.

Henrik
  • 1,097
  • 2
  • 11
  • 22
  • This may seem like a stupid question, but where did android leave us these examples ? – Pillager225 May 24 '11 at 04:30
  • Well, i don't remember if there's any example online at http://developer.android.com/index.html . There may be but however, there is in the samples directory in your'e android sdk. For example Lunar-Lander, that is a game using Canvas in android. – Henrik May 24 '11 at 08:56