0

我正在使用MotionEventMatrix拖动我的位图,但位图被“翻译”到附近的区域(大多数时候在相反的方向)。我希望能够使用矩阵翻译来拖动它。这是我的代码。

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);
        }    

}
4

1 回答 1

0
  1. 您的onTouch()方法应该返回true能够从 MotionEvent 连续获取值。
  2. isDraggable当出现拖动图像的尝试时,您立即将播放器的属性设置为 false。之后,你每次检查,如果它是真的,但它不会是。我不知道,这背后的逻辑是什么,但试试别的!
于 2012-08-28T09:03:05.267 回答