private Bitmap testAsBitMap(View view) {
TextPaint textPaint = new TextPaint();
textPaint.setColor(Color.CYAN);
// StaticLayout layout = new StaticLayout(s,textPaint,50,
// Layout.Alignment.ALIGN_NORMAL, 1.3f, 0.0f, true);
// Bitmap bitmap = Bitmap.createBitmap(layout.getWidth() + 20,
// layout.getHeight() + 20, Bitmap.Config.ARGB_8888);
// Canvas canvas = new Canvas(bitmap);
// canvas.translate(10, 10);
// canvas.drawColor(Color.WHITE);
//
// layout.draw(canvas);
// Log.d("textAsBitmap",
// String.format("1:%d %d", layout.getWidth(), layout.getHeight()));
// Log.d("1111111","#######");
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap=view.getDrawingCache();
Canvas c=new Canvas(bitmap);
c.drawColor(Color.WHITE);
int w=view.getWidth();
int h=view.getHeight();
/**?如果不设置canvas画布为白色,则生成透明?*/
view.layout(0,0, w,h);
view.draw(c);
return( convertTOBlackWrite(bitmap));
}
}
private static Bitmap convertTOBlackWrite(Bitmap bmp) {
int width = bmp.getWidth();
int height = bmp.getHeight();
int[] pixels = new int[width * height];
bmp.getPixels(pixels, 0, width, 0, 0, width, height);
int alpha = 0xFF << 24;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int grey = pixels[width * i + j];
// 分离三原色
int red = ((grey & 0x00FF0000) >> 16);
int green = ((grey & 0x0000FF00) >> 8);
int blue = (grey & 0x000000FF);
// 转化成灰度像素
grey = (int) (red * 0.3 + green * 0.59 + blue * 0.11);
grey = alpha | (grey << 16) | (grey << 8) | grey;
pixels[width * i + j] = grey;
}
}
// 新建图片
Bitmap newbmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
newbmp.setPixels(pixels, 0, width, 0, 0, width, height);
Bitmap resizeBmp = ThumbnailUtils.extractThumbnail(newbmp, width,
height);
return resizeBmp;
}
|