Constant looping onDraw

Problems with Canvas, OpenGL, etc...

Constant looping onDraw

Postby mod9 » Sat Jan 22, 2011 12:45 pm

I have an unexplained problem...Let's explain the situation,i have a Ball class(a very very changed version from a topic i've found here)
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class Ball  {
  2.  private Bitmap img; // the image of the ball
  3.  private int coordX = 0; // the x coordinate at the canvas
  4.  private int coordY = 0; // the y coordinate at the canvas
  5.  private int id;
  6.  private int count;
  7.  private boolean drawable = true;
  9.         public Ball(Context context, int drawable, Point point, int c) {
  11.                 BitmapFactory.Options opts = new BitmapFactory.Options();
  12.         opts.inJustDecodeBounds = true;
  13.         img = BitmapFactory.decodeResource(context.getResources(), drawable);
  14.         id=c;
  15.                 c++;
  16.                 coordX= point.x;
  17.                 coordY = point.y;
  19.         }
  21.         //setters geters go here etc..
Parsed in 0.010 seconds, using GeSHi

I also have an other class Box pretty much the same with Ball class without the drawavle property.
Finally i have the main Activity class Basket which only calls the extended View MyView which is actually do all the job!

Because i wanted to get the full width and height first and then draw anything i've called the onSizeChanged and initialize everything in there

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.             protected void onSizeChanged(int w, int h, int oldw, int oldh){//move everything in a method
  4.                 Point point1 = new Point((getWidth()/2-50),getHeight()/2-50);
  5.                 Point point2 = new Point(100,20);
  6.                 Point point3 = new Point(150,20);//
  8.                 box = new Box(getContext(),R.drawable.boximg, point1);
  9.                 balls[0] = new Ball(getContext(),R.drawable.ball1, point2,1);
  10.                 balls[1] = new Ball(getContext(),R.drawable.ball2, point3,2);
  12.                 super.onSizeChanged(w, h, oldw, oldh);
  13.             }
Parsed in 0.011 seconds, using GeSHi

Then i handle everything onTouchEvent...(ACTION_DOWN,ACTION_MOVE,ACTION_UP) and i call the onDraw.
In onDraw i check if a ball can be drawn with the ball.getDrawable() some code
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override protected void onDraw(Canvas canvas) {
  3.                 canvas.drawBitmap(box.getBitmap(), box.getX(), box.getY(),null);
  5.                 //draw the balls on the canvas
  6.                 for (Ball ball: balls) {
  7.                         Log.i(TAG, "inside for "+ball.getID()+" "+ball.getDrawable());//a check msg
  8.                         if(ball.getDrawable()){
  9.                                 Log.i(TAG, "if inside for "+ball.getID()+" "+ball.getDrawable());
  11.                                 canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), null);
  12.                         }
  13.                   }
  15.             }
Parsed in 0.011 seconds, using GeSHi

And here comes the weird.....
When i handle ACTION_UP without setDrawable enabled everything works fine!but when i uncomment it so it can do the job and parse the false flag on the ball something doesn't work right!After the "disappear" of the first ball
wherever i touch i'm taking also the message "Locked" which is used for looks like it's constantly in the for loop of ACTION_UP!some code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. case MotionEvent.ACTION_UP:
  2.                         Log.i(TAG,"action_up "+getWidth()+" "+getHeight());
  4.                         for(Ball ball:balls){//checks if it's in the area of box
  5.                         if(ball.getX()>(getWidth()/2-70) && ball.getX()<(getWidth()/2) && ball.getY()<(getHeight()/2+70) && (ball.getY()>getHeight()/2-70)){
  6.                                 Log.i(TAG,"Locked");
  7.                                 //drink.setDrawable(false); <-- without this everything works great
  8.                                 //invalidate();
  9.                                 //break;
  10.                         }
  11.                         }
  12.                      break;
Parsed in 0.011 seconds, using GeSHi

Any ideas why this is happening?

PS. Excuse me for not giving the complete source code,i know it would be many many times easier for you to help me but because it's for my thesis i don't want yet to give it and i've changed it a little(or more)...After the presentation and hopefully finished with this i'll give the full source code for a complete Android App.
Thank you!
Posts: 3
Joined: Sat Jan 22, 2011 11:47 am


Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 2 guests