0

I'm scaling the canvas, when I move it, it is going out of the screen.

Screenshot of before translating(i.e., when canvas.onDraw() executes for the first time)

BEFORE TRANSLATE

After Move:

enter image description here

I need some condition to say that don't go beyond this point(x-axis - 20).

        canvas.save();
        int maxX, minX;

        minX = /* some condition */;
        maxX = /* some condition */;
        if (mPosX > maxX)
            mPosX = maxX;

        if (mPosX < minX)
            mPosX = minX;

        canvas.translate(mPosX, 0);
        canvas.scale(xScaleFactor, yScaleFactor,
                px / xScaleFactor + left, py / yScaleFactor + canvas.getClipBounds().top);

        paint.setColor(ContextCompat.getColor(getContext(), R.color.green));
        canvas.drawRect(20, 20, width, height, paint);

While moving:

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        mScaleDetector.onTouchEvent(ev);
        int eid = ev.getAction();
        switch (eid & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                px = ev.getX();
                py = ev.getY();

                mLastTouchX = px;
                mActivePointerId = ev.getPointerId(0);
                break;

            case MotionEvent.ACTION_MOVE:
                if (eid == MotionEvent.ACTION_POINTER_UP || eid == MotionEvent.ACTION_POINTER_DOWN) {
                    return false;
                }
                pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)
                        >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
                final float x = ev.getX(pointerIndex);

                final float dx = x - mLastTouchX;
                mPosX += dx; 
                invalidate();

                mLastTouchX = x;
                break;
                  .......

Kindly help to get some condition which will restrict the rectangle from moving away from the screen. So that the left white space is not created. Every time the canvas invalidates, rectangle should start from position 20 of x-axis.

EDIT:

        private Matrix m;
        m = new Matrix();
        m.mapRect(new RectF(20, 0, mPosX, 0));

        // in onDraw()
        canvas.save(); 
        m.setTranslate(mPosX, 0);
        m.setScale(xScaleFactor, yScaleFactor,
                (px + canvas.getClipBounds().left)/ xScaleFactor, (py + canvas.getClipBounds().top)/ yScaleFactor);

        canvas.concat(m);

        paint.setColor(ContextCompat.getColor(getContext(), R.color.green));
        canvas.drawRect(20, 20, width, height, paint);

still it is going out

Prabs
  • 4,923
  • 6
  • 38
  • 59
  • in such cases it is much easier to work with a `android.graphics.Matrix` api – pskink Apr 10 '17 at 06:59
  • You mean, use matrix to control the translate? @pskink – Prabs Apr 10 '17 at 07:00
  • yes, translate and scale the matrix – pskink Apr 10 '17 at 07:00
  • okay. I'm checking that. could you suggest any accurate link if possible – Prabs Apr 10 '17 at 07:02
  • 1
    sure: https://developer.android.com/reference/android/graphics/Matrix.html – pskink Apr 10 '17 at 07:02
  • @pskink I read the documentation. Do I need to save the points to matrix and in matrix.postTranslate retrieve the points? How do I proceed. Any suggestions please – Prabs Apr 10 '17 at 07:30
  • 1
    see Canvas#concat – pskink Apr 10 '17 at 07:36
  • @pskink could you post the same as an answer with little description and and some code – Prabs Apr 10 '17 at 07:43
  • 1
    see [here](http://stackoverflow.com/a/21657145/2252830) for some `Matrix` related code – pskink Apr 10 '17 at 07:56
  • @pskink I've tried [this](https://github.com/pskink/android-gesture-detectors), the Earth is zooming, moving and rotating. But how do I restrict it to move within the visible area. I mean within the screen. It shouldn't go out of the screen – Prabs Apr 10 '17 at 09:20
  • use `Matrix#mapRect` and check if mapped rect is outside your screen, if it is modify your matrix accordingly – pskink Apr 10 '17 at 09:24
  • @pskink once check the question 'EDIT' part – Prabs Apr 10 '17 at 09:49
  • @pskink `m.mapRect(new RectF(30, 0, mPosX, 0));` `canvas.translate(mPosX, 0);` `canvas.setMatrix(m);` is adding the limit to transform. But I can't move to the right end of rectangle. What should be 3rd parameter in `mapRect` – Prabs Apr 10 '17 at 10:46
  • @pskink whatsoever the value of right parameter in `RectF` it is not allowing to scroll to the right end. canvas.concatMatrix doesn't have any effect on it. So I'm using `setMatrix`. got stuck here. :( – Prabs Apr 10 '17 at 11:18
  • no, `setMatrix` is wrong here, you should use `concat` – pskink Apr 10 '17 at 11:20
  • `concat` isn't stopping the canvas from moving away – Prabs Apr 10 '17 at 11:24
  • because your matrix is wrong – pskink Apr 10 '17 at 11:25
  • m.mapRect(new RectF(20, 0, mPosX, 0)); not like this? – Prabs Apr 10 '17 at 11:26
  • @pskink okay, I guess I'm little closer. I've added `transform` and `scale` to matrix instead of canvas. And concat the matrix to canvas. still it is not allowing complete right side scrolling. I've added the code in Question – Prabs Apr 10 '17 at 11:43
  • @pskink Failed to solve it. what should be the matrix? And could you check [this](http://stackoverflow.com/questions/43337835/can-i-access-canvas-elements-separately) once – Prabs Apr 11 '17 at 07:29

0 Answers0