Skip to content

Instantly share code, notes, and snippets.

@juanpabloprado
Last active February 20, 2016 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanpabloprado/88c6e8ec22007bd0371e to your computer and use it in GitHub Desktop.
Save juanpabloprado/88c6e8ec22007bd0371e to your computer and use it in GitHub Desktop.
RoundedCornersTransform for Picasso
/*
* Copyright 2016 Juan Pablo Prado
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.picassodemo;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import com.squareup.picasso.Transformation;
public class RoundedCornersTransform implements Transformation {
private final int radius;
public RoundedCornersTransform(int radius) {
this.radius = radius;
}
@Override public Bitmap transform(Bitmap source) {
Bitmap result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
Paint shaderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
BitmapShader shader = new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
shaderPaint.setShader(shader);
canvas.drawRoundRect(new RectF(0, 0, source.getWidth(), source.getHeight()), radius, radius, shaderPaint);
if (source != result) {
source.recycle();
}
return result;
}
@Override public String key() {
return RoundedCornersTransform.class.getCanonicalName();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment