Рисовать на подгруженном изображении
Создаю приложение для рисования. На обычном пустом канвасе отлично рисует, но когда я хочу подрузить изображение из галереи при нажатии я продолжаю рисовать под картинкой. Что делать? Код DrawView класса:
public DrawView(Context context) {
this(context, null);
}
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
//the below methods smoothens the drawings of the user
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.GREEN);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
//0xff=255 in decimal
mPaint.setAlpha(0xff);
Log.d(TAG, "DrawView created");
}
//this method instantiate the bitmap and object
public void init(int height, int width) {
mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
//set an initial color of the brush
currentColor = Color.RED;
//set an initial brush size
strokeWidth = 20;
}
@Override
protected void onDraw(Canvas canvas) {
canvas.save();
//DEFAULT color of the canvas
int backgroundColor = Color.WHITE;
mCanvas.drawColor(backgroundColor);
//now, we iterate over the list of paths and draw each path on the canvas
for (Stroke fp : paths) {
mPaint.setColor(fp.color);
mPaint.setStrokeWidth(fp.strokeWidth);
mCanvas.drawPath(fp.path, mPaint);
}
// canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawBitmap(mBitmap, 0, 0, null);
canvas.restore();
}
public Bitmap save() {
return mBitmap;
}
protected void setImageUri(Uri uri) {
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), uri);
float aspecRatio = (float) bitmap.getHeight() / (float) bitmap.getWidth();
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int mImageWidth = displayMetrics.widthPixels;
int mImageHeight = Math.round(mImageWidth * aspecRatio);
mBitmap = Bitmap.createScaledBitmap(bitmap, mImageWidth, mImageHeight, false);
invalidate();
requestLayout();
mCanvas.drawBitmap(mBitmap, mImageWidth, mImageHeight, null);
} catch (IOException e) {
e.printStackTrace();
}
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "1", null);
return Uri.parse(path);
}
MainActivity:
private ActivityResultLauncher<Intent> someActivityResultLauncher;
someActivityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
// There are no request codes
Intent data = result.getData();
if (data != null) {
InputStream inputStream;
Uri selectedImage = data.getData();
try {
inputStream = getContentResolver().openInputStream(selectedImage);
Bitmap image = BitmapFactory.decodeStream(inputStream);
Log.d(TAG, image + " ");
Log.d(TAG, drawView + " ");
Uri imageUri = drawView.getImageUri(MainActivity.this, image);
Log.d(INSERT_IMAGE, "Image inserted");
drawView.setImageUri(imageUri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
});