直接丢代码了
效果:

    /**      * 左下角添加水印(多行,图标 + 文字)     * 参考资料:     * Android 对Canvas的translate方法总结    https://blog.csdn.net/u013681739/article/details/49588549     * @param photo     */    public static Bitmap addWaterMark(Context context,Bitmap photo, List<String> textList,List<Integer> iconIdList,boolean isShowIcon) {        Bitmap newBitmap = photo;        try {            if (null == photo) {                return null;            }            int srcWidth = photo.getWidth();            int srcHeight = photo.getHeight();            //Resources resources = context.getResources();            //float scale = resources.getDisplayMetrics().density;            int unitHeight = srcHeight > srcWidth ? srcWidth/30 : srcHeight / 25;            int padding = unitHeight;            int marginLeft = padding;            int marginBottom = padding;            int textSize = unitHeight;            //创建一个bitmap            if (!newBitmap.isMutable()) {                newBitmap = copy(photo);            }            //将该图片作为画布            Canvas canvas = new Canvas(newBitmap);            // 设置画笔            TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);            textPaint.setTextSize(textSize);// 字体大小            textPaint.setTypeface(Typeface.DEFAULT);// 采用默认的宽度            textPaint.setColor(Color.WHITE);// 采用的颜色v            Rect bounds = new Rect();            String gText = "hello world!";            textPaint.getTextBounds(gText, 0, gText.length(), bounds);            int iconWidth = bounds.height();//图片宽度            int maxTextWidth = srcWidth - padding*3 - iconWidth;//最大文字宽度            for (int i = textList.size() - 1 ; i >=0 ; i--){                String text = textList.get(i);                int iconId = iconIdList.get(i);                canvas.save();//锁画布(为了保存之前的画布状态)                //文字处理                StaticLayout layout = new StaticLayout(text, textPaint, maxTextWidth, Layout.Alignment.ALIGN_NORMAL,                        1.0f, 0.0f, true); // 确定换行                //在画布上绘制水印图片                if (isShowIcon){                    Bitmap watermark= BitmapFactory.decodeResource(context.getResources(),iconId);                    int iconHeight = iconWidth * ((watermark.getHeight()*1000)/watermark.getWidth())/1000;//维持图片宽高比例,也可以简单粗暴 iconHeight = iconWidth;                    //图片相对文字位置居中                    RectF rectF=new RectF(marginLeft,srcHeight - marginBottom - layout.getHeight()/2 - iconHeight/2                            ,marginLeft + iconWidth,srcHeight - marginBottom - layout.getHeight()/2 + iconHeight/2);                    canvas.drawBitmap(watermark,null,rectF,null);//限定图片显示范围                }                //绘制文字                canvas.translate(isShowIcon ? marginLeft + iconWidth + padding : marginLeft, srcHeight - layout.getHeight() - marginBottom); // 设定画布位置                layout.draw(canvas); // 绘制水印                //marginBottom 更新                marginBottom = marginBottom + (padding + layout.getHeight());                canvas.restore();//把当前画布返回(调整)到上一个save()状态之前            }            // 保存            canvas.save(Canvas.ALL_SAVE_FLAG);            // 存储            canvas.restore();        } catch (Exception e) {            e.printStackTrace();            return newBitmap;        }        return newBitmap;    }
    /**     * 根据原位图生成一个新的位图,并将原位图所占空间释放     *     * @param srcBmp 原位图     * @return 新位图     */    public static Bitmap copy(Bitmap srcBmp) {        Bitmap destBmp = null;        try {            // 创建一个临时文件            File file = new File(Constant.PATH + "temppic/tmp.txt");            if (file.exists()) {// 临时文件 , 用一次删一次                file.delete();            }            file.getParentFile().mkdirs();            RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");            int width = srcBmp.getWidth();            int height = srcBmp.getHeight();            FileChannel channel = randomAccessFile.getChannel();            MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_WRITE, 0, width * height * 4);            // 将位图信息写进buffer            srcBmp.copyPixelsToBuffer(map);            // 释放原位图占用的空间            srcBmp.recycle();            // 创建一个新的位图            destBmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);            map.position(0);            // 从临时缓冲中拷贝位图信息            destBmp.copyPixelsFromBuffer(map);            channel.close();            randomAccessFile.close();            file.delete();        } catch (Exception ex) {            ex.printStackTrace();            destBmp = null;            return srcBmp;        }        return destBmp;    }

更多相关文章

  1. android小demo笔记
  2. android上传图片到服务器,求服务器那边和android的Activity的完整
  3. android 常见分辨率(mdpi、hdpi 、xhdpi、xxhdpi )屏幕适配
  4. android 之绘图
  5. Android中上传图片到服务端C#接收图
  6. Android(安卓)高斯模糊 RenderScript封装工具类
  7. 2011.10.19(2)——— android 圆角与倒影
  8. 2013.04.10——— android BitmapFactory.Options
  9. Android获取手机本地图片缩略图

随机推荐

  1. 让隐藏视窗的Dock成半透明状态
  2. 【微信小程序】自定义导航栏(一)
  3. 【微信小程序】自定义导航栏(二)
  4. 如何在Safari浏览器中设置活跃及其他网站
  5. 20210225-1 Python错误与异常
  6. 如何在Mac上的“照片”中使用滤镜来更改
  7. 【DB宝41】监控利器PMM的使用--监控MySQL
  8. 【免费开源】基于Vue和Quasar的crudapi前
  9. MySql数据库列表数据分页查询、全文检索A
  10. 【微信小程序】引入阿里巴巴图标库iconfo