在android中放大/缩小或拖动后获取Canvas坐标

发布于 2024-12-06 05:59:09 字数 3909 浏览 0 评论 0原文

我正在开发一个应用程序,在其中粘贴图像,在画布上绘图和绘画。该应用程序还可以放大/缩小画布或将其拖动到不同的位置。 我的问题是:缩放或拖动画布后无法获得正确的画布坐标。我想在缩放或拖动画布后绘制手指画,但无法检索我触摸过的正确位置..:( 我也是新蜜蜂。这是代码。

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    canvas.save();
    //canvas.translate(mPosX, mPosY);
    canvas.scale(mScaleFactor, mScaleFactor, super.getWidth() * 0.5f,
            super.getHeight() * 0.5f);
    mIcon.draw(canvas);
    for (Path path : listPath) {
        canvas.drawPath(path, paint);
    }
    canvas.restore();
}

public TouchExampleView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    // Let the ScaleGestureDetector inspect all events.
    mScaleDetector.onTouchEvent(ev);

    final int action = ev.getAction();
    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();

        mLastTouchX = x;
        mLastTouchY = y;
        mActivePointerId = ev.getPointerId(0);
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        final int pointerIndex = ev.findPointerIndex(mActivePointerId);
        final float x = ev.getX(pointerIndex);
        final float y = ev.getY(pointerIndex);

        // Only move if the ScaleGestureDetector isn't processing a gesture.
        if (!mScaleDetector.isInProgress()) {
            final float dx = x - mLastTouchX;
            final float dy = y - mLastTouchY;

            mPosX += dx;
            mPosY += dy;

            invalidate();
        }

        mLastTouchX = x;
        mLastTouchY = y;

        break;
    }

    case MotionEvent.ACTION_UP: {
        mActivePointerId = INVALID_POINTER_ID;
        break;
    }

    case MotionEvent.ACTION_CANCEL: {
        mActivePointerId = INVALID_POINTER_ID;
        break;
    }

    case MotionEvent.ACTION_POINTER_UP: {
        final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
        final int pointerId = ev.getPointerId(pointerIndex);
        if (pointerId == mActivePointerId) {
            // This was our active pointer going up. Choose a new
            // active pointer and adjust accordingly.
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mLastTouchX = ev.getX(newPointerIndex);
            mLastTouchY = ev.getY(newPointerIndex);
            mActivePointerId = ev.getPointerId(newPointerIndex);
        }
        break;
    }
    }

    float objectNewX,objectNewY;
    if (mScaleFactor >= 1) {
        objectNewX = ev.getX() + (ev.getX() - super.getWidth() * 0.5f) * (mScaleFactor - 1);
        objectNewY = ev.getY() + (ev.getY() - super.getHeight() * 0.5f) * (mScaleFactor - 1);
    } else {
        objectNewX = ev.getX() - (ev.getX() - super.getWidth() * 0.5f) * (1 - mScaleFactor);
        objectNewY = ev.getY() - (ev.getY() - super.getHeight() * 0.5f) * (1 - mScaleFactor);
    }

    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        path = new Path();
        path.moveTo(objectNewX,objectNewY);
        path.lineTo(objectNewX,objectNewY);
    } else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
        path.lineTo(objectNewX,objectNewY);
        listPath.add(path);
    } else if (ev.getAction() == MotionEvent.ACTION_UP) {
        path.lineTo(objectNewX,objectNewY);
        listPath.add(path);
    }

    return true;
}

private class ScaleListener extends
        ScaleGestureDetector.SimpleOnScaleGestureListener {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        mScaleFactor *= detector.getScaleFactor();

        // Don't let the object get too small or too large.
        mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));

        invalidate();
        return true;
    }
}

I'm developing an application in which I'm pasting images, doing drawing and painting on canvas. This app can also Scale up/down the canvas or drag it to different location.
My problem is: I can't get the correct canvas coordinates after scaling or dragging the canvas. I want to draw finger paint after the canvas is scaled or dragged but unable to retrieve the right place where i've touched..:(
Also I'm new bee. Here is the code.

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    canvas.save();
    //canvas.translate(mPosX, mPosY);
    canvas.scale(mScaleFactor, mScaleFactor, super.getWidth() * 0.5f,
            super.getHeight() * 0.5f);
    mIcon.draw(canvas);
    for (Path path : listPath) {
        canvas.drawPath(path, paint);
    }
    canvas.restore();
}

public TouchExampleView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    // Let the ScaleGestureDetector inspect all events.
    mScaleDetector.onTouchEvent(ev);

    final int action = ev.getAction();
    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();

        mLastTouchX = x;
        mLastTouchY = y;
        mActivePointerId = ev.getPointerId(0);
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        final int pointerIndex = ev.findPointerIndex(mActivePointerId);
        final float x = ev.getX(pointerIndex);
        final float y = ev.getY(pointerIndex);

        // Only move if the ScaleGestureDetector isn't processing a gesture.
        if (!mScaleDetector.isInProgress()) {
            final float dx = x - mLastTouchX;
            final float dy = y - mLastTouchY;

            mPosX += dx;
            mPosY += dy;

            invalidate();
        }

        mLastTouchX = x;
        mLastTouchY = y;

        break;
    }

    case MotionEvent.ACTION_UP: {
        mActivePointerId = INVALID_POINTER_ID;
        break;
    }

    case MotionEvent.ACTION_CANCEL: {
        mActivePointerId = INVALID_POINTER_ID;
        break;
    }

    case MotionEvent.ACTION_POINTER_UP: {
        final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
        final int pointerId = ev.getPointerId(pointerIndex);
        if (pointerId == mActivePointerId) {
            // This was our active pointer going up. Choose a new
            // active pointer and adjust accordingly.
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mLastTouchX = ev.getX(newPointerIndex);
            mLastTouchY = ev.getY(newPointerIndex);
            mActivePointerId = ev.getPointerId(newPointerIndex);
        }
        break;
    }
    }

    float objectNewX,objectNewY;
    if (mScaleFactor >= 1) {
        objectNewX = ev.getX() + (ev.getX() - super.getWidth() * 0.5f) * (mScaleFactor - 1);
        objectNewY = ev.getY() + (ev.getY() - super.getHeight() * 0.5f) * (mScaleFactor - 1);
    } else {
        objectNewX = ev.getX() - (ev.getX() - super.getWidth() * 0.5f) * (1 - mScaleFactor);
        objectNewY = ev.getY() - (ev.getY() - super.getHeight() * 0.5f) * (1 - mScaleFactor);
    }

    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        path = new Path();
        path.moveTo(objectNewX,objectNewY);
        path.lineTo(objectNewX,objectNewY);
    } else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
        path.lineTo(objectNewX,objectNewY);
        listPath.add(path);
    } else if (ev.getAction() == MotionEvent.ACTION_UP) {
        path.lineTo(objectNewX,objectNewY);
        listPath.add(path);
    }

    return true;
}

private class ScaleListener extends
        ScaleGestureDetector.SimpleOnScaleGestureListener {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        mScaleFactor *= detector.getScaleFactor();

        // Don't let the object get too small or too large.
        mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));

        invalidate();
        return true;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我ぃ本無心為│何有愛 2024-12-13 05:59:09

最后自己完成了。

通过将此公式应用于 (px,py) 坐标来绘制所有内容:

float px = ev.getX() / mScaleFactor + rect.left;
float py = ev.getY() / mScaleFactor + rect.top;
rect = canvas.getClipBounds();
//Get them in on Draw function and apply above formula before drawing

Done it finally by myself.

Draw everything by applying this formula to (px,py) coordinates:

float px = ev.getX() / mScaleFactor + rect.left;
float py = ev.getY() / mScaleFactor + rect.top;
rect = canvas.getClipBounds();
//Get them in on Draw function and apply above formula before drawing
陌伤ぢ 2024-12-13 05:59:09
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    clipBounds_canvas = canvas.getClipBounds();
    /////Do whatever you want to do..!!!            
};

@Override
public boolean onTouchEvent(MotionEvent ev) {
    int x = ev.getX() / zoomFactor + clipBounds_canvas.left;
    int y = ev.getY() / zoomFactor + clipBounds_canvas.top;
    //Use the above two values insted of ev.getX() and ev.getY();
}

希望这会有所帮助。

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    clipBounds_canvas = canvas.getClipBounds();
    /////Do whatever you want to do..!!!            
};

@Override
public boolean onTouchEvent(MotionEvent ev) {
    int x = ev.getX() / zoomFactor + clipBounds_canvas.left;
    int y = ev.getY() / zoomFactor + clipBounds_canvas.top;
    //Use the above two values insted of ev.getX() and ev.getY();
}

Hope this will help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文