赞
踩
- public static Bitmap drawTextToRightBottom(Context context, Bitmap bitmap, String text, int size, int color, int paddingRight, int paddingBottom) {
- Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
- paint.setColor(color);
- paint.setTextSize(dp2px(context, size));
- Rect bounds = new Rect();
- paint.getTextBounds(text, 0, text.length(), bounds);
- return drawTextToBitmap(context, bitmap, text, paint, bounds,
- bitmap.getWidth() - bounds.width() - dp2px(context, paddingRight),
- bitmap.getHeight() - dp2px(context, paddingBottom));
- }
-
-
- public static int dp2px(Context context, float dp) {
- final float scale = context.getResources().getDisplayMetrics().density;
- return (int) (dp * scale + 0.5f);
- }
-
-
- private static Bitmap drawTextToBitmap(Context context, Bitmap bitmap, String text, Paint paint, Rect bounds, int paddingLeft, int paddingTop) {
- android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
-
- paint.setDither(true); // 获取跟清晰的图像采样
- paint.setFilterBitmap(true);
- if (bitmapConfig == null) {
- bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
- }
- bitmap = bitmap.copy(bitmapConfig, true);
- Canvas canvas = new Canvas(bitmap);
- canvas.drawText(text, paddingLeft, paddingTop, paint);
- return bitmap;
- }
以Bitmap处理为例,在使用的地方直接调用:
- //得到可用的bitmap
- bmp = drawTextToRightBottom(this, bmp1,"123456789 ABC Test", 50, getColor(android.R.color.holo_red_dark), 100, 300);
-
- ……//将bitmap直接设置给view显示或者转成jpg文件输出即可。
可以控制要显示的文字内容、文字大小、文字颜色、并通过右边距和上边距控制位置。
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。