获取一张文字位图

来源:互联网 发布:西北市政设计院知乎 编辑:程序博客网 时间:2024/04/29 16:40
/** * 获取一张文字位图 * @param context 上下文 * @param text 文字 * @param textColor 文字颜色 * @param textSize 文字大小 * @param leftBitmap 可以在文字的左边放置一张图片 * @return 文字位图 */public static Bitmap getTextBitmap(Context context, String text, int textColor, float textSize, Bitmap leftBitmap){   //创建并初始化画笔   Paint paint = new Paint();   paint.setColor(textColor);   paint.setTextSize(textSize);   paint.setAntiAlias(true);//去除锯齿   paint.setFilterBitmap(true);//对文字进行滤波处理,增强绘制效果      //计算要绘制的文字的宽和高   float textWidth = getTextWidth(paint, text);   float textHeight = getTextHeight(paint);   //计算图片的宽高   int newBimapWidth = textWidth % 1==0?(int)textWidth:(int)textWidth + 1;   int newBimapHeight = textHeight % 1==0?(int)textHeight:(int)textHeight + 1;      if(leftBitmap != null){      newBimapWidth += leftBitmap.getWidth();      newBimapHeight = leftBitmap.getHeight() > newBimapHeight?leftBitmap.getHeight():newBimapHeight;   }      //先创建一张空白图片,然后在其上面绘制文字   Bitmap bitmap = Bitmap.createBitmap(newBimapWidth, newBimapHeight, Config.ARGB_8888);   Canvas canvas = new Canvas(bitmap);   if(leftBitmap != null){      canvas.drawBitmap(leftBitmap, 0, (newBimapHeight - leftBitmap.getHeight())/2, paint);      canvas.drawText(text, leftBitmap.getWidth(), (newBimapHeight - textHeight)/2 + getTextLeading(paint), paint);   }else{      canvas.drawText(text, 0, (newBimapHeight - textHeight)/2 + getTextLeading(paint), paint);   }   canvas.save();      return bitmap;}/** * 获取一张文字位图 * @param context 上下文 * @param text 文字 * @param textColor 文字颜色 * @param textSize 文字大小 * @return 文字位图 */public static Bitmap getTextBitmap(Context context, String text, int textColor, float textSize){   return getTextBitmap(context, text, textColor, textSize, null);}
0 0