android中向bitmap里写入文字
public static Bitmap drawText2Bitmap(String text, int imgResourceId, Context mContext) { try { Resources resources = mContext.getResources(); float scale = resources.getDisplayMetrics().density; Bitmap bitmap = BitmapFactory.decodeResource(resources, imgResourceId); android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig(); // set default bitmap config if none if (bitmapConfig == null) bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888; // resource bitmaps are imutable, so we need to convert it to mutable one bitmap = bitmap.copy(bitmapConfig, true); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); // new antialised Paint paint.setColor(Color.rgb(110, 110, 110)); // text color - #3D3D3D paint.setTextSize((int)(12 * scale)); // text size in pixels paint.setShadowLayer(1f, 0f, 1f, Color.DKGRAY); // text shadow // draw text to the Canvas center Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), bounds); int x = (bitmap.getWidth() - bounds.width()) / 6; int y = (bitmap.getHeight() + bounds.height()) / 5; canvas.drawText(text, x * scale, y * scale, paint); return bitmap; } catch (Exception e) { return null; } }
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
支付宝扫一扫捐赠
支付宝扫一扫捐赠
微信公众号: 共鸣圈
欢迎讨论,邮件: 924948$qq.com 请把$改成@
QQ群:263132197
QQ: 924948