Very nice and helpful sample,thanks!
I have a very strange problem with a simple modification on the code!I wanted to have the overall control of the let's say app in the activity so first i take all the code from the DrawView.class and paste it into the activity class and delete the DrawView.class file since it didn't need!everything works fine!!!
Then to seperate a little bit more the code i took all the onTouchevent code and paste it in the activity class(see the code below)!
And the strange to all this is that the onTouchevent is working(all actions up move down is being captured) but the if() statement which is checking if we are in the ball bounds doesn't!!!To be more specific it's like it can't read the X,Y vars!
When i use this(in onTouchEvent in ACTION_DOWN, the values i use here isn't the values that i was using when i was testing it i had calculate what values should i use)
Using java Syntax Highlighting
if (20 > ball.getX() && 20< ball.getX()+50 && 30> ball.getY() && 30 < ball.getY()+50)
Parsed in 0.012 seconds, using
GeSHi 1.0.8.4
it worked!(i mean i was getting the log msg)
but when i used the original if() it didn't!!(no log msg)
code:
Using java Syntax Highlighting
public class dragndrop extends Activity {
/** Called when the activity is first created. */
private ColorBall[] colorballs = new ColorBall[3]; // array that holds the balls
private static final String TAG="MyTAG";
DrawView myView;
private int balID = 0; // variable to know what ball is being dragged
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Point point1 = new Point();
point1.x = 50;
point1.y = 20;
Point point2 = new Point();
point2.x = 100;
point2.y = 20;
Point point3 = new Point();
point3.x = 150;
point3.y = 20;
// declare each ball with the ColorBall class
colorballs[0] = new ColorBall(this,R.drawable.bol_groen, point1);
colorballs[1] = new ColorBall(this,R.drawable.bol_rood, point2);
colorballs[2] = new ColorBall(this,R.drawable.bol_blauw, point3);
myView = new DrawView(this);
setContentView(myView);
}
// events when touching the screen
public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();
int X = (int)event.getX();
int Y = (int)event.getY();
switch (eventaction ) {
case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on a ball
balID = 0;
for (ColorBall ball : colorballs) {
Log.d(TAG,"inside action down inside for coords:"+X+" coords: "+Y);
Log.d(TAG,"ball coords:"+ball.getX()+" coords: "+ball.getY());
if (X > ball.getX() && X < ball.getX()+50 && Y > ball.getY() && Y < ball.getY()+50){
Log.d(TAG,"inside ball coords!!!!!!!!!!!!!!!!!!!!!!!!:"+ball.getX()+" coords: "+ball.getY());
balID = ball.getID();
break;
}
}
break;
case MotionEvent.ACTION_MOVE: // touch drag with the ball
// move the balls the same as the finger
if (balID > 0) {
colorballs[balID-1].setX(X-25);
colorballs[balID-1].setY(Y-25);
}
break;
case MotionEvent.ACTION_UP:
// touch drop - just do things here after dropping
break;
}
// redraw the canvas
myView.invalidate();
return true;
}
public class DrawView extends View {
public DrawView(Context context) {
super(context);
setFocusable(true); //necessary for getting the touch events
// setting the start point for the balls
}
// the method that draws the balls
@Override protected void onDraw(Canvas canvas) {
//canvas.drawColor(0xFFCCCCCC); //if you want another background color
//draw the balls on the canvas
for (ColorBall ball : colorballs) {
canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), null);
}
}
}
}
Parsed in 0.015 seconds, using
GeSHi 1.0.8.4
So i came up that although they are two simple vars(X,Y) which i can take their values and print them i can't use them inside the if statement because the ontouchevent should always be inside a view.(i know it doesn't sound right,can't we have touch events which will be handled from the activity?)
Any suggestions why this is happening?
thanks!