I have some images with white background and some with transparent backgrounds. When creating a rounded image of them I want all of them to have the white background so basically I want to replace the transparent pixels of the images with white pixels.
Is there any way of achieving this without resorting to replacing one by one pixel? This is the transformation I use for creating the rounded images:
package com.mobilefootie.util;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
public class RoundedTransformation implements com.squareup.picasso.Transformation {
public RoundedTransformation() {
}
@Override
public Bitmap transform(final Bitmap source) {
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
if (squaredBitmap != source) {
source.recycle();
}
Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
paint.setShader(shader);
paint.setAntiAlias(true);
float r = size/2f;
canvas.drawCircle(r, r, r, paint);
squaredBitmap.recycle();
return bitmap;
}
@Override
public String key() {
return "rounded";
}
}
You have to set a ColorFilter
for you Paint
(I applied GREEN for better visualization):
BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
paint.setShader(shader);
paint.setAntiAlias(true);
PorterDuff.Mode mMode = PorterDuff.Mode.OVERLAY;
paint.setColorFilter(new PorterDuffColorFilter(Color.GREEN, mMode));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With