Android-通过将ColorFIlter应用于Drawable从ImageView获取颜色 [英] Android - Get Color From ImageView with ColorFIlter Applied to Drawable

查看:217
本文介绍了Android-通过将ColorFIlter应用于Drawable从ImageView获取颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在GridView中获取ImageView的颜色,而我之前也已经应用了滤色器.我在带有4.2.2的Nexus 4上运行

I'm trying to get the color of an ImageView within a GridView that I've applied a color filter too previously. I'm running on a Nexus 4 with 4.2.2

我正在像这样更改ImageView的颜色.在应用滤镜之前,图像的实际颜色是白色.

I'm altering the color of the ImageView like this. The actual color of the image is white before I apply a 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 doesn't actually alter the source images color which is what I want. That always remains white.

我的问题是在丢失用于设置颜色的数据后,我试图很好地引用颜色.我想长按ImageView并获取其颜色.

My problem is I'm trying to reference the color well after I've lost the data I used to set it. I want to long press on the ImageView and get its color.

我在下面的问题中找到了一些代码,但是它似乎无法正常工作,因为它始终返回白色(255,255,255),这是原始图像的颜色,而不是滤镜. 如何在Android中获取像素颜色?

I found some code at the below question, but it doesn't appear to work as it always returns white (255,255,255), which IS the color of the original image, but not the filter. How to Get Pixel Colour 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);

我尝试了以下类似的操作,但是当我调用getColorFilter时,它只是崩溃了.

I've tried stuff like the below, but it just crashes when I call getColorFilter.

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

除了可能找到实际网格位置的X/Y,然后在那一点而不是图像上获取屏幕背景颜色之外,我不确定该怎么做.似乎应该有一种更简单的方法来做到这一点?

I'm not sure what else to do, except maybe find the X/Y of the actual grid position, and then get the screens background color at that point rather than the images. It seems like there should be an easier way to do this though?

推荐答案

您可以标记ImageView(

You could tag the ImageView (or any View for that matter) with the data you're interested in retrieving later on. For example, tag the view with the same Color that you use to construct 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 get garbage collected when the related view does.

或者,如果您对其进行修复,则第3个代码段中的方法实际上可能会起作用.当前,您正在尝试通过查找 drawable id来扩大视图的显示范围-这没有任何意义. ID的格式应为R.id.imageview_id,即:

Alternatively, the approach in your 3rd code snippet might actually work if you fix it up. Currently you're trying to inflate a view by looking for a drawable id - that doesn't make sense. The id should be in the form of R.id.imageview_id, i.e.:

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

这篇关于Android-通过将ColorFIlter应用于Drawable从ImageView获取颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆