简体   繁体   English

从Android TextView获取像素颜色

[英]Getting pixel colors from Android TextView

I am trying to get coordinates for a font in Android TextView. 我正在尝试获取Android TextView中字体的坐标。

To get this, I am planning to type in TextView, go horizontally and vertically throughout the TextView, and see which pixels are black and which pixels are white. 为此,我计划键入TextView,在整个TextView中水平和垂直移动,并查看哪些像素为黑色,哪些像素为白色。 Black is part of the text, white is not. 黑色是文本的一部分,白色不是文本的一部分。

Could you tell me how to distinguish in TextView which pixels are black and which pixels are white? 您能否告诉我如何在TextView中区分哪些像素是黑色和哪些像素是白色?

Thanks! 谢谢!

I would like to share my idea. 我想分享我的想法。 If you are planning to iterate textview pixels and match color, you first need to get bitmap from textview. 如果您打算迭代textview像素并匹配颜色,则首先需要从textview获取位图。

TextView textview = (TextView) findViewById(R.id.text_title);
textview.setDrawingCacheEnabled(true);
textview.buildDrawingCache();        
Bitmap bitmap = textview.getDrawingCache();

After that, you can simply check the pixel color by following method: 之后,您可以通过以下方法简单地检查像素颜色:

for(int x = 1; x <= width; x++)
   for(int y = 1; y <= height; y++) {

int pixel = bitmap.getPixel(x,y);
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);

//now check if black or white color
if(Color.argb(1,redValue, greenValue , blueValue) == Color.BLACK) {
//do work for black pixel
}
else {
//white pixel
}
}

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM