简体   繁体   中英

Android - touch drawing

I am looking at this sample drawing app by Google, and when I run the app in the simulator and try to draw with my mouse, the drawing does not appear smoothly. The circles are appearing with gaps between them. Only when I drag my mouse slowly I get a unbroken line. I don't yet have an actual device to test this on, so I want to know if this is how it will be on the actual device also, or is this a simulator behavior?

If this is how it will appear on the device also, what should I do to make the touch drawing smooth?

UPDATE:

The actual code that does the drawing is:

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 would be greatly appreciated.

在此处输入图片说明

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

And yes, it will look the same way on an actual device.

The other option, which I am doing from memory, is there was some pressure value that was between 0 and 255, or 0 and 1, set it to the highest value, all the time. That may actually be a simple solution to your problem.

It would help if you show the code that is actually doing the drawing, btw.

UPDATE:

As I mentioned change this line:

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

to

int pressureLevel = (int)(255);

should fix your problem.

If that doesn't then rather than using drawCircle you may want to just use drawLine , then you would keep track of the last circle you had just drawn.

Maybe try this:

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 have to be improved, but in general this is good for drawing.

There is a simple method that's supposed to fix this problem, When you define the mPaint attributes try:

mPaint.setStrokeJoin(Paint.Join.ROUND);

This attributes joins the "jagged lines" into a nice smooth line.

您最好在设备而不是仿真器上运行,因为仿真器对实时操作的处理太慢了:)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM