I'm trying to figure out the best way to get the pixel color value at a given point on a View. There are three ways that I write to the View:
I set a background image with View.setBackgroundDrawable(...).
I write text, draw lines, etc., with Canvas.drawText(...), Canvas.drawLine(...), etc., to a Bitmap-backed Canvas.
I draw child objects (sprites) by having them write to the Canvas passed to the View's onDraw(Canvas canvas) method.
Here is the onDraw() method from my class that extends View:
@Override
public void onDraw(Canvas canvas) {
// 1. Redraw the background image.
super.onDraw(canvas);
// 2. Redraw any text, lines, etc.
canvas.drawBitmap(bitmap, 0, 0, null);
// 3. Redraw the sprites.
for (Sprite sprite : sprites) {
sprite.onDraw(canvas);
}
}
What would be the best way to get a pixel's color value that would take into account all of those sources?
How about load the view to a bitmap (at some point after all your drawing/sprites etc is done), then get the pixel color from the bitmap?
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
return b;
}
then use getPixel(x,y) on the result?
http://developer.android.com/reference/android/graphics/Bitmap.html#getPixel%28int,%20int%29
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With