Getting the pixel color value of a point on an Android View that includes a Bitmap-backed Canvas

16,846

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

Share:
16,846

Related videos on Youtube

Ellen Spertus
Author by

Ellen Spertus

I am a computer science professor at Mills College. I used to work at Google on projects such as App Inventor (which I still contribute to), Blockly, and the Hour of Code. I have done research in computer architecture, compilers, artificial intelligence, information retrieval, and data mining.

Updated on March 20, 2020

Comments

  • Ellen Spertus
    Ellen Spertus about 4 years

    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:

    1. I set a background image with View.setBackgroundDrawable(...).

    2. I write text, draw lines, etc., with Canvas.drawText(...), Canvas.drawLine(...), etc., to a Bitmap-backed Canvas.

    3. 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?

  • Christopher Souvey
    Christopher Souvey almost 13 years
    Probably better to just draw your entire view to a Bitmap using this method, then check the pixels and also draw the entire Bitmap to your real onDraw canvas. That way you don't have to run onDraw twice. Worth checking the efficiency of both
  • jkhouw1
    jkhouw1 almost 13 years
    thats a good point by Christopher - I was making the assumption that the drawing happened often, but the pixel color retrieval was occasional...
  • Ellen Spertus
    Ellen Spertus almost 13 years
    Thanks so much for the suggestions. My expectation is that drawing will be more common than pixel checking.
  • Ellen Spertus
    Ellen Spertus almost 13 years
    How does the suggested technique compare to using buildDrawingCache/getDrawingCache, which I just learned about?
  • cV2
    cV2 over 11 years
    tried all methods.. thanks to you now it finally works :=) jiihahaa :)
  • Simon Forsberg
    Simon Forsberg almost 11 years
    Is there any reason to use the values from getLayoutParams instead of v.getWidth() and v.getHeight()?
  • Vyacheslav
    Vyacheslav over 9 years
    I'm getting black screen after using such method
  • Kishore
    Kishore over 9 years
    v.getLayoutParams returning null :(