0

i want to move and scale a bitmap on canvas on android in an scaled custom imageview. This works fine with getImageMatrix().invert(invertMatrix) overriding the onTouchEvent:

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (!isStringEmpty(getTextForCanvas())) {
        dumpEvent(event);
        eventX = event.getX();
        eventY = event.getY();
        eventXY = new float[] {eventX, eventY};
        getImageMatrix().invert(invertMatrix);
        invertMatrix.mapPoints(eventXY);
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                savedMatrix.set(invertMatrix);
                //start.set(event.getRawX(), event.getRawY());
                start.set(eventX, eventY);
                mode = Constants.DRAG;
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                oldDist = spacing(event);
                if (oldDist > 10f) {
                    savedMatrix.set(invertMatrix);
                    midPoint(mid, event);
                    mode = Constants.ZOOM;
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:
                mode = Constants.NONE;
                break;
            case MotionEvent.ACTION_MOVE:
                if (mode == Constants.DRAG) {
                    invertMatrix.set(savedMatrix);
                    invertMatrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
                } else if (mode == Constants.ZOOM) {
                    float newDist = spacing(event);
                    if (newDist > 10f) {
                        invertMatrix.set(savedMatrix);
                        scale = newDist / oldDist;
                        invertMatrix.postScale(scale, scale, mid.x, mid.y);
                    }
                }
                break;
        }
    }

    setImageMatrix(invertMatrix);
    return true;
}

The onDraw Method:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawBitmap(mImageBitmap, 0, 0, mImagePaint);
    canvas.drawBitmap(canvasBitmap, invertMatrix, null);
}

But the last invertMatrix looses the data and so at least the bitmap is drawn at the beginning (Android 4.4) or is gone (Android 4.2). here the log of invertMatrix:

I/System.out﹕ Matrix{[1.0, 0.0, 421.0][0.0, 1.0, 650.47687][0.0, 0.0, 1.0]}
I/System.out﹕ Matrix{[1.0, 0.0, 421.0][0.0, 1.0, 652.96216][0.0, 0.0, 1.0]}
I/System.out﹕ Matrix{[1.0, 0.0, 421.0][0.0, 1.0, 654.5][0.0, 0.0, 1.0]}
I/System.out﹕ Matrix{[1.0, 0.0, 421.0][0.0, 1.0, 656.0872][0.0, 0.0, 1.0]}
I/System.out﹕ Matrix{[1.0, 0.0, 0.0][0.0, 1.0, 0.0][0.0, 0.0, 1.0]}

Can anyone help me? Perhaps i have to store the matrix in case of invalidation? How?

Update: Log on Android 4.2: (Matrix coords are inverted):

I/System.out﹕ Matrix{[1.0, -0.0, 101.778534][-0.0, 1.0, 225.0][0.0, 0.0, 1.0]}
I/System.out﹕ Matrix{[1.0, 0.0, -101.778534][0.0, 1.0, -225.0][0.0, 0.0, 1.0]}
  • what you want to echieve? – pskink May 12 '14 at 17:24
  • you could probably read my answer here http://stackoverflow.com/questions/21633545/android-imageview-scaling-and-translating-issue – pskink May 12 '14 at 18:04
  • Thanks for your answer. i do not want to achieve anythink. i only want to keep the matrix. As you can see in the logs the inverse matrix lost its data at last. and i do not know why. Perhaps this problem is similar to http://stackoverflow.com/questions/20431374/bug-with-android-4-3-imageview-method-getimagematrix/20476958#20476958, but my app works neither on 4.2 nor 4.4. – user3626019 May 13 '14 at 12:52
  • if you want to move/scale/rotate the Bitmap see my answer here: http://stackoverflow.com/questions/21633545/android-imageview-scaling-and-translating-issue – pskink May 13 '14 at 13:23
  • to move/scale/rotage the bitmap with an matrix is not the problem. the problem is that the matrix looses the data at the end. – user3626019 May 13 '14 at 13:50
  • it cannot loose the data by itself, something or someone (yes you) sets its data, there are no miracles ... – pskink May 13 '14 at 13:56
  • As you see in the logs the matrix data is set to 0.0 or inverted at last time and i don't know why.... – user3626019 May 14 '14 at 08:31
  • btw i dont see setScaleType(MATRIX) – pskink May 14 '14 at 08:38

1 Answers1

0

Solution:

        if (firstTime) {
            eventXY = new float[] {eventX, eventY};
            getImageMatrix().invert(invertMatrix);
            invertMatrix.mapPoints(eventXY);
            firstTime = false;
        }