我想将动态字符串转换为Bitmap并将其保存在磁盘上。图像应该如下图所示,字符串周围有一个角。 我找不到直接将String转换为位图的方法,我尝试创建一个textView并将其转换为位图,但它无法正常工作。
Obs:我看到一些相似的问题,但他们真的没有解决这个问题。
public static String StringToBitMap(Context context, String name){
TextView textView = new TextView(context);
textView.setText(name);
try{
int w = textView.getWidth();
int h = textView.getHeight();
Bitmap bitmap = Bitmap.createBitmap(w, h,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);
Drawable bgDrawable = textView.getBackground();
if (bgDrawable!=null) {
bgDrawable.draw(canvas);
}
else {
canvas.drawColor(Color.WHITE);
}
textView.draw(canvas);
FileOutputStream out = null;
File file = null;
try {
String path = Environment.getExternalStorageDirectory().toString();
file = new File(path + "/Download/", "item.png");
out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
// PNG is a lossless format, the compression factor (100) is ignored
out.close();
MediaStore.Images.Media.insertImage(context.getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return file.getAbsolutePath();
}catch(Exception e){
e.getMessage();
return null;
}
}
答案 0 :(得分:0)
我刚刚找到解决方案:
public static String StringToBitMap(Context context, String name){
try{
// Create bitmap
Bitmap bitmap = Bitmap.createBitmap(250, 16,Bitmap.Config.ARGB_8888);
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(0xffffffff);
// new antialised Paint
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// text color - #3D3D3D
paint.setColor(Color.BLACK);
// text size in pixels
paint.setTextSize(12);
// draw text to the Canvas center
Rect bounds = new Rect();
paint.getTextBounds(name, 0, name.length(), bounds);
int x = (bitmap.getWidth() - bounds.width())/2;
int y = (bitmap.getHeight() + bounds.height())/2;
canvas.drawText(name, x, y, paint);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
paint.setStyle(Paint.Style.STROKE);
// draw corner to the Canvas
canvas.drawRect(x - 5, 0.0f, x + bounds.width() + 5, 16.0f, paint);
// save on disk
FileOutputStream out = null;
File file = null;
try {
String path = Environment.getExternalStorageDirectory().toString();
file = new File(path + "/Download/", "item.png");
out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
// PNG is a lossless format, the compression factor (100) is ignored
out.close();
MediaStore.Images.Media.insertImage(context.getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return file.getAbsolutePath();
}catch(Exception e){
e.getMessage();
return null;
}
}