android 图片下方添加文字水印

package com.dji.FPVDemo;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;

/**
 * Created by mzm on 2019/3/20.
 */

public class WaterMarkerUtils {

    public Bitmap addWaterMark(Bitmap src, String text) {
        return createBitmap(src, getWaterBitmap(src,text));
    }

    private Bitmap getWaterBitmap(Bitmap src, String text) {
        Bitmap newBitmap = Bitmap.createBitmap(src.getWidth(),150, Bitmap.Config.ARGB_4444);
        Canvas canvas = new Canvas(newBitmap);
        canvas.drawRGB(0, 0, 0);
        TextPaint textPaint = new TextPaint();
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(30.0F);
        textPaint.setColor(Color.WHITE);
        StaticLayout sl= new StaticLayout(text, textPaint, newBitmap.getWidth()-8, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
        canvas.translate(6, 10);
        sl.draw(canvas);
        canvas.save();
        canvas.restore();
        return newBitmap;
    }

    private Bitmap createBitmap(Bitmap src, Bitmap watermark) {
        if (src == null) {
            return null;
        }
        int w = src.getWidth();
        int h = src.getHeight();
        int wh = watermark.getHeight();
        Bitmap newb = Bitmap.createBitmap(w, h + wh, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(newb);
        canvas.drawBitmap(src, 0, 0, null);
        canvas.drawBitmap(watermark, 0, h, null);
        canvas.save();
        canvas.restore();
        src.recycle();
        watermark.recycle();
        return newb;
    }

}

你可能感兴趣的:(android 图片下方添加文字水印)