Change color without affecting anything previously drawn

前端 未结 2 1138
滥情空心
滥情空心 2020-12-04 00:31

I have the following CustomView I am using for painting in my app:

package com.test.testing;

import android.content.Context;
import android.gra         


        
相关标签:
2条回答
  • 2020-12-04 00:47

    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

    0 讨论(0)
  • 2020-12-04 01:02

    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!!

    0 讨论(0)
提交回复
热议问题