带有图像的Android绘画仅擦除颜色而不是背景图像



Android 使用图像绘制仅擦除颜色而不擦除背景图像

我有一个相对布局,其中图像设置为其android:background="@mipmap/ic_launcher"

然后有一个线性布局,我们可以在上面绘制各种颜色 使用橡皮擦我们可以 清除颜色 ,这工作正常

有了MotionEvent.ACTION_MOVE:颜色立即删除

此函数

public void setErase(boolean isErase){
//set erase true or false
if(isErase){
drawPaint.setAlpha(0xFF);
drawPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

}
else {
drawPaint.setXfermode(null);
}
}

问题出在哪里,Bakground图像也会随之擦除,

case MotionEvent.ACTION_UP:
drawCanvas.drawPath(drawPath, drawPaint);
drawPath.reset();
break;

图像被重置,我们在相对布局中取回图像

我只想删除颜色而不是带有MotionEvent.ACTION_MOVE的图像

我提到了这个 https://github.com/fachrur/Gmbr

代码是

对于自定义视图

public class CustomView extends View {
//drawing path
private Path drawPath;
//drawing and canvas paint
private Paint drawPaint, canvasPaint;
//initial color
private int paintColor = 0xFF660000;
//canvas
private Canvas drawCanvas;
//canvas bitmap
private Bitmap canvasBitmap;
private float brushSize, lastBrushSize;
private  int width, height;
//comment to attached on the image
private  String comment;
public CustomView(Context context, AttributeSet attrs ){
super(context, attrs);
setupDrawing();
}
private void setupDrawing(){
//get drawing area setup for interaction
drawPath = new Path();
drawPaint = new Paint();
brushSize = getResources().getInteger(R.integer.medium_size);
lastBrushSize = brushSize;
//initialize drawPaint
drawPaint.setColor(paintColor);
//initialize drawPath
drawPaint.setAntiAlias(true);
drawPaint.setStrokeWidth(brushSize);
drawPaint.setStyle(Paint.Style.STROKE);
drawPaint.setStrokeJoin(Paint.Join.ROUND);
drawPaint.setStrokeCap(Paint.Cap.ROUND);
canvasPaint = new Paint(Paint.DITHER_FLAG);
//drawCanvas.drawText("test", 20, 20, null);
}
public void setImageBitmap(Bitmap bmp){
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
/*
* */
int bmp_width = bmp.getWidth();
int bmp_height = bmp.getHeight();
float scaleWidth = ((float) width) / bmp_width;
float scaleHeight = ((float) height) / bmp_height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(
bmp, 0, 0, bmp_width, bmp_height, matrix, false);
//
drawCanvas.drawBitmap(resizedBitmap, 0, 0, paint);
invalidate();
}
public void startNew(){
drawCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
invalidate();
}
public void setComment(String commentText){
this.comment = commentText;
}
public void setErase(boolean isErase){
//set erase true or false
if(isErase){
drawPaint.setAlpha(0xFF);
drawPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

}
else {
drawPaint.setXfermode(null);
}
}
public void setBrushSize(float newSize){
//update size
brushSize= TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
newSize, getResources().getDisplayMetrics());
drawPaint.setStrokeWidth(brushSize);
}
public void setLastBrushSize(float lastSize){
lastBrushSize=lastSize;
}
public float getLastBrushSize(){
return lastBrushSize;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
width = w;
height = h;
canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
drawCanvas = new Canvas(canvasBitmap);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
canvas.drawPath(drawPath, drawPaint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float touchX = event.getX();
float touchY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if(!Objects.equals(comment, "") && comment != null ){
canvasPaint.setTextSize(60);
canvasPaint.setColor(paintColor);
drawCanvas.drawText(comment, touchX, touchY, canvasPaint);
comment = "";
return true;
}
else {
drawPath.moveTo(touchX, touchY);
}
}
break;
case MotionEvent.ACTION_MOVE:
drawPath.lineTo(touchX, touchY);
break;
case MotionEvent.ACTION_UP:
drawCanvas.drawPath(drawPath, drawPaint);
drawPath.reset();
break;
default:
return false;
}
invalidate();
return true;
}
public void setColor(String newColor){
//set color
invalidate();
paintColor = Color.parseColor(newColor);
drawPaint.setColor(paintColor);
}
public void setColor(int newColor){
invalidate();
drawPaint.setColor(newColor);
}
}

布局是

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/ic_launcher"
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation = "vertical"
android:background="#00000000"
tools:context=".MainActivity"
android:id="@+id/main_activity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/new_btn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:contentDescription="@string/start_new"
android:src="@drawable/new_pic"/>
<ImageButton
android:id="@+id/draw_btn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:contentDescription="@string/start_new"
android:src="@drawable/ic_brush"/>
<ImageButton
android:id="@+id/erase_btn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:contentDescription="@string/start_new"
android:src="@drawable/ic_eraser"/>
<ImageButton
android:id="@+id/save_btn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:contentDescription="@string/start_new"
android:src="@drawable/ic_save"/>
<ImageButton
android:id="@+id/takingPhoto_btn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:contentDescription="@string/photo"
android:src="@drawable/ic_camera"/>
<ImageButton
android:id="@+id/text_btn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:contentDescription="@string/text"
android:src="@drawable/ic_text"/>
</LinearLayout>
<com.rfachrur.gmbr.CustomView
android:id="@+id/drawing"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:layout_weight="1"
android:background="#00000000"
android:layout_margin="0dp" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal" >
<Button
android:id="@+id/select_img"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="@string/add_image"
android:src="@drawable/ic_text"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<!-- Top Row -->
<LinearLayout
android:id="@+id/paint_colors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:layout_width="@dimen/large_brush"
android:layout_height="@dimen/large_brush"
android:layout_margin="2dp"
android:background="#FF660000"
android:contentDescription="@string/paint"
android:onClick="paintClicked"
android:src="@drawable/paint"
android:tag="#FF660000" />
<ImageButton
android:layout_width="@dimen/large_brush"
android:layout_height="@dimen/large_brush"
android:layout_margin="2dp"
android:background="#FFFF0000"
android:contentDescription="@string/paint"
android:onClick="paintClicked"
android:src="@drawable/paint"
android:tag="#FFFF0000" />
<ImageButton
android:layout_width="@dimen/large_brush"
android:layout_height="@dimen/large_brush"
android:layout_margin="2dp"
android:background="#FFFF6600"
android:contentDescription="@string/paint"
android:onClick="paintClicked"
android:src="@drawable/paint"
android:tag="#FFFF6600" />
<ImageButton
android:layout_width="@dimen/large_brush"
android:layout_height="@dimen/large_brush"
android:layout_margin="2dp"
android:background="#FFFFCC00"
android:contentDescription="@string/paint"
android:onClick="paintClicked"
android:src="@drawable/paint"
android:tag="#FFFFCC00" />
<ImageButton
android:layout_width="@dimen/large_brush"
android:layout_height="@dimen/large_brush"
android:layout_margin="2dp"
android:background="#FF009900"
android:contentDescription="@string/paint"
android:onClick="paintClicked"
android:src="@drawable/paint"
android:tag="#FF009900" />
<ImageButton
android:layout_width="@dimen/large_brush"
android:layout_height="@dimen/large_brush"
android:layout_margin="2dp"
android:background="#FF009999"
android:contentDescription="@string/paint"
android:onClick="paintClicked"
android:src="@drawable/paint"
android:tag="#FF009999" />
</LinearLayout>
<!-- Bottom Row -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:layout_width="@dimen/large_brush"
android:layout_height="@dimen/large_brush"
android:layout_margin="2dp"
android:background="#FF0000FF"
android:contentDescription="@string/paint"
android:onClick="paintClicked"
android:src="@drawable/paint"
android:tag="#FF0000FF" />
<ImageButton
android:layout_width="@dimen/large_brush"
android:layout_height="@dimen/large_brush"
android:layout_margin="2dp"
android:background="#FF990099"
android:contentDescription="@string/paint"
android:onClick="paintClicked"
android:src="@drawable/paint"
android:tag="#FF990099" />
<ImageButton
android:layout_width="@dimen/large_brush"
android:layout_height="@dimen/large_brush"
android:layout_margin="2dp"
android:background="#FFFF6666"
android:contentDescription="@string/paint"
android:onClick="paintClicked"
android:src="@drawable/paint"
android:tag="#FFFF6666" />
<ImageButton
android:layout_width="@dimen/large_brush"
android:layout_height="@dimen/large_brush"
android:layout_margin="2dp"
android:background="#FFFFFFFF"
android:contentDescription="@string/paint"
android:onClick="paintClicked"
android:src="@drawable/paint"
android:tag="#FFFFFFFF" />
<ImageButton
android:layout_width="@dimen/large_brush"
android:layout_height="@dimen/large_brush"
android:layout_margin="2dp"
android:background="#FF787878"
android:contentDescription="@string/paint"
android:onClick="paintClicked"
android:src="@drawable/paint"
android:tag="#FF787878" />
<ImageButton
android:layout_width="@dimen/large_brush"
android:layout_height="@dimen/large_brush"
android:layout_margin="2dp"
android:background="#FF000000"
android:contentDescription="@string/paint"
android:onClick="paintClicked"
android:src="@drawable/paint"
android:tag="#FF000000" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>

我只想删除颜色而不是背景图像

这实际上解决了我的问题,非常易于使用和实现

https://github.com/mukeshsolanki/DrawingView-Android

最新更新