Search code examples
androidandroid-canvasdrawpixel

Android canvas change some pixels color


I have a Canvas object at start. I need to change color of some pixels depending on their current color. How can I do that in a proper way?

Details:

I have my own class extended from ImageView. In onDraw(Canvas canvas) method I draw something with third party class and have only Canvas object with result. I need after that change color of some pixels depending on their current color.


Solution

  • Assuming that you have android.graphics.Canvas object called canvas and X & Y are points where you want to change pixel, so here you go

    Call :

    canvas.drawPoint(X, Y, paint);
    

    Here is how you initalize Object of class android.graphics.Paint i.e paint

    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.WHITE);
    

    Search more on this Link to change multiple pixels at different positions, there are lot many functions that will help you achieve what you want. Best luck :-)