我正在尝试在画布中移动绘图。我有工作代码,但问题是,当我点击画布第二次拖动时,它会将自身重置为其初始位置(默认位置)n然后再次开始移动。我想让它从我第一次拖动的地方移开。
在我的自定义视图代码中,我有一个名为setupCoordinates的函数,其中初始化了我的绘图坐标。 (x和y,我用它来移动图纸。这些值由我活动的onTouch监听器更新)
我将视图添加到相对布局
relLay.addView(custView);
我的布局有一个onTouch侦听器
relLay.setOntouchListener(touch);
在我的触控听众中,我通过传递evet.getX()和event.getY()来更新我的绘图
custView.setXY(evet.getX(), event.getY());
以下是我正在绘制的自定义视图的代码:
public CustView(Context context) {
super(context);
setupDrawing();
setupCoordinates();
}
private void setupDrawing() {
path = new Path();
paint = new Paint();
borderPaint = new Paint();
paint.setColor(color);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
borderPaint.setColor(Color.BLACK);
borderPaint.setAntiAlias(true);
borderPaint.setStyle(Paint.Style.STROKE);
}
private void setupCoordinates() {
x = 0;
y = 0;
top = 150 ;
right = 450 ;
left = 200 ;
bottom = 400 ;
linex = 120;
liney = 300;
openSize = 20;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
path.reset();
mCanvas = canvas;
rectf = new RectF(left+x, top+y, right+x, bottom+y);
rect = rectf;
int angle = (int) angle(new Point((getWidth())/2, (getHeight())/2), new Point(linex,liney));
int startAngle = angle + (openSize/2);
int sweepAngle = 360 - openSize;
path.arcTo(rectf, startAngle, sweepAngle);
path.lineTo(linex, liney);
canvas.drawPath(path, paint);
canvas.drawPath(path, borderPaint);
}
public void setXY(int dx, int dy){
x = dx;
y = dy;
invalidate();
}
private double angle(Point point1, Point point2) {
double deltaX = (point1.x - point2.x);
double deltaY = (point1.y - point2.y);
double angle = Math.toDegrees(Math.atan2(deltaY, deltaX));
if (angle < 0) {
angle += 360;
}
if (angle > 360) {
angle -= 360;
}
// Compensate for 0
angle -= 180;
return angle;
}
任何人都可以告诉我为什么在第二次拖动时它会重置到初始位置?
我也对在画布内移动图纸的其他方法持开放态度,如有,请告诉我。我不想只移动视图或画布。
提前致谢:)
答案 0 :(得分:0)
以下是如何避免捕捉的示例,并调整应该起作用的值:
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mCustomView.setXYAnimated((int) event.getX(),
(int) event.getY());
break;
case MotionEvent.ACTION_MOVE:
mCustomView.setXY((int) event.getX(),
(int) event.getY());
break;
case MotionEvent.ACTION_UP:
mCustomView.setXY((int) event.getX(),
(int) event.getY());
break;
}
在自定义视图中:
private int x = -1;
private int y = -1;
private CountDownTimer mCountDownTimer;
public void setXY(int dx, int dy) {
if (mCountDownTimer == null) {
x = dx;
y = dy;
invalidate();
}
}
public void setXYAnimated(final int newX, final int newY) {
if (x == -1 && y == -1) {
setXY(newX, newY);
} else {
if (mCountDownTimer == null) {
final int time = 250;
mCountDownTimer = new CountDownTimer(time, 10) {
@Override
public void onTick(long millisUntilFinished) {
float progress = 1 - ((float) millisUntilFinished / time);
int x2, y2;
if (x < newX) {
x2 = (int) (x + (progress * (newX - x)));
} else {
x2 = (int) (x - (progress * (x - newX)));
}
if (y < newY) {
y2 = (int) (y + (progress * (newY - y)));
} else {
y2 = (int) (y - (progress * (y - newY)));
}
x = x2;
y = y2;
postInvalidate();
}
@Override
public void onFinish() {
mCountDownTimer = null;
setXY(newX, newY);
}
}.start();
}
}
}