画布

时间:2015-09-04 06:06:55

标签: android android-canvas

我试图用画布绘制一条线。我能画一条线但是一旦我从屏幕上拿出手指它就会消失。我需要屏幕上可见的线条。这是我的代码`

 public class CanvasView extends View {
 private Canvas mcanvas;
 private Bitmap mBitmap;
 private Path mPath;
private Paint mPaint;
private float mX,mY;
private static final float TOLERANCE = 5;
Context context;

private float firstX ;
private float firstY ;

public CanvasView(Context c, AttributeSet attrs) {
    super(c, attrs);

    context = c;

    mPath = new Path();

    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setColor(Color.BLACK);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeWidth(4f);

    firstX = (float)68.85;
    firstY = (float)60.25;

}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);

    mBitmap     =   Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
    mcanvas     =   new Canvas(mBitmap);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
//        canvas.drawPath(mPath, mPaint);
//        canvas.drawPoint(mX,mY,mPaint);/*just a point tarvel
    canvas.drawLine(firstX,firstY,mX,mY,mPaint);
}

private void downTouch(float x, float y){
    mPath.reset();
    mPath.moveTo(x,y);
    mX  =   x;
    mY  =   y;
    firstX = x;
    firstY = y;
}

private void moveTouch(float x, float y){
//        float dx = Math.abs(x - mX);
//        float dy = Math.abs(y - mY);
//        if (dx >= TOLERANCE || dy >= TOLERANCE) {
//            mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
        mX = x;
        mY = y;
//        }
}

private void upTouch(float x, float y){
    mPath.lineTo(x,y);
    mPath.moveTo(x,y);
    firstX = x;
    firstY = y;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();

    switch (event.getAction()){
        case MotionEvent.ACTION_DOWN:
            downTouch(x,y);
            invalidate();
            break;

        case MotionEvent.ACTION_MOVE:
            moveTouch(x, y);
            invalidate();
            break;

        case MotionEvent.ACTION_UP:
            upTouch(x,y);
            invalidate();
            break;
    }

    return true;
}

public void clearCanvas(){
    mPath.reset();
    invalidate();
}
}`

任何人都可以帮助我。任何帮助/建议表示赞赏。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

你能在下面试一下吗?

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class CanvasView extends View {

    private static final float STROKE_WIDTH = 5f;
    private static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2;
    private Paint paint = new Paint();
    private Path path = new Path();
    private float lastTouchX;
    private float lastTouchY;
    private final RectF dirtyRect = new RectF();

    public CanvasView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint.setAntiAlias(true);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(STROKE_WIDTH);
    }

    public void clear() {
        path.reset();
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawPath(path, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float eventX = event.getX();
        float eventY = event.getY();

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            path.moveTo(eventX, eventY);
            lastTouchX = eventX;
            lastTouchY = eventY;
            return true;

        case MotionEvent.ACTION_MOVE:
        case MotionEvent.ACTION_UP:
            resetDirtyRect(eventX, eventY);
            int historySize = event.getHistorySize();
            for (int i = 0; i < historySize; i++) {
                float historicalX = event.getHistoricalX(i);
                float historicalY = event.getHistoricalY(i);
                expandDirtyRect(historicalX, historicalY);
                path.lineTo(historicalX, historicalY);
            }

            path.lineTo(eventX, eventY);
            break;

        default:
            return false;
        }

        invalidate((int) (dirtyRect.left - HALF_STROKE_WIDTH),
                (int) (dirtyRect.top - HALF_STROKE_WIDTH),
                (int) (dirtyRect.right + HALF_STROKE_WIDTH),
                (int) (dirtyRect.bottom + HALF_STROKE_WIDTH));

        lastTouchX = eventX;
        lastTouchY = eventY;

        return true;
    }

    private void expandDirtyRect(float historicalX, float historicalY) {
        if (historicalX < dirtyRect.left) {
            dirtyRect.left = historicalX;
        } else if (historicalX > dirtyRect.right) {
            dirtyRect.right = historicalX;
        }
        if (historicalY < dirtyRect.top) {
            dirtyRect.top = historicalY;
        } else if (historicalY > dirtyRect.bottom) {
            dirtyRect.bottom = historicalY;
        }
    }

    private void resetDirtyRect(float eventX, float eventY) {
        dirtyRect.left = Math.min(lastTouchX, eventX);
        dirtyRect.right = Math.max(lastTouchX, eventX);
        dirtyRect.top = Math.min(lastTouchY, eventY);
        dirtyRect.bottom = Math.max(lastTouchY, eventY);
    }
}

希望这会对你有所帮助。