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;
  8.        
  9.         public Ball(Context context, int drawable, Point point, int c) {
  10.  
  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;
  18.  
  19.         }
  20.  
  21.         //setters geters go here etc..
  22.  
  23.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4

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
  3.                
  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);//
  7.                
  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);
  11.                
  12.                 super.onSizeChanged(w, h, oldw, oldh);
  13.             }
  14.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


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) {
  2.                        
  3.                 canvas.drawBitmap(box.getBitmap(), box.getX(), box.getY(),null);
  4.                
  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());
  10.  
  11.                                 canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), null);
  12.                         }
  13.                   }
  14.  
  15.             }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4

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 debugging....it 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());
  3.  
  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;
  13.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4

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!
mod9
Freshman
Freshman
 
Posts: 3
Joined: Sat Jan 22, 2011 11:47 am

Top

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

Who is online

Users browsing this forum: No registered users and 6 guests