0

Strange problem I'm having here. I add to an arraylist when the user clicks on the view. I get the position and add it to an ArrayList of coordinates. I then draw a circle on the canvas where the coordinates says to do so. The Size check in onDraw always returns 0.

private static ArrayList<Coordinate> coords = new ArrayList<Coordinate>();

OnTouchEvent

...
case MotionEvent.ACTION_POINTER_UP: {
    mLastTouchX = event.getX();
    mLastTouchY = event.getY();
    this.coords.add(new Coordinate(mLastTouchX, mLastTouchY));
    break;
...

OnDraw

protected void onDraw(Canvas canvas) {

    super.onDraw(canvas);
    for(int i = 0; i < this.coords.size(); i++) {
        canvas.drawCircle(coords.get(i).x, coords.get(i).y, 30.0f, mPaint);
    }
}
2
  • Are you sure the motion event code is ever executed? Commented Aug 24, 2012 at 5:24
  • @veer See the comments on my post, his issue appears to be that onDraw is only called once at init, so it never updates with the new coordinates Commented Aug 24, 2012 at 5:29

1 Answer 1

1

How are you expecting to get a static field with this ? But assuming that's just some typo, try adding some logging:

case MotionEvent.ACTION_POINTER_UP: {
    mLastTouchX = event.getX();
    mLastTouchY = event.getY();
    this.coords.add(new Coordinate(mLastTouchX, mLastTouchY));
    System.out.println("Added a coordinate; new size: " + coords.size());//to see if we are adding it
    break;

And in your onDraw:

System.out.println(coords);//Just to see what all is in it
for(int i = 0; i < this.coords.size(); i++) {
    canvas.drawCircle(coords.get(i).x, coords.get(i).y, 30.0f, mPaint);
}
Sign up to request clarification or add additional context in comments.

8 Comments

You can access static methods via an instance.
Seems like onDraw isn't being called continuously ... when printing coords, I get an empty list. This print occurs one time.
Ah, yes, was thinking it was flipped. On a different note that, you should probably use the class's name, not this (ex. ClassName.coords, not this.coords)
@lostdev Are their any other methods that you can override that have to do with painting? I'm not familiar with the android API, but it appears that's the issue (that it's only called once to init)
My understanding of the Android onDraw() is that its called cyclically. This doesn't seem to be the case. It maybe helpful to know the Class extends View.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.