Is there a way to directly access the color of a pixel of the display in Android?

非 Y 不嫁゛ 提交于 2019-12-23 04:03:58

问题


Do you know if there is a way to get, at some point during the execution of an application, an information of the colors of all the pixels (or one pixel, it's indifferent)? The thing I need to get the average color of the entire display at a particular instant. I only found solutions that use screenshots of the display, but this thing takes time for the computation. Practically, even if there is, I would do something like:

Display display = ((WindowManager)getSystemService(this.WINDOW_SERVICE)).getDefaultDisplay();
int pixel = display.getPixel(x,y);
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);

回答1:


You can get your root view as a bitmap and then sample pixels.

For example:

View myLayout = findViewById(R.id.myRootLayout);
myLayout.setDrawingCacheEnabled(true);
Bitmap bitmap = myLayout.getDrawingCache();
myLayout.setDrawingCacheEnabled(false);

//sample a pixel
int color = bitmap.getPixel(20,20);


来源:https://stackoverflow.com/questions/22444534/is-there-a-way-to-directly-access-the-color-of-a-pixel-of-the-display-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!