Android:恢复基于 Canvas 的位图恶化?

发布于 2024-10-20 08:47:26 字数 1072 浏览 2 评论 0原文

我有一个绘图应用程序,用户可以用手指在屏幕上绘图。绘制发生在离屏位图上,然后在 onDraw() 中发布到屏幕上。

当用户通过呼叫或点击主页切换离开应用程序,然后返回到应用程序时,绘图屏幕将显示先前的绘图,但绘图边缘现在有伪影。骑车经过多个家->简历->首页->恢复循环会导致工件每次都变得更糟。五个周期后的结果请参见附图。

有人见过这个吗?知道为什么会发生这种情况吗?

谢谢

原图:

在此处输入图像描述

5 个周期后:

在此处输入图像描述

编辑:更多详细信息:

当用户触摸屏幕时,我拦截触摸并将它们存储为屏幕外位图 mBitmap 上的路径。路径是使用启用了 Paint.ANTI_ALIAS_FLAG 标志的 Paint 绘制的。在 onDraw() 中,我通过以下方式将它们写入屏幕:

@Override
protected void onDraw(Canvas canvas) {
  // wipe the canvas
  canvas.drawColor(0xffffffff);

  // draw the stored paths
  canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

  // draw any active paths
  if (mStrokePath != null) {
 canvas.drawPath(mStrokePath, mStrokePaint);
  }
}

其中 mBitmapPaint 定义为:

mBitmapPaint = new Paint(Paint.DITHER_FLAG);

编辑 2:好的,搞清楚了

我的问题是,在创建基于 Canvas 的视图的 Activity 的 onResume 中,我正在重新加载将形状复制到恢复的位图顶部的画布上,该位图已经具有形状,因此抗锯齿效果恶化。

I have a drawing app where the user can draw on the screen with their finger. The drawing happens to an off-screen bitmap, and is then posted to the screen in onDraw().

When the user is switched away from the application, via a call or by hitting home, then returns to the app, the drawing screen is shown with the previous drawing, except the drawings edges now have artifacts. Cycling through a number of home -> resume -> home -> resume cycles results in the artifacts getting worse each time. See attached images for the results after five cycles.

Has anyone seen this before? Any idea why this is happening?

Thanks

Original drawing:

enter image description here

After 5 Cycles:

enter image description here

EDIT: More details:

As the user touches the screen, I intercept the touches and store them as Path's on an offscreen Bitmap, mBitmap. The Paths are drawn with a Paint that has the flag Paint.ANTI_ALIAS_FLAG enabled. The in onDraw(), I write them to the screen via:

@Override
protected void onDraw(Canvas canvas) {
  // wipe the canvas
  canvas.drawColor(0xffffffff);

  // draw the stored paths
  canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

  // draw any active paths
  if (mStrokePath != null) {
 canvas.drawPath(mStrokePath, mStrokePaint);
  }
}

Where mBitmapPaint is defined as:

mBitmapPaint = new Paint(Paint.DITHER_FLAG);

EDIT 2: Ok, got it figured out

My issue was that in the onResume for the Activity that creates the Canvas-based View, I was reloading the shapes onto the canvas ON TOP of the restored Bitmap, which already had the shapes, thus the deterioration of the anti-aliasing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

意中人 2024-10-27 08:47:26

我以前从未为 Android 开发过,所以我不熟悉它的绘图系统,但对我来说,你的示例看起来像是原始图像已在其自身之上绘制了多次,突出了抗锯齿边缘。您有什么方法可以强制清除/擦除画布,然后在用户从主屏幕返回时重新绘制吗?

I've never developed for Android before so I'm not familiar with its drawing system, but to me, your example looks like the original image has been drawn on top of itself multiple times, accentuating antialias edges. Have you any way of forcibly clearing/wiping the canvas and then redrawing when the user returns from the home screen?

镜花水月 2024-10-27 08:47:26

考虑使用Paint.FILTER_BITMAP_FLAG。

Consider using Paint.FILTER_BITMAP_FLAG.

溺渁∝ 2024-10-27 08:47:26

根据已发布问题中的编辑 2 部分:

我的问题是,在创建基于画布的视图的活动的 onResume 中,我将形状重新加载到已恢复位图顶部的画布上,该位图已经具有形状,因此抗锯齿效果恶化。

因此,恢复的画布并不是真正的问题,而是我实现加载画布的方式才是问题。要标记它并关闭它。

Per the EDIT 2 section in the posted question:

My issue was that in the onResume for the Activity that creates the Canvas-based View, I was reloading the shapes onto the canvas ON TOP of the restored Bitmap, which already had the shapes, thus the deterioration of the anti-aliasing.

So, not really an issue with the restored Canvas being an issue, rather the way I implemented loading the canvas was the issue. Going to mark this and close it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文