实现橡皮擦在Android绘图应用程序 - 黑色的踪迹,然后透明 [英] Implementing an eraser in an Android drawing app - black trail and then transparent
问题描述
我有一个Android的应用程序绘制和我目前正在为一个真正的橡皮擦添加到它。之前,我刚刚用白色油漆的橡皮擦,但不会做了,因为现在我让背景颜色和图像。我做到这一点,我有透明画布下面的示意图。
I have an drawing app for Android and I am currently trying to add a real eraser to it. Before, I had just used white paint for an eraser, but that won't do anymore since now I allow background colors and images. I do this by having an image view underneath my transparent canvas.
这是我现在面临的问题是,每当我让我的橡皮擦,它吸引了坚实的黑色踪迹,而我有我的手指下来,但一旦我放开去透明。请参阅下面的屏幕截图:
The problem that I am facing is that whenever I enable my eraser, it draws a solid black trail while I have my finger down, but once I release it goes to transparent. See the screen shots below:
这是它的外观,而我的手指在屏幕上 - 一个纯黑色的线索
This is how it looks while my finger is on the screen - a solid black trail
这是什么样子的,一旦我删除了我的手指从屏幕上
This is what it looks like once I remove my finger from the screen
所以,好像我越来越近,但我无法找到设置的正确组合,以避免黑色的踪迹,而我的手指触摸,而擦除。这里有一些相关的code片段:
So, it seems like I am getting close, but I can't find the right combination of settings to avoid the black trail while my finger is touching while erasing. Here are some relevant code snippets:
的OnDraw
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.TRANSPARENT);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
canvas.drawPath(mPreviewPath, mPaint);
}
的onTouchEvent
@Override
public boolean onTouchEvent(MotionEvent event) {
float currentX = event.getX();
float currentY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touchStart(currentX, currentY);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touchMove(currentX, currentY);
invalidate();
break;
case MotionEvent.ACTION_UP:
touchUp(currentX, currentY);
invalidate();
break;
}
return true;
}
当前尝试橡皮擦设置
public void startEraser() {
mPaint.setAlpha(0);
mColor = Color.TRANSPARENT;
mPaint.setColor(Color.TRANSPARENT);
mPaint.setStrokeWidth(mBrushSize);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setMaskFilter(null);
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
mPaint.setAntiAlias(true);
}
有大约橡皮擦几个其他职位,但他们大多只是说使用 PorterDuff.Mode.CLEAR
, setMakFilter(空)
并应工作。就我而言,事实并非如此。不管我怎么努力,我先升后得到的黑色踪迹期望的结果我释放之后。
There are several other posts about erasers, but most of them just say to use PorterDuff.Mode.CLEAR
, setMakFilter(null)
and that that should work. In my case, it doesn't. No matter what I try, I get the black trail first and then the desired result only after I release.
我可以在必要时提供更多的code。
I can provide more code if necessary.
推荐答案
我可以建议你阅读<一个官方样片href="https://android.googlesource.com/platform/development/+/master/samples/ApiDemos/src/com/example/android/apis/graphics/FingerPaint.java">FingerPaint.java
它完全匹配您正试图在这里实现什么。
I could suggest you to read the official sample of FingerPaint.java
It exactly matches what you are trying to achieve here.
不显示,当你删除的内容,一起来看看在的OnDraw()
方法和 eraserMode
变量:
To not show the trail when you erase content, take a look at the onDraw()
method and the eraserMode
variable:
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(0xFFAAAAAA);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
if (!eraserMode) {
canvas.drawPath(mPath, mPaint);
}
}
boolean eraserMode = false;
@Override
public boolean onOptionsItemSelected(MenuItem item) {
eraserMode = false;
mPaint.setXfermode(null);
mPaint.setAlpha(0xFF);
switch (item.getItemId()) {
/*...*/
case ERASE_MENU_ID:
// Add this line
eraserMode = true;
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
return true;
/*...*/
}
return super.onOptionsItemSelected(item);
}
这篇关于实现橡皮擦在Android绘图应用程序 - 黑色的踪迹,然后透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!