gpt4 book ai didi

java - 如何在 ImageView android上绘制文本

转载 作者:行者123 更新时间:2023-12-02 05:54:00 26 4
gpt4 key购买 nike

我有一个应用程序可以计算手机使用时间,您可以在社交网络上分享它,而不是普通的 gettext() 我想将结果放在图像上并将其保存到手机上。如何创建可保存到手机的包含自定义文本的图像?

最佳答案

您想要做的是绘制链接到位图的 Canvas ,并保存位图。下面是一些代码,您应该能够将它们串在一起以使其工作。请注意,您仍然必须在 getBitmap() 函数中将绘画添加到 Canvas 。

private Bitmap getBitmap() {
Bitmap bitmap = Bitmap.createBitmap(mPieToss.getWidth(),
mPieToss.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
// Draw things to your canvas. They will be included as your BitMap, which is saved later on


return bitmap;
}

public void save() {
Bitmap bitmap = getBitmap();

File path = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());

String filename = "Imagen_" + timestamp + ".jpg";
File file = new File(path, filename);
FileOutputStream stream;
// This can fail if the external storage is mounted via USB
try {
stream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, stream);
stream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

mUri = Uri.fromFile(file);

bitmap.recycle();
}

关于java - 如何在 ImageView android上绘制文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23274262/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com