android 服务器获取bitmap ,canvas添加文案,添加二维码,合成分享图片

做个标记,方便要实现此功能的朋友借鉴,注意图片显示使用自适应fitxy,imageView按照UI给的底图比例动态设置一下宽高,否则屏幕适配显示会异常,分享出去是正常的;


原图


合成后分享的图片

//绘图

private static bitmap setTextToImgForTeacher(Context context, Bundle bundle,int position){

InviteUserBean inviteUserBean = (InviteUserBean) bundle.getSerializable("inviteUserBean");

  ArrayList posters = bundle.getStringArrayList("posters");

  Bitmap bitmap =base64ToBitmap(posters.get(position));

  InviteUserBean.UserDetailsitemBean userDetailsitemBean = inviteUserBean.userDetails.get(position);

  Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());// 创建一个新的和SRC长度宽度一样的位图

  Canvas canvas =new Canvas(newBitmap);

  canvas.drawBitmap(bitmap, 0,0, null);

  //合并二维码 右下角

  Bitmap qrBitmap =base64ToBitmap(inviteUserBean.picQRCode);

  // Matrix类进行图片处理(缩小或者旋转)

  Matrix matrix1 =new Matrix();

  // 缩小

  matrix1.postScale(0.4f, 0.4f);

  // 生成新的二维码图片

  Bitmap newQrBitmap = Bitmap.createBitmap(qrBitmap, 0, 0, qrBitmap.getWidth(), qrBitmap.getHeight(), matrix1, true);

  // 将二维码图片绘制在右下角

  canvas.drawBitmap(newQrBitmap, bitmap.getWidth()-newQrBitmap.getWidth()-20,bitmap.getHeight()-newQrBitmap.getHeight()-30, null);

  Paint paintSchoolName =new Paint();

  Paint paintInviteCode =new Paint();

  Paint paintTeacherName =new Paint();

  // 抗锯齿

  paintSchoolName.setAntiAlias(true);

  paintInviteCode.setAntiAlias(true);

  paintTeacherName.setAntiAlias(true);

  // 防抖动

  paintSchoolName.setDither(true);

  paintInviteCode.setDither(true);

  paintTeacherName.setDither(true);

  paintSchoolName.setColor(Color.parseColor("#ffffff"));

  paintInviteCode.setColor(Color.parseColor("#ffffff"));

  paintTeacherName.setColor(Color.parseColor("#ffffff"));

  paintSchoolName.setTextSize(28);

  paintInviteCode.setTextSize(40);

  paintTeacherName.setTextSize(22);

  // 文字宽

  float schoolNameWidth = paintSchoolName.measureText(userDetailsitemBean.schoolName);

  float teacherNameWidth = paintTeacherName.measureText(userDetailsitemBean.userName+"老师邀请您使用");

  //绘制邀请码

  canvas.drawText("邀请码:"+inviteUserBean.inviteCode,40,70, paintInviteCode);

  //居中绘制学校名称,及教师名称

  canvas.translate(bitmap.getWidth() /2, 0);

  canvas.drawText(userDetailsitemBean.schoolName,-schoolNameWidth/2,140, paintSchoolName);

  canvas.drawText(userDetailsitemBean.userName+"老师邀请您使用",-teacherNameWidth/2,180, paintTeacherName);

  return newBitmap;

}



/**

* base64转为bitmap

* @param base64Data

* @return

*/

public static Bitmapbase64ToBitmap(String base64Data) {

byte[] bytes = Base64.decode(base64Data, Base64.DEFAULT);

  BitmapFactory.Options options =new BitmapFactory.Options();

  TypedValue value=new TypedValue();

  options.inTargetDensity = value.density;

  options.inScaled =false;//options.inScaled = false; 不因屏幕大小对图像进行放缩

  return BitmapFactory.decodeByteArray(bytes, 0, bytes.length ,options);

}


//友盟分享图片

private void share(SHARE_MEDIA platform) {

if (!ShareUtil.isWeixinAvilible(getApplication())) {

ToastUtil.show("请先安装微信客户端");

return;

    }

UMImage umImage =new UMImage(this, selectBitmap);

    umImage.setThumb(umImage);

    new ShareAction(this)

.setPlatform(platform)

.withMedia(umImage)

.share();

}

你可能感兴趣的:(android 服务器获取bitmap ,canvas添加文案,添加二维码,合成分享图片)