【发布时间】:2017-02-20 21:27:57
【问题描述】:
我正在尝试将半透明橡皮刷写入我正在开发的应用程序中,但发现根据其实现方式会出现两个问题。希望有一个我可以指出的解决方法。
代码示例位于 mono droid / xamarin 中
所以,通常情况下,我有一个在绘图画布上绘制路径的设置。我的视图的OnDraw 方法绘制它的基本位图,然后在其上绘制我的路径。
// Canvas is set up elsewhere
canvasBitmap = Bitmap.CreateBitmap(w, h, Bitmap.Config.Argb8888);
DrawCanvas.SetBitmap(canvasBitmap);
protected override void OnDraw(Canvas canvas)
{
base.OnDraw(canvas);
// Draw the saved canvas bitmap
canvas.DrawBitmap(canvasBitmap, 0, 0, canvasPaint);
// On top of that draw the current path
canvas.DrawPath(DrawPath, DrawPaint);
}
public bool OnTouch(View view, MotionEvent ev)
{
float touchX = ev.GetX();
float touchY = ev.GetY();
switch (ev.Action)
{
case MotionEventActions.Down:
if (Editable)
{
// I've removed a bunch of code here but it basically
// moves the path start to the touch point and sets
// up the brush to start erasing. It does set the
// porterduff mode to dstOur
DrawPaint.SetMaskFilter(null);
DrawPaint.SetXfermode( new PorterDuffXfermode(PorterDuff.Mode.DstOut));
DrawPath.MoveTo(touchX, touchY);
} else { return false; }
break;
case MotionEventActions.Move:
// Just draws a line to the new touch coordinates
DrawPath.LineTo(touchX, touchY);
break;
case MotionEventActions.Up:
case MotionEventActions.Cancel:
// saves some data about the path and draws to the drawing canvas.
DrawCanvas.DrawPath(DrawPath, DrawPaint);
// Recycle it
DrawPath.Reset();
break;
}
return true;
}
所以有两种方法可以在绘制橡皮擦线时绘制它。第一种是在绘图画布上绘图(写入canvasBitmap),第二种是直接在OnDraw 提供的canvas 上绘图。
我的问题如下:
如果我在DrawCanvas 上绘图,我会得到一种堆叠效果,擦除的线条会逐渐变得越来越透明。这是可以理解的,因为在每个OnDraw 循环期间,橡皮擦路径都被烘焙到canvasBitmap 中。
如果我直接在OnDraw 提供的canvas 上绘图,我会遇到“black line”问题,这也是有一定道理的,因为它会擦除什么都没有。当在Up 事件中将路径绘制到canvasBitmap 时,一切看起来都正确,并且有一条漂亮的半透明橡皮擦线。
理想情况下,对于“black line”问题,我不知道有一个不同的解决方案,但我找不到任何东西。我还尝试考虑缓存canvasBitmap 的方法,并且在绘制事件正在进行时使用它而不是Move 事件绘制到的活动副本。
有没有人可以看一下可能会提出解决方案的东西? Java 很好,因为它很容易移植到 c#。
提前非常感谢! 奈杰尔
【问题讨论】: