0

I am trying to draw line in android.But i am not satisfy with that because it doesn't draw full line it draws a dotted line when i move object fastly and it draws a complete line when i move object slowly.Plaese help me why this happenes.I want just complete line not dotted line.My code is here: On Touch event of view:

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;
}

And my draw method is like this:

@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);


    }

And i also want to know how to clear all the drawing.
Please help me to solve this. Thank you.

4

1 回答 1

1

那么当你只画点的时候,你怎么期望画一条线呢?触摸事件注册/处理速度不够快,无法在您的手指触摸的每个新像素上触发。使用路径来存储您的点并使用路径中的点绘制一条线/路径。

于 2013-03-20T13:42:37.943 回答