petrbel
petrbel

Reputation: 2538

Canvas ingame scaling (determinating the real touch coord)

I'm newbee in android-game developing and I would appreciate your help. I'd like to paint some graphics on the canvas which is supposed to be way larger than screen. So some scaling and moving would be great. I've read some questions but they usually answers only some details - not the whole concept.

I've tried use Camera

cam.save();
cam.translate(0f, 0f, -8f);
cam.applyToCanvas(canvas);
cam.restore();

This scales perfectly, but I am unable to decode the real touch coord.

I don't want to use openGL (it's overkill and also I'd like to start with sth simple)

Anyway, I tried canvas.scale(int, int) as well, but didn't work. I believe the Camera is the right way, but I'm lost.

So the question is: how to determinate real coord? Furthermore, It would be nice if some could share a piece of tutorial etc. or some concept of using canvas transformation. (Or maybe there more appropriate ways how to solve it)

Thanks in advance

Upvotes: 0

Views: 127

Answers (1)

ShreeshaDas
ShreeshaDas

Reputation: 2042

you have to override the onTouchEvent

@Override
     public boolean onTouchEvent(MotionEvent event) {
         float x = event.getX();
         float y = event.getY();

         switch (event.getAction()) {
             case MotionEvent.ACTION_DOWN:
                 touch_start(x, y);
                 invalidate();
                 break;
             case MotionEvent.ACTION_MOVE:
                 touch_move(x, y);
                 invalidate();
                 break;
             case MotionEvent.ACTION_UP:
                 touch_up();
                 invalidate();
                 break;
         }
         return true;
     }



 private void touch_start(float x, float y) {
        mPath.reset();
        mPath.moveTo(x, y);
        mX = x;
        mY = y;
    }

     private void touch_move(float x, float y) {
         float dx = Math.abs(x - mX);
         float dy = Math.abs(y - mY);
         if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
             mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
             mX = x;
             mY = y;
         }
     }
     private void touch_up() {
         mPath.lineTo(mX, mY);
         // commit the path to our offscreen
         mCanvas.drawPath(mPath, mPaint);
         // kill this so we don't double draw
         mPath.reset();
     }

Upvotes: 1

Related Questions