Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

android - Stick matrix to specific angle

I'm using matrix to zoom, rotate and scale image and here's my code of onTouch() as given below:

public boolean onTouch(@NonNull View v, @NonNull MotionEvent event) {
    ImageView view = (ImageView) v;
    // make the image scalable as a matrix
    float scale;
    view.setScaleType(ImageView.ScaleType.MATRIX);
    // Handle touch events here...
    switch (event.getAction() & MotionEvent.ACTION_MASK) {

        case MotionEvent.ACTION_DOWN: //first finger down only
            matrix.set(view.getImageMatrix());
            savedMatrix.set(matrix);
            start.set(event.getX(), event.getY());
            Log.d(TAG, "mode=DRAG");
            mode = DRAG;
            lastEvent = null;
            break;
        case MotionEvent.ACTION_UP: //first finger lifted
        case MotionEvent.ACTION_POINTER_UP: //second finger lifted
            mode = NONE;
            lastEvent = null;
            Log.d(TAG, "mode=NONE");
            break;
        case MotionEvent.ACTION_POINTER_DOWN: //second finger down
            oldDist = getSpacing(event); // calculates the distance between two points where user touched.
            Log.d(TAG, "oldDist=" + oldDist);
            // minimal distance between both the fingers
            if (oldDist > 10f) {
                savedMatrix.set(matrix);
                getMidPoint(mid, event); // sets the mid-point of the straight line between two points where user touched.
                mode = ZOOM;
                Log.d(TAG, "mode=ZOOM");
            }
            lastEvent = new float[4];
            lastEvent[0] = event.getX(1);
            lastEvent[1] = event.getX(0);
            lastEvent[2] = event.getY(1);
            lastEvent[3] = event.getY(0);
            d = getRotation(event);
            break;

        case MotionEvent.ACTION_MOVE:
            if (mode == DRAG) { //movement of first finger
                matrix.set(savedMatrix);
                if (view.getLeft() >= -392) {
                    matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
                }
            } else if (mode == ZOOM && event.getPointerCount() == 2) { //pinch zooming
                float newDist = getSpacing(event);
                matrix.set(savedMatrix);
                Log.d(TAG, "newDist=" + newDist);
                if (newDist > 10f) {
                    matrix.set(savedMatrix);
                    scale = newDist / oldDist; //thinking I need to play around with this value to limit it**
                    matrix.postScale(scale, scale, mid.x, mid.y);
                }
                if (lastEvent != null) {
                    newRot = getRotation(event);
                    float r = newRot - d;
                    matrix.postRotate(r, view.getMeasuredWidth() / 2f,
                            view.getMeasuredHeight() / 2f);
                }

                float rotation = getOriginalRotation();
                if (rotation > -7 && rotation < 7) {
                    rotateImageToZeroAngle();
                }

            }
            break;
    }

    // Perform the transformation
    view.setImageMatrix(matrix);

    return true; // indicate event was handled
}

Now, i want to stick matrix rotation to 0 when rotation is between -3 to 3 and for that i added method getOriginalRotation() to get rotation and added one more method to set rotation rotateImageToZeroAngle() which is as given below:

private void rotateImageToZeroAngle() {
    Matrix curMatrix = mainImage.getImageMatrix();
    float[] v = new float[9];
    matrix.getValues(v);
    curMatrix.setValues(v);

    mainImage.setScaleType(ImageView.ScaleType.MATRIX);
    curMatrix.setRotate(0);
    mainImage.setImageMatrix(curMatrix);
}

Now, i don't know whats the problem but this isn't working.By the way when i change scaleType to FIT_CENTER it will work but between -3 to 3 degree image will be resized to FIT in given area and in Matrix scaleType, it will not working. I need some help and any help will be appreciated,Thank you in advanced!

question from:https://stackoverflow.com/questions/65952983/stick-matrix-to-specific-angle

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

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...