public static Bitmap drawTextToBitmap(Context context, Bitmap bitmap, String... strings) {
//根据图片大小和屏幕大小将图片缩放
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int screenWidth = metrics.widthPixels;
int screenHeight = metrics.heightPixels;
int bitmapWidth = bitmap.getWidth();
int bitmapHeight = bitmap.getHeight();
Matrix matrix = new Matrix();
float widthScale = (float)screenWidth/bitmapWidth; //一定要加上float不然createBitmap的时候会报错
float heightScale = (float)screenHeight/bitmapHeight;
matrix.postScale(widthScale, heightScale);
Bitmap src = Bitmap.createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, true);
Bitmap result = src.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setTextSize(40);
paint.setColor(Color.WHITE);
int textHeight = 0;
for (int i=0; i
String str = strings[i];
Rect bounds = new Rect();
paint.getTextBounds(str, 0, str.length(), bounds);
if (i == 0) {
textHeight = bounds.height();
}
canvas.drawText(str, 20, result.getHeight() - textHeight*(i+1) - 40*(i+1), paint);
}
canvas.save();
canvas.restore();
bitmap.recycle();
return result;
}