Regarding Android Paint drawing color

前端 未结 1 755
臣服心动
臣服心动 2020-12-10 17:47

DrawView.java

public class DrawView extends View implements OnTouchListener {
private Canvas mCanvas;
private Path mPath;
public Paint mPaint;
ArrayList

相关标签:
1条回答
  • 2020-12-10 18:16

    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.

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