获取放大/缩小或拖动机器人后,画布的坐标 [英] Get Canvas coordinates after scaling up/down or dragging in android

查看:250
本文介绍了获取放大/缩小或拖动机器人后,画布的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发中,我粘贴图片,做绘画在画布上的应用程序。这个应用程序还可以放大/缩小画布或将其拖动到不同的位置。 我的问题是:我不能缩放或拖动画布后,正确的画布坐标。我想画的画布上进行缩放或拖动但无法检索在那里我已经碰到了合适的位置后,手指画.. :( 此外,我是新蜂。这里是code。

  @覆盖
公共无效的OnDraw(帆布油画){
    super.onDraw(画布);

    canvas.save();
    //canvas.translate(mPosX,mPosY);
    canvas.scale(mScaleFactor,mScaleFactor,super.getWidth()* 0.5F,
            super.getHeight()* 0.5F);
    mIcon.draw(画布);
    对于(路径路径:listPath){
        canvas.drawPath(路径,油漆);
    }
    canvas.restore();
}

公共TouchExampleView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
    超(背景下,ATTRS,defStyle);

}

@覆盖
公共布尔的onTouchEvent(MotionEvent EV){
    //让ScaleGestureDetector检查所有事件。
    mScaleDetector.onTouchEvent(EV);

    最终诠释行动= ev.getAction();
    开关(行动及放大器; MotionEvent.ACTION_MASK){
    案例MotionEvent.ACTION_DOWN:{
        最终浮动X = ev.getX();
        最终浮动Y = ev.getY();

        mLastTouchX = X;
        mLastTouchY = Y;
        mActivePointerId = ev.getPointerId(0);
        打破;
    }

    案例MotionEvent.ACTION_MOVE:{
        最终诠释pointerIndex = ev.findPointerIndex(mActivePointerId);
        最终浮动X = ev.getX(pointerIndex);
        最终浮动Y = ev.getY(pointerIndex);

        //只有当ScaleGestureDetector没有正在处理手势移动。
        如果(!mScaleDetector.isInProgress()){
            最终浮动DX = X  -  mLastTouchX;
            最终浮动DY = Y  -  mLastTouchY;

            mPosX + = DX;
            mPosY + = DY;

            无效();
        }

        mLastTouchX = X;
        mLastTouchY = Y;

        打破;
    }

    案例MotionEvent.ACTION_UP:{
        mActivePointerId = INVALID_POINTER_ID;
        打破;
    }

    案例MotionEvent.ACTION_CANCEL:{
        mActivePointerId = INVALID_POINTER_ID;
        打破;
    }

    案例MotionEvent.ACTION_POINTER_UP:{
        最终诠释pointerIndex =(ev.getAction()及MotionEvent.ACTION_POINTER_INDEX_MASK)GT;> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
        最终诠释pointerId = ev.getPointerId(pointerIndex);
        如果(pointerId == mActivePointerId){
            //这是我们主动指针往上走。选择一个新的
            //活跃的指针和相应的调整。
            最终诠释newPointerIndex = pointerIndex == 0? 1:0;
            mLastTouchX = ev.getX(newPointerIndex);
            mLastTouchY = ev.getY(newPointerIndex);
            mActivePointerId = ev.getPointerId(newPointerIndex);
        }
        打破;
    }
    }

    浮objectNewX,objectNewY;
    如果(mScaleFactor> = 1){
        objectNewX = ev.getX()+(ev.getX() -  super.getWidth()* 0.5F)*(mScaleFactor  -  1);
        objectNewY = ev.getY()+(ev.getY() -  super.getHeight()* 0.5F)*(mScaleFactor  -  1);
    } 其他 {
        objectNewX = ev.getX() - (ev.getX() -  super.getWidth()* 0.5F)*(1  -  mScaleFactor);
        objectNewY = ev.getY() - (ev.getY() -  super.getHeight()* 0.5F)*(1  -  mScaleFactor);
    }

    如果(ev.getAction()== MotionEvent.ACTION_DOWN){
        路径=新路径();
        path.moveTo(objectNewX,objectNewY);
        path.lineTo(objectNewX,objectNewY);
    }否则,如果(ev.getAction()== MotionEvent.ACTION_MOVE){
        path.lineTo(objectNewX,objectNewY);
        listPath.add(路径);
    }否则,如果(ev.getAction()== MotionEvent.ACTION_UP){
        path.lineTo(objectNewX,objectNewY);
        listPath.add(路径);
    }

    返回true;
}

私有类ScaleListener扩展
        ScaleGestureDetector.SimpleOnScaleGestureListener {
    @覆盖
    公共布尔onScale(ScaleGestureDetector检测器){
        mScaleFactor * = detector.getScaleFactor();

        //不要让物体得到过小或过大。
        mScaleFactor = Math.max(0.1F,Math.min(mScaleFactor,5.0F));

        无效();
        返回true;
    }
}
 

解决方案

由我自己做了最后。

通过应用该公式(PX,PY)画出所有坐标:

 浮动PX = ev.getX()/ mScaleFactor + rect.left;
浮PY = ev.getY()/ mScaleFactor + rect.top;
RECT = canvas.getClipBounds();
//让他们在绘图功能和应用上面的公式,再进行绘制
 

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;
    }
}

解决方案

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

这篇关于获取放大/缩小或拖动机器人后,画布的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆