Getting background color in ImageView

I have a simple ImageView colorSquare object.

Directly set the color of the object on call.

 colorSquare.setBackgroundColor(color); 

But how can I do the opposite, i.e. get imageview background color?

+6
source share
2 answers

What you can do is

Get ColorDrawable from ImageView.

 ColorDrawable drawable = (ColorDrawable) colorSquare.getBackground(); 

now

drawable.getColor()

will give color u.

This will only work if you have the color set or else you get a ClassCastException

+14
source
 private int colorfindcolor(View v, int x, int y) { int offset = 1; // 3x3 Matrix int pixelsNumber = 0; int xImage = 0; int yImage = 0; Bitmap imageBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.palette_color); xImage = (int) (x * ((double) imageBitmap.getWidth() / (double) paletteImg .getWidth())); yImage = (int) (y * ((double) imageBitmap.getHeight() / (double) paletteImg .getHeight())); for (int i = xImage - offset; i <= xImage + offset; i++) { for (int j = yImage - offset; j <= yImage + offset; j++) { try { color = imageBitmap.getPixel(i, j); pixelsNumber += 1; } catch (Exception e) { // Log.w(TAG, "Error picking color!"); } } } return color; } 

Where x, y - event event.getX (), event.getX () events of the touch of the image

0
source

Source: https://habr.com/ru/post/908763/


All Articles