文字生成Bitmap
private Bitmap fromText(String text) {
int dpSize = 12;//dp
float pixel =TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, dpSize, this.getResources().getDisplayMetrics());
Paint paint = new Paint();
paint.setTextSize(pixel);
paint.setTextAlign(Paint.Align.LEFT);
paint.setAlpha(77);
paint.setColor(Color.WHITE);
Paint.FontMetricsInt fm = paint.getFontMetricsInt();
int width = (int) paint.measureText(text);
int height = fm.descent - fm.ascent;
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawText(text, 0, fm.leading - fm.ascent, paint);
canvas.save();
return bitmap;
}
Bitmap保存文件
private void saveBitmap(Bitmap bitmap){
String cacheDir = context.getApplicationContext().getExternalCacheDir().getAbsolutePath();
String filePic = cacheDir+"/test.png"
FileOutputStream fos = new FileOutputStream(filePic);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
}