I was trying to understand the finger paint application in Api demos in android.It is a sketching app.Here is the crux of the app.
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
开发者_运维知识库 break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
}
//Called when you place your finger on the screen.
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
//called when you move your finger on the screen
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
}
}
//called when you lift your finger
private void touch_up() {
mPath.lineTo(mX, mY);
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath.reset();
}
My doubt is in the function void touch_move(float x, float y).The function is called when you move your finger over the screen.Can someone tell me what is the reason the function mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2) is called and what is the concept behind using (x+mX)/2,(y+mY)/2 ?It would be great if someone could explain what is happening here.
P.S:I have already gone through the dev site and googled on this so please don't post links refering to the dev site
Look like it adds a curve from the start to the next point.
See this.
For an explanation of quadTo(), which is used to draw Bezier curves, see: http://developer.android.com/reference/android/graphics/Path.html#quadTo%28float,%20float,%20float,%20float%29 ...
As far as (x+mX)/2,(y+mY)/2, it looks very similar to the linear midpoint formula to me, but I'm not sure how it's being used here ... it looks like it might be related to finding the point lying on the midpoint of the line between the points (x,y) and (mX, mY)
Seems like you might also need to read a bit about constructing Bezier curves if you want to understand what is happening here.
精彩评论