我试图在不删除背景图像的情况下擦除Canvas
的内容。我只想从画布中删除可绘制的内容,即线条,颜色等。但目前背景图像也会被删除。基本上我到目前为止尝试的是我创建了一个框架布局并将ImageView
放在另一个ImageView
上,考虑到如果顶部的图像被删除,那么下面的图像将不会被删除。< / p>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="310dp">
<com.almaarijsoft.kanvas.DrawingView
android:id="@+id/drawing"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/smoky" />
<com.almaarijsoft.kanvas.DrawingView
android:id="@+id/drawingViewBackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_gravity="bottom|center"
android:background="@color/smoky" />
</FrameLayout>
然后
private Paint drawPaint;
//usage
//set erase true or false
public void setErase(boolean isErase) {
erase = isErase;
if (erase) {
drawPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
} else{
drawPaint.setXfermode(null);
}
}
这就是我将图像设置为画布的方式
public static void setCanvasFromGalleryImage(Intent data, DrawingView drawView, DrawingView drawView2, Activity activity) {// DrawingView is class extended from View
if (data != null) {
// our BitmapDrawable for the thumbnail
BitmapDrawable bmpDrawable = null;
// try to retrieve the image using the data from the intent
Cursor cursor = activity.getContentResolver().query(data.getData(), null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
String fileSrc = cursor.getString(idx);
bitmap = BitmapFactory.decodeFile(fileSrc); // load
bitmap = Bitmap.createScaledBitmap(bitmap, 750, 600, false);
drawView.setImage(bitmap);
drawView2.setImage(bitmap);
drawView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); // IMPORTANT set hardware acceleration off with this line. Otherwise your view's background will NOT be transparent
drawView.bringToFront(); //
} else {
bmpDrawable = new BitmapDrawable(activity.getResources(), data.getData().getPath());
drawView.setImage(bitmap);
}
} else {
MyAppUtil.getToast(activity.getApplicationContext(), "Cancelled");
}
}
修改
// inside Drawview following implementation is added by me.
private Canvas drawCanvas;
//start new drawing
public void setImage(Bitmap bitmap) {
drawCanvas.drawBitmap(bitmap, 0, 0, null);
invalidate();
}
从源(图库)加载图像并将其设置为画布上的背景(作为位图)
答案 0 :(得分:1)
您可以尝试像这样修改setImage
的{{1}}:
DrawingView
如果在使用public void setImage(Bitmap bitmap) {
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
this.setBackground(drawable);
} else {
this.setBackgroundDrawable(drawable);
}
}
时将位图设置为背景,则只会删除画布上绘制的内容。
此外,您还不需要多个setErase(true)
但只需要一个。
答案 1 :(得分:0)
我只是通过扩展ImageView并在onDraw中调用super方法来获得描述的行为。
public class DrawingView extends ImageView
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
canvas.drawPath(drawPath, drawPaint);
}
设置&#34;背景&#34;通过ImageView
的方法:
drawView.setImageBitmap(bitmap);