Android - touch drawing

I am watching this sample drawing application from Google, and when I run the application in the simulator and try to draw with the mouse, the drawing does not display smoothly. Circles appear between them. Only when I move the mouse slowly does I get a solid line. I don’t have an actual device yet to test this, so I want to know if this will be the case on the device itself or is this the behavior of the simulator?

If it will be displayed on the device, what should I do to make the touch smooth?

UPDATE:

Actual code that makes the drawing:

private void drawPoint(float x, float y, float pressure, float width) { if (width < 1) width = 6; if (mBitmap != null) { float radius = width / 2; int pressureLevel = (int)(pressure * 255); mPaint.setARGB(pressureLevel, 255, 255, 255); mCanvas.drawCircle(x, y, radius, mPaint); mRect.set((int) (x - radius - 2), (int) (y - radius - 2), (int) (x + radius + 2), (int) (y + radius + 2)); invalidate(mRect); } } @Override public boolean onTouchEvent(MotionEvent event) { int N = event.getHistorySize(); int P = event.getPointerCount(); for (int i = 0; i < N; i++) { for (int j = 0; j < P; j++) { mCurX = event.getHistoricalX(j, i); mCurY = event.getHistoricalY(j, i); drawPoint(mCurX, mCurY, event.getHistoricalPressure(j, i), event.getHistoricalTouchMajor(j, i)); } } for (int j = 0; j < P; j++) { mCurX = event.getX(j); mCurY = event.getY(j); drawPoint(mCurX, mCurY, event.getPressure(j), event.getTouchMajor(j)); } } 

Any help in improving it will be greatly appreciated.

enter image description here

+4
source share
4 answers

It will be jagged, but I believe that the code is a drawing point, so if you just draw a line, starting from where the last end was drawn to a new point, it will be continuous.

And yes, it will look the same on the device itself.

Another option I make from memory is some pressure value that was between 0 and 255, or 0 and 1, and set it to the maximum value, all the time. This may be a simple solution to your problem.

This will help if you show the code that actually makes the drawing, btw.

UPDATE:

As I said, change this line:

 int pressureLevel = (int)(pressure * 255); 

to

 int pressureLevel = (int)(255); 

should fix your problem.

If this is not the case, and not using drawCircle , you can simply use drawLine , then you will keep track of the last circle you just drew.

+2
source

It’s better not to run the emulator on the device, since the emulator that processes the action in real time is too slow :).

0
source

Perhaps try the following:

 public boolean onTouch(View view, MotionEvent event) { if (event.getHistorySize() == 0) { x = event.getX(); y = event.getY(); } if (x == 0 && y == 0) { x = event.getX(); y = event.getY(); } bitmapCanvas.drawLine(x, y, event.getX(), event.getY(), paint); x = event.getX(); y = event.getY(); invalidate(); return true; } 

Some things need to be improved, but overall it is useful for drawing.

0
source

There is a simple method that should solve this problem. When you define the mPaint attributes, try:

 mPaint.setStrokeJoin(Paint.Join.ROUND); 

These attributes connect the jagged lines to a nice smooth line.

0
source

All Articles