AndiM
AndiM

Reputation: 2188

Drawing line using drawPoint()

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.

Upvotes: 0

Views: 321

Answers (1)

WarrenFaith
WarrenFaith

Reputation: 57702

So how do you expect that a line should be drawn when you only draw dots? The touch event registration/handling is not fast enough to get fired on each new pixel your finger touched. Use a path to store your points and draw a line/path using the points in your path.

Upvotes: 1

Related Questions