Change color without affecting anything previously drawn

时光总嘲笑我的痴心妄想 提交于 2019-11-27 15:51:41

You're adding to a single path with each TouchEvent. Then the Path is drawn using the current value of the Paint's color. That's why you are seeing everything drawing in a single color. You would need to create a separate Path and color for each color change and then draw them in sequence, changing the Paint's color for each drawPath() call

I don't think so. It's not that bad to break out the paths.

List<Pair<Path, Integer>> path_color_list = new ArrayList<Pair<Path,Integer>>()

then each time you change the color. Take the current path and view.paint.getColor and save it to your list.

path_color_list.add( new Pair.create(path, view.paint.getColor());
path = new Path();

then in your draw() iterate through the path_color_list, setting the new paint color each time

for (Pair<Path,Integer> path_clr : path_color_list ){
   paint.setColor(path_clr.second);
  canvas.drawPath( path_clr.first, paint);
}

followed with the last drawPath() that you have

Just do one thing always create new instance of your view class view1.paint.setColor(Color.LTGRAY);, then layout.addView(view1); means create new view it will save prevoius one also, its work for me. Very simple!!

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