You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中如何在图片下方绘制文字?附代码求助

在Android中实现图片下方添加文字的方案

我看你现在的问题是能在图片上绘制文字,但没法在图片下方单独开辟区域来放文字——当前代码里创建的新Bitmap和原图片尺寸完全一致,所以文字只能叠加在图片底部,而不是真正在图片下面加区域。下面给你调整后的完整解决方案:

核心逻辑

  1. 先算出你要显示的4行文字(姓名、职位、邮箱、手机)需要的总高度,包括每行文字的高度和行间距。
  2. 创建一个比原图片更高的Bitmap:宽度和原图一样,高度是原图高度加上刚才算出来的文字区域高度。
  3. 分两步绘制:先把原图片画在新Bitmap的顶部,再在下方的空白区域依次画文字。

修改后的关键代码

替换你share.setOnClickListener里if (addsign.isChecked())块内的Bitmap处理部分,其他代码保持不变:

if (addsign.isChecked()) {
    BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
    Bitmap bm = drawable.getBitmap();
    Bitmap.Config config = bm.getConfig();
    // 防止部分图片的Config为null,兜底设置为ARGB_8888
    if(config == null){
        config = Bitmap.Config.ARGB_8888;
    }
    
    // 计算文字区域需要的高度
    Paint paint = new Paint();
    paint.setTextSize(25);
    Paint.FontMetrics fontMetrics = paint.getFontMetrics();
    // 单行文字的实际高度(包含上下留白)
    float singleLineHeight = fontMetrics.bottom - fontMetrics.top;
    // 自定义行间距,可根据需求调整
    int lineSpacing = 8;
    // 4行文字 + 3个行间距的总高度
    int textAreaHeight = (int)(singleLineHeight * 4 + lineSpacing * 3);
    
    // 创建新Bitmap:宽度和原图一致,高度=原图高度+文字区域高度
    int newWidth = bm.getWidth();
    int newHeight = bm.getHeight() + textAreaHeight;
    Bitmap newImage = Bitmap.createBitmap(newWidth, newHeight, config);
    Canvas c = new Canvas(newImage);
    
    // 第一步:把原图片绘制在新Bitmap的顶部
    c.drawBitmap(bm, 0, 0, null);
    
    // 第二步:在图片下方绘制文字
    paint.setColor(Color.RED);
    paint.setStyle(Paint.Style.FILL);
    paint.setTextSize(25);
    
    // 第一行:姓名,用FontMetrics计算基线位置,避免文字截断
    float currentY = bm.getHeight() + singleLineHeight - fontMetrics.descent;
    c.drawText(getName, 30.0F, currentY, paint);
    
    // 第二行:职位,加上行间距
    currentY += singleLineHeight + lineSpacing;
    c.drawText(getJob, 30.0F, currentY, paint);
    
    // 第三行:邮箱
    currentY += singleLineHeight + lineSpacing;
    c.drawText(getEmail, 30.0F, currentY, paint);
    
    // 第四行:手机
    currentY += singleLineHeight + lineSpacing;
    c.drawText(getMobile, 30.0F, currentY, paint);
    
    // 后续的保存、分享代码和你原来的一样,不用改
    try {
        FileOutputStream outStream = null;
        File sdCard = Environment.getExternalStorageDirectory();
        File dir = new File(sdCard.getAbsolutePath() + "/dgfs");
        dir.mkdirs();
        String fileName = String.format("%d.jpg", System.currentTimeMillis());
        File outFile = new File(dir, fileName);
        outStream = new FileOutputStream(outFile);
        newImage.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
        outStream.flush();
        outStream.close();
        
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        intent.setData(Uri.fromFile(outFile));
        sendBroadcast(intent);
        
        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("image/jpeg");
        share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + outFile.getAbsolutePath()));
        startActivity(Intent.createChooser(share, "Share Image"));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

几个重要细节

  • 文字对齐:用FontMetrics计算文字基线位置,能避免文字被截断或者显示不全,比手动算位置靠谱多了。
  • 动态计算高度:根据你设置的字号自动算单行高度,以后改字号不用手动调位置。
  • 兼容性处理:有些图片的Config可能是null,所以加了兜底逻辑,防止崩溃。

这样改完后,生成的图片就会在原图片下方留出专门的区域显示文字,不会和图片内容重叠了。

内容的提问来源于stack exchange,提问作者karan handa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:22:36