我有问题在画布上的缩放和变换图像上绘制线条,以及转换后我能够在图像上绘制线条,但如果图像缩放(放大或缩小),则线条使用手指触摸意外地绘制了像素。由于我正在从事油漆应用,但对此没有更多的了解,所以我需要你的帮助,我在谷歌搜索但没有运气,我没有找到任何东西
这里的图像是第一个默认正常工作,第二个移动的图像正常工作正确划线
虽然在缩放图像上的第三个没有正确绘制,但我的意思是如果从顶部/左角开始绘制然后线绘制点与此处不同
如果您需要代码然后说出来我会编辑问题
答案 0 :(得分:2)
好多次实施后我得到了解决方案。
这是我在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然后绘制线/使用手指触摸绘图时间的路径。
这是我的onTouch()代码
@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;
}