Android - 从 ImageView 获取颜色并将 ColorFIlter 应用于 Drawable

标签 android android-imageview android-gridview android-drawable

我试图在我之前也应用过滤色器的 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/

相关文章:

android - 在 ImageView 的 setImageBitmap 之后 onmeasure 方法上的时间太多

android - 如何检测语音

android - 如何将 Canvas 上的点保持在屏幕上的固定点,即使 Canvas 缩放时也是如此? - 安卓

android - 如何从设备中删除 ldpi 支持?

android - 在 android 的原生 imageViewer 中查看图像

Android - GridView 中 Assets 文件夹中的图像

android - 在 GridView 中加载异步图像 (Android)

android - 默认情况下关注 gridview 项目 - android

c# - 用户无法登录 Google Play 服务

android - 如何将数据从 fragment 发送到 Activity