Draw on a canvas with Bitmap as Background

2019-04-02 06:12发布

问题:

I am trying to draw on a picture i have taken. Drawing works without the picture drawn but if i draw the bitmap i only see the bitmap but n drawing comes up. I tried a lot but nothing seems to help. Thanks in advance.

private class myView extends View implements OnTouchListener{

    File root = Environment.getExternalStorageDirectory();
    Path path;
    ArrayList<Path> _graphics = new ArrayList<Path>();
    Bitmap myBitmap;
    Paint myPaint;

    public myView(Context context) {
        super(context);

        File file = new File(root, "temp.jpg");
        myBitmap = null;
        if (file.exists()) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            myBitmap = setBitmap(BitmapFactory.decodeFile(new File(root,
                    "temp.jpg").getPath(), options));
        }

        myPaint = new Paint();
        myPaint.setColor(Color.GREEN);
        myPaint.setStyle(Paint.Style.STROKE);
        myPaint.setStrokeWidth(3);
    }

    @Override
    protected void onDraw(Canvas canvas) {          
        canvas.drawBitmap(myBitmap, 0, 0, null);
        for (Path path : _graphics) {
             canvas.drawPath(path, myPaint);
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            path = new Path();
            path.moveTo(event.getX(), event.getY());
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            path.lineTo(event.getX(), event.getY());
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            path.lineTo(event.getX(), event.getY());
            _graphics.add(path);
        }

        return true;
    }
}

回答1:

public class DrawView extends View implements OnTouchListener {
private Canvas mCanvas;
private Path mPath;
private Paint mPaint;
private ArrayList<Path> paths = new ArrayList<Path>();
private ArrayList<Path> undonePaths = new ArrayList<Path>();


public DrawView(Context context) {
    mBitmapPaint = new Paint(Paint.DITHER_FLAG);
    super(context);
    setFocusable(true);
    setFocusableInTouchMode(true);
    this.setOnTouchListener(this);
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(Color.CYAN);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(4);
    mCanvas = new Canvas();
    mPath = new Path();
    paths.add(mPath);

}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  super.onSizeChanged(w, h, oldw, oldh);
  BitmapFactory.Options opt = new BitmapFactory.Options();
          opt.inMutable = true;
  Bitmap tempbit = BitmapFactory.decodeFile(new File(root,
                "temp.jpg").getPath(),opt);
  Bitmap mBitmap=Bitmap.createBitmap(tempbit.getWidth, tempbit.getHeight, Bitmap.Config.ARGB_8888);
  mBitmap=Bitmap.createBitmap(tempbit);
  mCanvas = new Canvas(mBitmap);
}

@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mBitmap,0,0,mBitmapPaint);
    for (Path p : paths) {
        canvas.drawPath(p, mPaint);
    }
}

private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;

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

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;
    }
}

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 = new Path();
    paths.add(mPath);
}

public void onClickUndo() {
    if (paths.size() > 0) {
        undonePaths.add(paths.remove(paths.size() - 1));
        invalidate();
    } else {

    }
    // toast the user
}

public void onClickRedo() {
    if (undonePaths.size() > 0) {
        paths.add(undonePaths.remove(undonePaths.size() - 1));
        invalidate();
    } else {

    }
    // toast the user
}

public boolean onTouch(View arg0, 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;
}