当前位置: 代码迷 >> Android >> Android橡皮擦删除背景图片如何避免
  详细解决方案

Android橡皮擦删除背景图片如何避免

热度:32   发布时间:2023-08-04 09:42:15.0

我试图在不擦除背景图像的情况下擦除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();
}

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

你可以尝试修改setImageDrawingView是这样的:

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

而且,您不需要多个DrawingView而只需要一个。

我可以通过扩展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);
}

通过ImageView的方法设置“背景”:

drawView.setImageBitmap(bitmap);
  相关解决方案