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]}
Solution:
if (firstTime) {
eventXY = new float[] {eventX, eventY};
getImageMatrix().invert(invertMatrix);
invertMatrix.mapPoints(eventXY);
firstTime = false;
}