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

前端 未结 1 670
轻奢々
轻奢々 2020-11-30 10:00

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 back

相关标签:
1条回答
  • 2020-11-30 10:33

    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

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