androidwatermarkandroid-glideandroid-paint

Add markerwater in Android using Glide


I have the code to add a bitmap on imageview:

try {
        Glide.with(imagen.getContext())
                .load(item.getIdImagen())
                .into(imagen);
    } catch(Exception e) {
    }

I also have the code that would add the watermark:

public Bitmap applyWaterMarkEffect(Bitmap src, String watermark, int x, int y, int color, int alpha, int size, boolean underline) {
    int w = src.getWidth();
    int h = src.getHeight();
    Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());

    Canvas canvas = new Canvas(result);
    canvas.drawBitmap(src, 0, 0, null);

    Paint paint = new Paint();
    paint.setColor(color);
    paint.setAlpha(alpha);
    paint.setTextSize(size);
    paint.setAntiAlias(true);
    paint.setUnderlineText(underline);
    canvas.drawText(watermark, x, y, paint);

    return result;
}

The problem is that I do not know how to combine them to show everything together. Should I get the image(bitmap) to pass it as a parameter? In that case, how do I get it?

Thanks!


Solution

  • You should make your own custom transformation

    You get the original bitmap in your transformation class, you add the watermark and return the bitmap with the watermark.

    Than you could simply do

    Glide  
        .with(context)
        .load(someUrl)
        .bitmapTransform(new WatermarkTransformation(context))
        .into(imageView);