0

I am using MotionEvent and Matrix to drag my bitmap but instead the bitmap gets "translated" to a nearby region (most of the times in the opposite direction of press). I want to be able to drag it using Matrix translations. Here's my code.

switch(action){
        case MotionEvent.ACTION_DOWN: {
            final float touchX = event.getX();
            final float  touchY = event.getY();
          //check to see if the user is pressing  at most 80 pixels far from the bitmap 
            if(((Math.abs(touchX - player.prevX)<=80) && ((Math.abs(touchY - player.prevY)<=80)))){
                 //if so then proceed to drag it to a new location
                player.isDraggable = true;
            }
            break;
        }
        case MotionEvent.ACTION_MOVE: {
            if(player.isDraggable){
                player.x = event.getX();
                player.y = event.getY();                    
                float dx = player.x - player.prevX, dy = player.y - player.prevY;
                player.matrix.postTranslate(dx, dy);
                player.prevX = player.x;
                player.prevY = player.y;
                player.isDraggable = false; 
        }
            break;
        }
    }
public void draw(Canvas canvas) {       
        canvas.drawBitmap(background, 0, 0, null);
        canvas.drawBitmap(player.bmp, player.matrix, null);
       for(int i=0;i<particles.size();i++){
           particles.get(i).drawShower(canvas);
        }    

}
jmishra
  • 2,086
  • 2
  • 24
  • 38

1 Answers1

0
  1. Your onTouch() method should return true to be able to get continuously values from the MotionEvent.
  2. You set the isDraggable attribute of your player immediately to false, when an attempt to drag the image appears. After that, you check each time, if it is true, but it wont be. I don't know, what is the logic behind that, but try something else!
Balázs Édes
  • 13,452
  • 6
  • 54
  • 89
  • Thanks for your answer. The problem wasn't the 1 but actually 2. You were right about that. Now I call the `player.isDraggable=false` by a function call from a different class (instead of setting it to false right away) so that now it gets dragged properly – jmishra Aug 30 '12 at 18:59