gpt4 book ai didi

java - 为 Android 绘图应用程序创建橡皮擦

转载 作者:行者123 更新时间:2023-12-01 12:39:39 25 4
gpt4 key购买 nike

我正在开发一个绘图应用程序,并且即将发布,但我在应用程序的橡皮擦部分遇到问题。我有 2 个主屏幕( fragment ),其中一个只是一 block 空白的白色 Canvas ,用户可以在上面绘制一些选项等。另一个是笔记 fragment 。这个笔记 fragment 看起来像笔记本纸。因此,为了删除绘图 fragment ,我可以简单地使用 Canvas 的背景,用户不会知道其中的区别。在笔记 fragment 上,虽然我不能这样做,因为我需要保持背景完好无损。我研究了 PorterDuffer 模式并尝试了清晰版本并尝试绘制到单独的位图上,但没有任何效果。如果有一种方法可以控制在什么之上绘制什么,那么这将会很有用。我愿意接受任何建议,但我似乎无法做任何事情。

我还尝试过在删除之前启用绘图缓存,但这不起作用。此外,我尝试了硬件启用,这使得我的自定义 View 表现得很奇怪。下面是相关代码。我的绘制方法经历了很多路径,因为我正在查询它们以便允许一些其他功能。

@Override
protected void onDraw(Canvas canvas) {

//draw the backgroumd type
if(mDrawBackground) {
//draw the background
//if the bitmap is not null draw it as the background, otherwise we are in a note view
if(mBackgroundBitmap != null) {
canvas.drawBitmap(mBackgroundBitmap, 0, 0, backPaint);
} else {
drawBackgroundType(mBackgroundType, canvas);
}
}

for (int i = 0; i < paths.size(); i++ ) {
//Log.i("DRAW", "On draw: " + i);
//draw each previous path.
mDrawPaint.setStrokeWidth(strokeSizes.get(i));
mDrawPaint.setColor(colors.get(i));
canvas.drawPath(paths.get(i), mDrawPaint);
}
//set paint attributes to the current value
mDrawPaint.setStrokeWidth(strokeSize);
mDrawPaint.setColor(mDrawColor);
canvas.drawPath(mPath, mDrawPaint);

}

以及我的绘制背景方法

/**
* Method that actually draws the notebook paper background
* @param canvas the {@code Canvas} to draw on.
*/
private void drawNoteBookPaperBackground(Canvas canvas) {

//create bitmap for the background and a temporary canvas.
mBackgroundBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBackgroundBitmap);
//set the color to white.
mBackgroundBitmap.eraseColor(Color.WHITE);

//get the height and width of the view minus padding.
int height = getHeight() - getPaddingTop() - getPaddingBottom();
int width = getWidth() - getPaddingLeft() - getPaddingRight();

//figure out how many lines we can draw given a certain line width.
int lineWidth = 50;
int numOfLines = Math.round(height / lineWidth);
Log.i("DRAWVIEW", "" + numOfLines);


//iterate through the number of lines and draw them.
for(int i = 0; i < numOfLines * lineWidth; i+=lineWidth) {
mCanvas.drawLine(0+getPaddingLeft(), i+getPaddingTop(), width, i+getPaddingTop(), mNoteBookPaperLinePaint);
}

//now we need to draw the vertical lines on the left side of the view.
float startPoint = 30;
//set the color to be red.
mNoteBookPaperLinePaint.setColor(getResources().getColor(R.color.notebook_paper_vertical_line_color));

//draw first line
mCanvas.drawLine(startPoint, 0, startPoint, getHeight(), mNoteBookPaperLinePaint);
//space the second line next to the first one.
startPoint+=20;
//draw the second line
mCanvas.drawLine(startPoint, 0, startPoint, getHeight(), mNoteBookPaperLinePaint);

//reset the paint color.
mNoteBookPaperLinePaint.setColor(getResources().getColor(R.color.notebook_paper_horizontal_line_color));

canvas.drawBitmap(mBackgroundBitmap, 0, 0, backPaint);
}

最佳答案

对于所有看到这个问题的人,我想我应该补充一下我是如何解决这个问题的。我正在做的是在自定义 View 中创建背景位图,然后将其传递到我的托管 fragment 。然后, fragment 将该位图设置为其包含 View 组的背景,以便当我使用 PorterDuff.CLEAR Xfermode 时,绘制的路径将被清除,但 fragment 父级中的背景保持不变。

关于java - 为 Android 绘图应用程序创建橡皮擦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25233964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com