0

i have a problem with moving bitmap using matrix

    case MotionEvent.ACTION_MOVE:
        if (!ScaleDetector.isInProgress()) {

            speedx = (event.getX() - downCorx);
            speedy = (event.getY() - downCory);

            matrix.postTranslate(speedx, speedy);
            Log.e("000", speedx + "| " + speedy + "! ");

        }
        this.invalidate();
        break;

that code works but it Accelerates the bitmap speed!

rest of the codes:

 protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(finalBitmap, matrix, null);

    }



public void setBitmap(Bitmap bitmap) {
    finalBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    myCanvas = new Canvas(finalBitmap);
    myCanvas.drawBitmap(bitmap, 0, 0, null);
}

is there any better way to do this?

MREZA
  • 33
  • 6
  • are your variables `downCorx` and `downCory` updated when processing the `ACTION_MOVE` event? if these variables are only set when processing the `ACTION_DOWN` event and the `x` and `y` positions increase while moving, then this will result in increasing values for `speedx` and `speedy` every time the `x` and `y` positions increase. – aschattney Oct 17 '16 at 10:21
  • @aschattney yes they only update when action down happens, what you suggest? – MREZA Oct 17 '16 at 10:27
  • change `postTranslate` to `setTranslate` – pskink Oct 17 '16 at 10:30
  • @pskink it worked but changed my matrix scale to default – MREZA Oct 17 '16 at 10:44
  • so use `dx` and `dy` when calling `postTranslate`, see [here](http://stackoverflow.com/a/21657145/2252830) on how to do that right way – pskink Oct 17 '16 at 10:47

1 Answers1

1
    case MotionEvent.ACTION_MOVE:
    if (!ScaleDetector.isInProgress()) {

        speedx = (event.getX() - downCorx);
        speedy = (event.getY() - downCory);

        // update your current position. So when ACTION_MOVE is triggered again, you actually calculate only the speed between the current and last event of ACTION_MOVE
        downCorx = event.getX();
        downCory = event.getY();

        matrix.postTranslate(speedx, speedy);
        Log.e("000", speedx + "| " + speedy + "! ");

    }
    this.invalidate();
    break;
aschattney
  • 329
  • 4
  • 6