Android - Get color from ImageView with ColorFIlter used for drawing

I am trying to get an ImageView color in a GridView that previously applied a color filter. I work on Nexus 4 with 4.2.2

I am changing the color of the ImageView as follows. The actual color of the image is white before applying the filter.

Drawable myIcon = getResources().getDrawable(R.drawable.bufferbuildercolor3); myIcon.setColorFilter(new PorterDuffColorFilter(Color.rgb(Color.red(color),Color.green(color), Color.blue(color)),PorterDuff.Mode.MULTIPLY)); 

Now this does not actually change the color of the source images that I want. It always stays white.

My problem is that I am trying to refer well to the color after I lost the data that I used to set it. I want to click on ImageView for a long time and get its color.

I found the code on the question below, but it does not work, because it always returns white (255,255,255), which is the color of the original image, but not the filter. How to get pixel color in Android?

 ImageView imageView = ((ImageView)v); Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap(); int pixel = bitmap.getPixel(x,y); int redValue = Color.red(pixel); int blueValue = Color.blue(pixel); int greenValue = Color.green(pixel); 

I tried things like below, but it just crashes when I call getColorFilter.

 ImageView image = (ImageView)findViewById(R.drawable.bufferbuildercolor3); ColorFilter test = image.getColorFilter(); 

I'm not sure what else to do, except maybe finding the X / Y of the actual position of the grid, and then getting the background color of the screens at that point, not the image. There seems to be an easier way to do this though?

+4
source share
1 answer

You can mark the ImageView ( or any View for that matter ) with the data you want later, For example, mark the view with the same Color that you use to create the PorterDuffColorFilter :

 Color tagColor = Color.rgb(Color.red(color),Color.green(color), Color.blue(color)); // tag imageview.setTag(tagColor); // get tag tagColor = (Color) imageview.getTag(); 

Obviously, the tag will receive garbage collection when the corresponding view is created.

Alternatively, the approach in your 3rd code snippet may work if you fix it. You are currently trying to inflate the view by looking for an available identifier - that doesn't make sense. The identifier should be in the form of R.id.imageview_id , R.id.imageview_id .:

 ImageView image = (ImageView) findViewById(R.id.imageview); ColorFilter test = image.getColorFilter(); 
+5
source

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


All Articles