Reputation: 10686
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.
Upvotes: 8
Views: 13005
Reputation: 1855
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 :-)
Upvotes: 4
Reputation: 1716
I recommend looking at Faster way to set a (PNG) bitmap color instead of pixel by pixel. It has code to get and set bitmap colors pixel by pixel (in the question), as well as a suggestion for an alternative to a pixel by pixel approach (in the answer). Also possibly useful: Explanation of the method getPixels for a Bitmap in Android.
Upvotes: 0
Reputation: 6368
There's probably a dozen ways to do this. If you want to do the Canvas approach, there's a way to draw to a Bitmap object. You can then draw the object to another Canvas. The Bitmap object may also have functions to modify pixels.
Bitmap also lets you get a copy into a buffer, and if you know about how pixels are stored, that would be a very fast way of image manipulation. I'm not sure if Canvas itself has that
Upvotes: 0