开发者

Android - touch drawing

开发者 https://www.devze.com 2023-04-12 05:31 出处:网络
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 betw

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)开发者_如何学Python;
        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.

Android - touch drawing


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.


You'd better run on device not emulator, since emulator processing the real-time action is too slow :).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号