【发布时间】:2015-10-19 12:16:16
【问题描述】:
我正在尝试删除 Canvas 的内容而不删除背景图像。我只想从画布中删除可绘制的内容,即线条、颜色等。但目前背景图像也被删除。到目前为止,基本上我尝试的是创建一个框架布局并将ImageView 放在另一个ImageView 上,考虑到如果顶部的图像被擦除,下面的精确副本将不会被擦除。
<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();
}
从源(图库)加载图像并将其设置为画布上的背景(作为位图)
【问题讨论】:
-
你是如何使用你的
Paint的? -
没关系,你好像在关注一些tuts+ guide
-
在链接教程中
DrawingView没有setImage方法。你能发布你的实现吗?不然真的很难帮你 -
@MattiaMaestrini:请看我提到的编辑。