Skip to content

Instantly share code, notes, and snippets.

@odedhb
Last active June 9, 2018 18:10
Show Gist options
  • Save odedhb/4c72b1d73ddd7ece1949 to your computer and use it in GitHub Desktop.
Save odedhb/4c72b1d73ddd7ece1949 to your computer and use it in GitHub Desktop.
Watermark Transformation for the Picasso image loading library (https://github.com/square/picasso). The transformation will add the text you provide in the constructor to the image. This was created to be implemented in http://wheredatapp.com, android's greatest search engine.
package com.nextstagesearch.design;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.graphics.Palette;
import com.squareup.picasso.Transformation;
/**
* Created by oded on 9/15/15.
* Watermark Transformation for the Picasso image loading library (https://github.com/square/picasso).
* The transformation will add the text you provide in the constructor to the image.
* This was created to be implemented in http://wheredatapp.com, android's greatest search engine.
*/
public class WatermarkTransformation implements Transformation {
private String waterMark;
private static final int PADDING = 8;
public WatermarkTransformation(String waterMark) {
this.waterMark = waterMark;
}
@Override
public Bitmap transform(Bitmap source) {
//choose the color of the text based on the color contents of the image
Palette palette = Palette.generate(source);
Bitmap workingBitmap = Bitmap.createBitmap(source);
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);
Paint paint2 = new Paint();
paint2.setColor(palette.getVibrantColor(0xdd03a9f4));
paint2.setTextSize(24);
paint2.setTextAlign(Paint.Align.RIGHT);
paint2.setAntiAlias(true);
Rect textBounds = new Rect();
paint2.getTextBounds(waterMark, 0, waterMark.length(), textBounds);
int x = source.getWidth() - PADDING;
int y = source.getHeight() - PADDING;
canvas.drawText(waterMark, x, y, paint2);
source.recycle();
return mutableBitmap;
}
@Override
public String key() {
return "WaterMarkTransformation-" + waterMark;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment