我试图在我之前也应用过滤色器的 GridView 中获取 ImageView 的颜色。我在 Nexus 4 上运行 4.2.2
我正在像这样改变 ImageView 的颜色。在我应用滤镜之前,图像的实际颜色是白色。
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));
现在这实际上并没有改变我想要的源图像颜色。那总是白色的。
我的问题是,在丢失用于设置颜色的数据后,我试图很好地引用颜色。我想长按 ImageView 并获取它的颜色。
我在下面的问题中找到了一些代码,但它似乎不起作用,因为它总是返回白色 (255,255,255),这是原始图像的颜色,而不是过滤器的颜色。 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 时它就崩溃了。
ImageView image = (ImageView)findViewById(R.drawable.bufferbuildercolor3);
ColorFilter test = image.getColorFilter();
我不确定还能做什么,除了可能找到实际网格位置的 X/Y,然后获取该点的屏幕背景颜色而不是图像。似乎应该有更简单的方法来做到这一点?
最佳答案
您可以在 ImageView
( or any View
for that matter ) 上标记您有兴趣稍后检索的数据。例如,使用与构造 PorterDuffColorFilter
相同的 Color
标记 View :
Color tagColor = Color.rgb(Color.red(color),Color.green(color), Color.blue(color));
// tag
imageview.setTag(tagColor);
// get tag
tagColor = (Color) imageview.getTag();
显然,当相关 View 执行时,标签将被垃圾回收。
或者,如果您修复了第三个代码 fragment 中的方法,它实际上可能会起作用。当前,您正在尝试通过查找 drawable id 来扩充 View - 这没有意义。 id应该是R.id.imageview_id
的形式,即:
ImageView image = (ImageView) findViewById(R.id.imageview);
ColorFilter test = image.getColorFilter();
关于Android - 从 ImageView 获取颜色并将 ColorFIlter 应用于 Drawable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16684289/