Android我希望在画布缩放和翻译时获得不同的x,y位置

时间:2017-04-28 05:09:42

标签: android canvas

我希望在缩放和翻译画布时获得当前的x,y坐标,我该如何实现?

当画布缩放并且在没有获得当前x y位置后翻译并且我得到不同的x,y坐标。

以下是我的画布代码

                canvas.save();
                super.onDraw(canvas);
                canvas.setMatrix(getImageMatrix());
                matrix1.set(getImageMatrix());
                tempscalx = getscalx();
                tempscaly = getscaly();
                this.f1658w[0] = 0.0f;
                this.f1658w[1] = 0.0f;
                this.f1658w[2] = (float) getWidth();
                this.f1658w[3] = (float) getHeight();
                getImageMatrix().mapPoints(this.f1658w);
                drawCanvas.drawColor(65536, PorterDuff.Mode.CLEAR);
                if (replaceboolean_for_shape) {
                    drawCanvas.drawBitmap(original, 0, 0, null);
                    drawCanvas.drawBitmap(framebt, matrix, null);
                    drawCanvas.drawBitmap(maskbt, matrix, maskpaint);
                    canvas.drawBitmap(black, 0.0f, 0.0f, null);
                } else {
                    drawCanvas.drawBitmap(black, 0, 0, null);
                    drawCanvas.drawBitmap(framebt, matrix, null);
                    drawCanvas.drawBitmap(maskbt, matrix, maskpaint);
                    canvas.drawBitmap(original, 0.0f, 0.0f, null);
                }
                canvas.drawBitmap(canvasBitmap, 0.0f, 0.0f, null);
                this.setDrawingCacheEnabled(true);
                color_priview_bitmap = this.getDrawingCache(true);
                canvas.restore();

以下是我的 OnTouchEvent()代码。

switch (event.getAction() & MotionEvent.ACTION_MASK) {
                        case MotionEvent.ACTION_DOWN: //first finger down only
                            savedMatrix.set(matrix);
                            **//Here in below Line i cant get proper [getX & getY]**
                            start.set(event.getX(), event.getY());
                            mode = DRAG;
                            break;
                        case MotionEvent.ACTION_CANCEL:
                            break;
                        case MotionEvent.ACTION_UP:

                            break;
                        case MotionEvent.ACTION_POINTER_UP: //second finger lifted
                            mode = NONE;
                            lastEvent = null;
                            break;
                        case MotionEvent.ACTION_POINTER_DOWN: //second finger down
                            oldDist = spacing(event);
                            if (oldDist > 5f) {
                                savedMatrix.set(matrix);
                                midPoint(mid, event);
                                mode = ZOOM;
                            }
                            lastEvent = new float[4];
                            lastEvent[0] = event.getX(0);
                            lastEvent[1] = event.getX(1);
                            lastEvent[2] = event.getY(0);
                            lastEvent[3] = event.getY(1);
                            d = rotation(event);
                            break;
                        case MotionEvent.ACTION_MOVE:
                            if (mode == DRAG) { //movement of first finger
                                matrix.set(savedMatrix);
                                matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
                            } else if (mode == ZOOM) {
                                float newDist = spacing(event);
                                matrix.set(savedMatrix);
                                if (newDist > 10f) {
                                    scale = newDist / oldDist;
                                    matrix.postScale(scale, scale, mid.x, mid.y);
                                }
                                if (lastEvent != null) {
                                    newRot = rotation(event);
                                    float r = newRot - d;
                                    matrix.postRotate(r, getMeasuredWidth() / 2, getMeasuredHeight() / 2);
                                }
                            }
                            break;
                    }

1 个答案:

答案 0 :(得分:2)

使用此方法在画布触点上获取坐标

final float[] getPointerCoords(ImageView view, MotionEvent e){
    final int index = e.getActionIndex();
    final float[] coords = new float[] { e.getX(index), e.getY(index) };
    Matrix matrix = new Matrix();
    view.getImageMatrix().invert(matrix);
    matrix.postTranslate(view.getScrollX(), view.getScrollY());
    matrix.mapPoints(coords);
    return coords;
}