就在画布上缩放和转换图像的android画线 [英] android draw line on scaled and transformed image on canvas

查看:421
本文介绍了就在画布上缩放和转换图像的android画线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题画线的缩放和转换图像在画布上,以及对于转变我能画线的图像,但在拍摄时缩放(放大或缩小),然后行引来了意外使用手指触摸像素。由于我工作的绘图应用程序,但没有知识,更多关于这个,所以我需要你的帮助,我已搜查它在谷歌,但没有运气,我没有发现任何东西。

I have problem to draw line on scaled and transformed image on canvas, well for the transformed I am able to draw line on image but if the image was scaled(zoom in or out) then line drew unexpectedly pixel using finger touch. As I am working on paint application but didn't have knowledge more about this so I need your help, I have searched it on google but no luck, I didn't found any thing

下面是图片1日是默认的正常工作,并在第二个移动图像正常工作正常提请行

here is the images 1st is default working properly and in second moved image working properly drew the line properly

而在第三个对缩放后的图像不能正确得出,我的意思是,如果从顶部/左上角开始绘制后再行画点是不同的喜欢这里

while in third on scaled image not properly draw, I mean if start draw from the top/left corner then the line drew point was different like here

如果您需要code,那么说出来,我会编辑的问题

if you need for code then say it I'll edit the question

推荐答案

好了很多实施后,我得到了这样的解决方案。

Ok after many implementation I got the solution for this.

这是我的code中的OnDraw()

This is my code for drawing in onDraw()

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.save();
    canvas.translate(mPosX, mPosY);
    canvas.scale(mScaleFactor, mScaleFactor);

    canvas.drawBitmap(MoveImageTestActivity.bmp, 0,0,null);
    if(mScaleFactor!=1.f)
        canvas.restore();

    if(isDrawing)
        canvas.drawPath(tempPath,get_paint());

    if(mScaleFactor==1.f)
        canvas.restore();
}

在这里你必须检查的比例因子值恢复画布。如果比例因子值默认是1.F和形象是正义之举,然后拉绳/路径之后恢复,如果比例因子不等于到1.F(默认),则恢复画布1日再画线/路径上使用手指触摸绘图时间。

here you have to check for the scale factor value for restore the canvas. If the scale factor value was default that is 1.f and image was just move then restore after the draw line/path, if the scale factor was not equals to the 1.f (default) then restore the canvas 1st then draw line/path at using finger touch drawing time.

这是我的onTouch()code

here is my onTouch() code

@Override
public boolean onTouchEvent(MotionEvent ev) {
    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();

        if(isDrawing){
            float dx = ev.getX();
            float dy = ev.getY();;

            if(mScaleFactor==1.f){
                dx = ev.getX() - mPosX;
                dy = ev.getY() - mPosY;
            }
            tempPath = new Path();
            tempPath.moveTo(dx,dy);
        }else{
            mLastTouchX = x;
            mLastTouchY = y;
            // Save the ID of this pointer
            mActivePointerId = ev.getPointerId(0);
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        // Find the index of the active pointer and fetch its position
        if(isDrawing){
            float dx = ev.getX();
            float dy = ev.getY();;

            if(mScaleFactor==1.f){
                dx = ev.getX() - mPosX;
                dy = ev.getY() - mPosY;
            }
            tempPath.lineTo(dx,dy);
        }else{
            final int pointerIndex = ev.findPointerIndex(mActivePointerId);
            final float x = ev.getX(pointerIndex);
            final float y = ev.getY(pointerIndex);

            final float dx = x - mLastTouchX;
            final float dy = y - mLastTouchY;

            mPosX += dx;
            mPosY += dy;

            mLastTouchX = x;
            mLastTouchY = y;
        }
        break;
    }
    case MotionEvent.ACTION_UP: {
        points.clear();
        mActivePointerId = INVALID_POINTER_ID;
        break;
    }

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

    case MotionEvent.ACTION_POINTER_UP: {
        if(!isDrawing){
            // Extract the index of the pointer that left the touch sensor
            final int pointerIndex = (action & 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;
    }
    }
    invalidate();
    return true;
}

这篇关于就在画布上缩放和转换图像的android画线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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