I created a sample drawing app where the user can draw using variable width stroke, So far drawing a path with variable stroke is working, but the lines drawn are not smooth. Th
Instead of jumping immediately to a new stroke width when you detect a change, you could set a target and interpolate toward it until you reach it. Your mStrokes
would need to be Float
s instead of Integer
s.
private static final float STROKE_DELTA = 0.0001f; // for float comparison
private static final float STROKE_INCREMENT = 0.01f; // amount to interpolate
private float currentStroke = STROKE_WIDTH;
private float targetStroke = STROKE_WIDTH;
Where you currently create a new path for a new stroke width, do something like this:
// if current not roughly equal to target
if( Math.abs(targetStroke - currentStroke) > STROKE_DELTA ) {
// move towards target by the increment
if( targetStroke > currentStroke )
currentStroke = Math.min(targetStroke, currentStroke + STROKE_INCREMENT);
else
currentStroke = Math.max(targetStroke, currentStroke - STROKE_INCREMENT);
mPath.lineTo(mX, mY);
mPath = new Path();
mPath.moveTo(mX,mY);
mPaths.add(mPath);
mStrokes.add(currentStroke);
}
You would update targetStroke
where you currently set variableWidthDelta
.