I've built a customview to paint over a bitmap the problem is when I try to save or share the image only the original bitmap is saved without the painted lines over it, here's the code i use for custom view and for saving image and setting the view to the image view .
Here is my custom view :
public class DrawView extends android.support.v7.widget.AppCompatImageView {
private ArrayList<ColouredPoint> paths ;
private ColouredPoint mPath;
private Paint mPaint;
private static final float TOUCH_TOLERANCE = 4;
boolean erase;
// Current used colour
private int mCurrColour;
public void setErase(boolean era){
erase=era;
}
public void setColor(int colour) {
mCurrColour = colour;
}
public DrawView(Context context) {
super(context);
init();
}
// XML constructor
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
paths = new ArrayList<>();
mPaint = new Paint();
mPath = new ColouredPoint(mCurrColour);
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(15);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
}
private float mX, mY;
@Override
public boolean onTouchEvent(MotionEvent event) {
// Capture a reference to each touch for drawing
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mPath = new ColouredPoint(mCurrColour);
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
invalidate();
break;
case MotionEvent.ACTION_MOVE:
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;
}
invalidate();
break;
case MotionEvent.ACTION_UP:
mPath.lineTo(mX, mY);
paths.add(mPath);
invalidate();
break;
}
return super.onTouchEvent(event);
}
@Override
protected void onDraw(Canvas c) {
// Let the image be drawn first
super.onDraw(c);
// Draw your custom points here
if(!erase){
for (ColouredPoint i:paths) {
mPaint.setColor(i.colour);
c.drawPath(i, mPaint);}
mPaint.setColor(mCurrColour);
c.drawPath(mPath,mPaint);}
else if (paths.size()>0){
paths.remove(paths.size()-1);} }
/**
* Class to store the coordinate and the colour of the point.
*/
private class ColouredPoint extends Path{
int colour;
public ColouredPoint(int colour) {this.colour = colour;}}}
i call it in main activity :
im = (DrawView) findViewById(R.id.imageView); im.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
DrawView mcustomImagview = (DrawView) v;
mcustomImagview.invalidate();
mcustomImagview.setColor(color);
mcustomImagview.setErase(value);
if (v.onTouchEvent(event)) {
// Do something with event.getX(), event.getY()
}
return true;
}
});
i save the image by this code :
Bitmap b2 = ((BitmapDrawable)im.getDrawable()).getBitmap();
MediaStore.Images.Media.insertImage(getContentResolver(), b2, "title",null);
Toast.makeText(getBaseContext(),"Saved",Toast.LENGTH_LONG).show();