Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding image rounding with Picasso Transformation #146

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestCreator;
import com.squareup.picasso.Transformation;

import java.io.File;

Expand Down Expand Up @@ -49,6 +50,8 @@ public abstract class BaseSliderView {

private Picasso mPicasso;

private Transformation mTransformation;

/**
* Scale type of the image.
*/
Expand Down Expand Up @@ -219,6 +222,10 @@ public void onClick(View v) {
return;
}

if (mTransformation != null) {
rq.transform(mTransformation);
}

if(rq == null){
return;
}
Expand Down Expand Up @@ -325,4 +332,9 @@ public Picasso getPicasso() {
public void setPicasso(Picasso picasso) {
mPicasso = picasso;
}

public BaseSliderView setTransformation(Transformation transformation) {
mTransformation = transformation;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.daimajia.slider.library.Transformations;

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;

public class RoundedTransformation implements com.squareup.picasso.Transformation {
private final int radius;
private final int margin;

public RoundedTransformation(int margin, int radius) {
this.margin = margin;
this.radius = radius;
}

@Override
public Bitmap transform(Bitmap source) {
final Paint paint = new Paint();

paint.setAntiAlias(true);
paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));

Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
canvas.drawRoundRect(new RectF(margin, margin, source.getWidth() - margin, source.getHeight() - margin), radius, radius, paint);

if (source != output) {
source.recycle();
}

return output;
}

@Override
public String key() {
return "rounded";
}
}