矩阵如何获取Matrix中的位图中心点

时间:2016-04-05 08:58:07

标签: android matrix

我是android Matrix新手。获取矩阵的中心点(X和Y)时出现问题,该矩阵被旋转,缩放和平移。 虽然我能够得到位图的x和y,但是在使用postrotate旋转位图后我遇到了问题。

下面是我获取位图的X和Y的代码,但是在旋转矩阵时这会改变。任何帮助人员。

@Override
public boolean onTouch(View view, MotionEvent event) {

    float[] point; 
    float bitmapX = 0; 
    float bitmapY = 0;
    if(targetSelected != null){
        point = new float[] {
                targetSelected.bitmapX, 
                targetSelected.bitmapY + (targetSelected.bitmap.getHeight() * targetSelected.getScaleMatrixFactor())};
        targetSelected.matrix.mapPoints(point);
        bitmapX = point[0];
        bitmapY = point[1];
    }
 if (event.getAction() == MotionEvent.ACTION_MOVE) {
    targetSelected.bX = bitmapX;
    targetSelected.bY = bitmapY;
 }
}


 //The targetSelected Layer
public void rotate90(){
    rotation = rotation + 90;    
    Matrix m = new Matrix();
    m.postRotate(rotation, bitmap.getWidth() /2 , bitmap.getHeight() /2 );
    m.postTranslate(bX+(bitmap.getWidth() * getScaleMatrixFactor() / 2),
                    bY-(bitmap.getHeight() * getScaleMatrixFactor() / 2));

    matrix.reset(); // reset old matrix     
    matrix.set(m); // apply new matrix to old matrix

}



/**
 * 
 * @return calculate real scale factor of the matrix.
 */
public float getScaleMatrixFactor(){
    float[] values = new float[9];
    matrix.getValues(values);
    float scalex = values[Matrix.MSCALE_X];
    float skewy = values[Matrix.MSKEW_Y];
    float rScale = (float) Math.sqrt(scalex * scalex + skewy * skewy);
    return rScale;
}

0 个答案:

没有答案