I'm trying to make a media player for the Android platform and one of the features I'm trying to add is the ability to drag and pinch-zoom pictures.
The problem I'm having is I copied this code from "Hello, Android" ed. 3:
@Override
public boolean onTouch(View v, MotionEvent event) {
ImageView view = (ImageView) v;
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
}
else if (mode == ZOOM) {
float newDist = spacing(event);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
}
view.setImageMatrix(matrix);
This code snippet does exactly what I want, but I need the pure X,Y coordinates and the width/height of the picture instead of the matrix.
Does anyone know how to change the matrix to X, Y coordinates and the width and height?
Turns out the solution is simple:
float[] values = new float[9];
matrix.getValues(values);
globalX = values[Matrix.MTRANS_X];
globalY = values[Matrix.MTRANS_Y];
width = values[Matrix.MSCALE_X]*imageWidth;
height = values[Matrix.MSCALE_Y]*imageHeight;
thanks, i've been looking all night to find this, only one thing: in my example i had to use:
float height = matrixValues[4]*((ImageView)currentView).getDrawable().getIntrinsicHeight();
to get the correct height.
i wrote some methods which may help some users (thanks a lot for the code above!!!):
private void logImageViewMatrixInfos(Matrix matrix, ImageView imageView) {
float[] values = new float[9];
matrix.getValues(values);
float globalX = values[2];
float globalY = values[5];
float width = values[0]* imageView.getWidth();
float height = values[4] * imageView.getHeight();
Log.i("Log value", "Image Details: xPos: " + globalX + " yPos: " + globalY + "\nwidth: " + width + " height: " + height);
}
private float getXValueFromMatrix(Matrix matrix) {
float[] values = new float[9];
matrix.getValues(values);
float globalX = values[2];
return globalX;
}
private float getYValueFromMatrix(Matrix matrix) {
float[] values = new float[9];
matrix.getValues(values);
float globalY = values[5];
return globalY;
}
private float getWidthFromMatrix(Matrix matrix, ImageView imageview) {
float[] values = new float[9];
matrix.getValues(values);
float width = values[0]* imageview.getWidth();
return width;
}
private float getHeightFromMatrix(Matrix matrix, ImageView imageview) {
float[] values = new float[9];
matrix.getValues(values);
float height = values[4] * imageview.getHeight();
return height;
}
:=)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With