Android 将文字转换成图片并保存起来,搭建android开发环境实验报告

private Canvas mCanvas;

// 画笔--画图片

private Paint mPicturePaint = new Paint();

// 画笔--写字

private Paint mTextPaint = new Paint();

// 画笔--写运动数据

private Paint mDataPaint = new Paint();

/**

  • 根据图片和文字生成图片

  • @return

*/

private Bitmap drawBitmap() {

Bitmap bm_bg = BitmapFactory.decodeResource(getResources(),

R.drawable.ic_default_head_one);

// 得到图片的宽、高

int width_bg = bm_bg.getWidth();

int height_bg = bm_bg.getHeight();

// 创建一个你需要尺寸的Bitmap

mBitmap = Bitmap.createBitmap(width_bg, height_bg, Bitmap.Config.ARGB_8888);

// 用这个Bitmap生成一个Canvas,然后canvas就会把内容绘制到上面这个bitmap中

mCanvas = new Canvas(mBitmap);

// 绘制背景图片

mCanvas.drawBitmap(bm_bg, 0.0f, 0.0f, mPicturePaint);

// 绘制图片

Bitmap bm_head = BitmapFactory.decodeResource(getResources(),

R.drawable.ic_default_head_one);

// 得到图片的宽、高

int width_head = bm_head.getWidth();

int height_head = bm_head.getHeight();

// 绘制图片--保证其在水平方向居中

mCanvas.drawBitmap(bm_head, (width_bg - width_head) / 2, 0.0f,

mPicturePaint);

// 绘制文字

mTextPaint.setColor(Color.WHITE);// 白色画笔

mTextPaint.setTextSize(18.0f);// 设置字体大小

// 绘制文字

mDataPaint.setColor(Color.RED);// 红色画笔

mDataPaint.setTextSize(18.0f);// 设置字体大小

String distanceTextString = “1.蓝牙名称:我只是一个孩子”;

String distanceDataString = “2.可用次数:888”;

String distanceScalString = “3.已用次数:888”;

String startTime = “4.开始时间:2020-09-21 :16:16:16”;

String endTime = “5.结束时间:2020-09-21 :16:16:16”;

float distanceTextString_width = mTextPaint.measureText(

distanceTextString, 0, distanceTextString.length());

float distanceDataString_width = mDataPaint.measureText(

distanceDataString, 0, distanceDataString.length());

float distanceScalString_width = mTextPaint.measureText(

distanceScalString, 0, distanceScalString.length());

float x = (width_bg - distanceTextString_width

  • distanceDataString_width - distanceScalString_width) / 2;

Log.i(“info”,“xxxx”+x);

mCanvas.drawText(distanceTextString, width_head/6, height_head/5, mTextPaint);// 绘制文字

mCanvas.drawText(distanceDataString, width_head/6,

height_head/4+50, mDataPaint);// 绘制文字

// mCanvas.drawText(distanceScalString, x + distanceTextString_width

// + distanceDataString_width, height_head, mTextPaint);// 绘制文字

mCanvas.drawText(distanceScalString, width_head/6

, height_head/4+100, mTextPaint);// 绘制文字

mCanvas.drawText(startTime, width_head/6

, height_head/4+150, mTextPaint);// 绘制文字

mCanvas.drawText(endTime, width_head/6

, height_head/4+200, mTextPaint);// 绘制文字

// 保存绘图为本地图片

mCanvas.save();

mCanvas.restore();

saveImageToGallery(this,mBitmap,“share.png”);

FileInputStream fis = null;

推荐学习资料


  • 脑图
    Android 将文字转换成图片并保存起来,搭建android开发环境实验报告_第1张图片
    Android 将文字转换成图片并保存起来,搭建android开发环境实验报告_第2张图片
    Android 将文字转换成图片并保存起来,搭建android开发环境实验报告_第3张图片
    由于文章篇幅问题点击链接查看详细文章以及获取学习笔记:GitHub

4/Android-P7/blob/master/Android%E5%BC%80%E5%8F%91%E4%B8%8D%E4%BC%9A%E8%BF%99%E4%BA%9B%EF%BC%9F%E5%A6%82%E4%BD%95%E9%9D%A2%E8%AF%95%E6%8B%BF%E9%AB%98%E8%96%AA%EF%BC%81.md)**

你可能感兴趣的:(程序员,架构,移动开发,android)