Android 设置水印图片在imageview右下角

拍照之后自动在图片的右下角添加自定义的水印,其实原理就是把2个视图结合在一起组成一个bitmap然后图片存储

	
    /**
     * 设置水印图片到中间
     */
    public static Bitmap createWaterMaskCenter(Bitmap src, Bitmap watermark) {
        return createWaterMaskBitmap(src, watermark,
                (src.getWidth() - watermark.getWidth()) / 2,
                (src.getHeight() - watermark.getHeight()) / 2);
    }
	/**
	 * 设置水印图片在左上角
	 */
	public static Bitmap createWaterMaskLeftTop(Context context, Bitmap src, Bitmap watermark, int paddingLeft, int paddingTop) {
		return createWaterMaskBitmap(src, watermark,
				dp2px(context, paddingLeft), dp2px(context, paddingTop));
	}

	/**
	 * 设置水印图片到左下角
	 */
	public static Bitmap createWaterMaskLeftBottom(Context context, Bitmap src, Bitmap watermark, int paddingLeft, int paddingBottom) {
		return createWaterMaskBitmap(src, watermark, dp2px(context, paddingLeft),
				src.getHeight() - watermark.getHeight() - dp2px(context, paddingBottom));
	}
	 //图片上绘制文字
    private static Bitmap drawTextToBitmap(Context context, Bitmap bitmap, String text, Paint paint, Rect bounds, int paddingLeft, int paddingTop) {
        android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();

        paint.setDither(true); // 获取跟清晰的图像采样
        paint.setFilterBitmap(true);// 过滤一些
        if (bitmapConfig == null) {
            bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
        }
        bitmap = bitmap.copy(bitmapConfig, true);
        Canvas canvas = new Canvas(bitmap);

        canvas.drawText(text, paddingLeft, paddingTop, paint);
        return bitmap;
    }

  /**
     * 绘制文字到右上方
     */
    public static Bitmap drawTextToRightTop(Context context, Bitmap bitmap, String text, int size, int color, int paddingRight, int paddingTop) {
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(color);
        paint.setTextSize(dp2px(context, size));
        Rect bounds = new Rect();
        paint.getTextBounds(text, 0, text.length(), bounds);
        return drawTextToBitmap(context, bitmap, text, paint, bounds,
                bitmap.getWidth() - bounds.width() - dp2px(context, paddingRight),
                dp2px(context, paddingTop) + bounds.height());
    }
//设置水印图片在右下角
public static Bitmap createWaterMaskRightBottom(Context context, Bitmap src, Bitmap watermark, int paddingRight, int paddingBottom) {
		return createWaterMaskBitmap(src, watermark,
				src.getWidth() - watermark.getWidth() - dp2px(context, paddingRight),
				src.getHeight() - watermark.getHeight() - dp2px(context, paddingBottom));
	}

	private static Bitmap createWaterMaskBitmap(Bitmap src, Bitmap watermark, int paddingLeft, int paddingTop) {
		if (src == null) {
			return null;
		}
		int width = src.getWidth();
		int height = src.getHeight();
		//创建一个bitmap
		Bitmap newb = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
		//将该图片作为画布
		Canvas canvas = new Canvas(newb);
		//在画布 0,0坐标上开始绘制原始图片
		canvas.drawBitmap(src, 0, 0, null);
		//在画布上绘制水印图片
		canvas.drawBitmap(watermark, paddingLeft, paddingTop, null);
		// 保存
		canvas.save();//Canvas.ALL_SAVE_FLAG
		// 存储
		canvas.restore();
		return newb;
	}

视图转bitmap图片

	public static Bitmap getViewBitmap(View v) {
		v.clearFocus();
		v.setPressed(false);
		boolean willNotCache = v.willNotCacheDrawing();
		v.setWillNotCacheDrawing(false);
		int color = v.getDrawingCacheBackgroundColor();
		v.setDrawingCacheBackgroundColor(0);
		if (color != 0) {
			v.destroyDrawingCache();
		}
		v.buildDrawingCache();
		Bitmap cacheBitmap = v.getDrawingCache();
		if (cacheBitmap == null) {
			return null;
		}
		Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
		v.destroyDrawingCache();
		v.setWillNotCacheDrawing(willNotCache);
		v.setDrawingCacheBackgroundColor(color);
		return bitmap;
	}

调用方式`,ivpreviw为一个imageview图片控件,llview是我自定义的一个LinearLayout布局界面。将最后获取到的bitmap1 保存本地下来就可以了

 // imgbitmap 背景图缩放
        Bitmap bitmap = ImageTools.resizeImage(ImageTools.getViewBitmap(ivpreviw), 1600, 1200);//BitmapFactory.decodeFile(photoPath)
        //右下角文字图片缩放
        Bitmap bitmapview = ImageTools.resizeImage(ImageTools.getViewBitmap(llview), 530, 400);
        //将文字图片置于背景图的右下角显示
        Bitmap bitmap1 = ImageTools.createWaterMaskRightBottom(context, bitmap, bitmapview, 0, 0);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值