Home  >  Q&A  >  body text

java - 经过matrix平移缩放旋转的图片,如何动态求出图片中心点坐标

项目中遇到这样的需求,需要动态求出正在编辑的图片的中心点坐标,编辑操作共有三种,平移,缩放和旋转.
其中平移缩放可以按照如下代码正确求出图片左上角和右下角坐标,但是旋转不可以.

 /**
     *
     * @param matrix imageView的矩阵对象
     * @param imageView 图片对象
     * @return
     */
 public float[] getLeftTop(Matrix matrix, GestureCropImageView imageView) {
        Rect rectTemp = imageView.getDrawable().getBounds();
        float[] values = new float[9];
        matrix.getValues(values);
        //左上角X坐标
        mLeftX = values[2];
        //左上角Y坐标
        mLeftY = values[5];

        //如果旋转的话
        if (mState == ROTATE_STATE) {

            //x0= (x - rx0)*cos(a) - (y - ry0)*sin(a) + rx0 ;
            //
            //y0= (x - rx0)*sin(a) + (y - ry0)*cos(a) + ry0 ;
            //右下角X坐标
            mRightX = (float) ((mRightX - mMidPntX) * Math.cos(mAngle / 180 * Math.PI) - (mRightY - mMidPntY) * Math.sin(mAngle / 180 * Math.PI) + mRightX);
            //右下角Y坐标
            mRightY = (float) ((mRightX - mMidPntX) * Math.sin(mAngle / 180 * Math.PI) + (mRightY - mMidPntY) * Math.cos(mAngle / 180 * Math.PI) + mRightY);


            Log.i(TAG, "宽度 :" + rectTemp.width() * values[0]);
            Log.i(TAG, "长度 :" + rectTemp.height() * values[4]);

        } else {
            //右下角X坐标
            mRightX = values[2] + rectTemp.width() * values[0];
            //右下角Y坐标
            mRightY = values[5] + rectTemp.height() * values[4];
        }

        float[] arr = {mLeftX, mLeftY, mRightX, mRightY};

        return arr;
    }

2.现在已知的条件有,图片旋转的角度,图片按某点旋转的点坐标,左上角的坐标,如何才能求出右下角的坐标?

这是我的一种思路,但是出来的结果不对.

ringa_leeringa_lee2711 days ago529

reply all(3)I'll reply

  • 巴扎黑

    巴扎黑2017-04-18 09:49:25

    The problem has been solved, the method is as follows:

        /**
         * @param matrix 变化后的矩阵
         * @param rectF 矩形对象
         */
        public Float[] getCenter(Matrix matrix,RectF rectF){
            matrix.mapRect(rectF);
            //其实在此处就可以获得中心! wtf
            float centerX = rectF.centerX();
            float centerY = rectF.centerY();
            return new Float[]{centerX,centerY};
        }

    reply
    0
  • 怪我咯

    怪我咯2017-04-18 09:49:25

    There is a simple method, you can try it, use the transform method of the MotionEvent class provided by Android
    The specific steps are to first generate a MotionEvent object based on the center coordinates of the image before rotation, then call the transform method, pass the Matrix, and then take it out The corresponding transformed x, y should be the coordinates you want

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 09:49:25

    You can first get the coordinates of the center point before transformation, and then use the Matrix.mappoints method after setting the matrix to get the coordinates of the center point after transformation

    reply
    0
  • Cancelreply