gpt4 book ai didi

Android Eraser删除背景图像如何避免它

转载 作者:行者123 更新时间:2023-11-30 01:49:55 29 4
gpt4 key购买 nike

我正在尝试删除 Canvas 的内容而不删除背景图像。我只想从 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);
}
}

这就是我将图像设置为 Canvas 的方式

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();
}

从源(图库)加载图像并将其设置为 Canvas 上的背景(作为位图)

最佳答案

你可以尝试这样修改DrawingViewsetImage:

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) 时将位图设置为背景,则只会删除在 Canvas 上绘制的内容。

而且你不需要多个 DrawingView 而只需要一个。

关于Android Eraser删除背景图像如何避免它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33214165/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com