美文网首页
Android 生成图片-模板篇

Android 生成图片-模板篇

作者: 我才是张雷 | 来源:发表于2018-09-19 18:34 被阅读0次

最近有个需求,给一个模板图片,生成每个店铺自己的收款码,模板大小固定
图片长这样


付款码模板.png

生成这样


火锅店的付款码.jpg

步骤
1.加载模板图片,id是图片id

 Bitmap bgBitmap = decodeResource(getResources(), id);

2.用模板生长一张画布

Canvas canvas = new Canvas(bgBitmap);

2.创建一个画笔画上店铺名称

Paint  textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint.setColor(Color.BLACK);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setTextSize(txtSize);
textPaint.setAntiAlias(true);
textPaint.setTextAlign(Paint.Align.CENTER);
canvas.drawText("店铺名称", x, y,textPaint);

3.画上二维码

Bitmap qrBitmap =QRCodeUtil.createQRCodeBitmapMargin(s, qrCodeSize, 0);
Rect mSrcRect = new Rect(left, imgMarginTop, left + qrCodeWidth, bottom);
canvas.drawBitmap(qrBitmap, null, mSrcRect, new Paint(Paint.ANTI_ALIAS_FLAG));

注意这里要canvasdrawBitmap(@NonNull Bitmap bitmap, @Nullable Rect src, @NonNull Rect dst, @Nullable Paint paint)方法,单纯的用drawBitmap(@NonNull Bitmap bitmap, float left, float top, @Nullable Paint paint)方法画出的二维码在不同的机型上大小不同,原因不知,希望有知道的大神指点下

4.大功告成,保存到本地吧

FileUtils.saveFile(mContext, bgBitmap, "Order365收款码.jpg", filePath);

相关文章

网友评论

      本文标题:Android 生成图片-模板篇

      本文链接:https://www.haomeiwen.com/subject/tyzinftx.html