Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

android - Getting coordinates and width/height from a matrix

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...