我创建了一个具有图像的自定义视图,用户可以平移和缩放image.zoom效果正常工作但是当我移动图像时它的移动完美但是当我再次移动图像时,它首先会移动到初始状态位置即x = 0,y = 0,然后它再次从那里移动,但我希望图像从最后位置移动。
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.scale(scaleFactor, scaleFactor);
if (!flag) {
canvas.translate(dxFactor, dyFactor);
}
image.draw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
scaleGestureDetector.onTouchEvent(event);
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN: {
dxFactorLast = event.getX();
dyFactorLast = event.getY();
flag = true;
break;
}
case MotionEvent.ACTION_MOVE: {
dxFactorCurrent=event.getX();
dyFactorCurrent=event.getY();
if(dxFactorCurrent!=dxFactorLast || dyFactorCurrent!=dyFactorLast)
{
dxFactor = dxFactorCurrent - dxFactorLast;
dyFactor = dyFactorCurrent - dyFactorLast;
flag = false;
invalidate();
}
break;
}
case MotionEvent.ACTION_UP: {
break;
}
}
return true;
}
图像正在顺畅地移动但是当我下次移动图像时,它不会从当前位置移动而不是移动到其初始位置并再次从它们移动。