【问题标题】:Android Eraser removes background image how to avoid itAndroid Eraser 删除背景图片如何避免
【发布时间】: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:请看我提到的编辑。

标签: android canvas bitmap


【解决方案1】:

您可以尝试像这样修改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) 时将位图设置为背景,则只会擦除画布上绘制的内容。

你也不需要多个DrawingView,而只需要一个。

【讨论】:

  • 工作就像一个魅力,你能告诉我我错过了什么吗?或者如果这不可能,你能告诉你你做了什么不同吗?
  • 因为在您的setImage 中,位图是直接在画布上绘制的,当您启用擦除模式时,画布上的所有内容都会被擦除,您的图像也会被擦除。在我的答案setImage 中,位图是在视图的背景中绘制的,它不是画布的一部分,因此当您启用擦除模式时,它不会被擦除。
【解决方案2】:

我能够通过扩展 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);

【讨论】:

  • 没有任何改变,是的,你是对的,我正在按照上述教程进行操作
  • 如果没有人可以重现您的问题,那么没有人可以帮助您。您的问题没有相关代码。
  • 这个问题可以很容易地重现,即通过将图像设置为 Canvas 的背景,然后尝试擦除图像。我唯一想要的是我不希望图像被删除。现在我提出了我的解决方案并将其放在我的问题中。现在我正在寻找一些 1 来指出我正在做的事情的问题。或者提出一个我在这里找不到的更好的解决方案。
  • 在我提出的解决方案中,我已将您的背景绘制委托给 ImageView 类。您可以通过其setImageBitmap 方法设置位图。
猜你喜欢
  • 2012-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-16
  • 2012-02-10
  • 2021-02-06
相关资源
最近更新 更多