Regarding Android Paint drawing color

一世执手 提交于 2019-11-28 11:27:41

To do this, you'd have to create a new Paint for every object drawn. This is because when the Canvas redraws, it references that same Paint object every time, so all paths will use this paint.

Firstly, I would change your paths array to contain both a Paint and a Path. You can achieve this using the Android type Pair.

ArrayList<Pair<Path, Paint>> paths = new ArrayList<Pair<Path, Paint>>();

You will also have to convert your undonePaths variable in this manner.

Then, in your touch_up() method, you need to add this new Paint object.

Paint newPaint = new Paint(mPaint); // Clones the mPaint object
paths.add(new Pair<Path, Paint>(mPath, newPaint));

Lastly, your loop has to be adjusted for this as well:

for (Pair<Path, Paint> p : paths) {
    canvas.drawPath(p.first, p.second);
}

This is quite memory intensive, so you will have to take good care to reset these items when they're no longer in use, but to have so many different colors, you must have all of these different Paint objects.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!