我正在尝试在android中画线。但是我不满足于因为它没有绘制完整的线条,当我快速移动物体时它绘制了一条虚线,当我慢慢移动物体时它绘制了一条完整的线条.Plaese帮助我为什么会这样。我想要完整的线而不是虚线。我的代码在这里: 触摸事件视图:
public boolean onTouch(View view, MotionEvent event) {
// TODO Auto-generated method stub
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view
.getLayoutParams();
_xDelta = X - lParams.leftMargin;
Log.e("ACTION DOWN X", "" + Y + "---" + lParams.leftMargin);
_yDelta = Y - lParams.topMargin;
Log.e("ACTION DOWN Y", "" + Y + "---" + lParams.leftMargin);
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
.getLayoutParams();
layoutParams.leftMargin = X - _xDelta;
Log.e("ACTION Move left margin", "" + (X - _xDelta));
layoutParams.topMargin = Y - _yDelta;
Log.e("ACTION Move top margin", "" + (Y - _yDelta));
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
view.setBackgroundColor(random.nextInt());
view.setLayoutParams(layoutParams);
draw = new DrawLine(MainActivity.this, X - _xDelta, Y - _yDelta);
root.addView(draw);
break;
}
root.invalidate();
return true;
}
我的绘制方法是这样的:
@Override
protected void onDraw(final Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
// paint.setColor(random.nextInt());
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(4);
canvas.drawPoint(startX, startY, paint);
}
我也想知道如何清除所有图纸 请帮我解决这个问题。 谢谢。
答案 0 :(得分:1)
那么当你只绘制点时,你怎么期望画出一条线呢?触摸事件注册/处理速度不够快,无法触发手指触摸的每个新像素。使用路径存储您的点并使用路径中的点绘制线条/路径。