How to get pixel color in Android?

后端 未结 3 1049
一向
一向 2021-01-23 18:26

I have set a PNG image(image having transparent background) as button background, When I touch the button it shows me X-coordinate and Y-coordinate of the button’s touched posit

相关标签:
3条回答
  • 2021-01-23 19:10

    Try this

     Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.picture);
        int color=bitmap.getPixel(x_coordinate, y_coordinate);
    

    Use this color in your if statements to do the required operations

    0 讨论(0)
  • 2021-01-23 19:16

    First, you can get the Bitmap with the BitmapFactory like this:
    Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic1);

    Now you have the Bitmap. On this Bitmap, you can call the function getPixel(int x, int y) to get the Color of this Pixel.
    I guess you then can get the alpha from that Color..

    See the following links for further information:

    • Bitmap Example
    • getPixel(..)
    • Android Color
    0 讨论(0)
  • 2021-01-23 19:30

    Try this

    Drawable drawable = button.getBackground();
    Bitmap bmp = ((BitmapDrawable) drawable).getBitmap();
    int color = bmp.getPixel(x_coordinate, y_coordinate);
    
    0 讨论(0)
提交回复
热议问题