Android canvas drawpath方法绘制带边的路径

时间:2016-03-24 16:15:54

标签: android android-canvas

我正在为绘图应用程序使用自定义类。它工作正常,但我有一个问题。当我用正常运动绘制时,它显示边缘,当我用慢动作绘制时,它绘制得很好。

我的自定义课程:

public class MyCanvas extends View {

private Path drawPath;
private Paint drawPaint, canvasPaint;
private int paintColor = 0xFF660000;
private Canvas drawCanvas;
private Bitmap canvasBitmap;


public MyCanvas(Context context) {
    super(context);
    setUpDrawing();
}

public MyCanvas(Context context, AttributeSet attrs) {
    super(context, attrs);
    setUpDrawing();
}

public MyCanvas(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    setUpDrawing();
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public MyCanvas(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    setUpDrawing();
}

private void setUpDrawing() {
    drawPath = new Path();
    drawPaint = new Paint();
    drawPaint.setColor(paintColor);
    drawPaint.setAntiAlias(true);
    drawPaint.setStrokeWidth(3F);
    drawPaint.setStrokeCap(Paint.Cap.ROUND);
    drawPaint.setStrokeJoin(Paint.Join.ROUND);
    drawPaint.setStyle(Paint.Style.STROKE);
    canvasPaint = new Paint(Paint.DITHER_FLAG);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
    canvas.drawPath(drawPath, drawPaint);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    canvasBitmap = Bitmap.createBitmap(w, h,
            Bitmap.Config.ARGB_8888);
    drawCanvas = new Canvas(canvasBitmap);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    float touchX = event.getX();
    float touchY = event.getY();
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            drawPath.moveTo(touchX, touchY);
            break;
        case MotionEvent.ACTION_MOVE:
            drawPath.lineTo(touchX, touchY);
            break;
        case MotionEvent.ACTION_UP:
            drawPath.lineTo(touchX, touchY);
            drawCanvas.drawPath(drawPath, drawPaint);
            drawPath.reset();
            break;
        default:
            return false;
    }
    invalidate();
    return true;
}}
绘图后的

结果: enter image description here

需要:

enter image description here

我需要帮助去除边缘并在绘制时带来平滑度。 我也加速了硬件,但结果是一样的。

0 个答案:

没有答案