From 51d6d3db83bd7073bc99fdde513ee1a30fb20cc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erkin=20=C3=87akar?= Date: Fri, 28 Aug 2015 01:21:12 +0300 Subject: [PATCH] Adding image rounding with Picasso Transformation --- .../library/SliderTypes/BaseSliderView.java | 12 ++++++ .../RoundedTransformation.java | 42 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 library/src/main/java/com/daimajia/slider/library/Transformations/RoundedTransformation.java diff --git a/library/src/main/java/com/daimajia/slider/library/SliderTypes/BaseSliderView.java b/library/src/main/java/com/daimajia/slider/library/SliderTypes/BaseSliderView.java index 8ad1d35..e4a43d3 100644 --- a/library/src/main/java/com/daimajia/slider/library/SliderTypes/BaseSliderView.java +++ b/library/src/main/java/com/daimajia/slider/library/SliderTypes/BaseSliderView.java @@ -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; @@ -49,6 +50,8 @@ public abstract class BaseSliderView { private Picasso mPicasso; + private Transformation mTransformation; + /** * Scale type of the image. */ @@ -219,6 +222,10 @@ public void onClick(View v) { return; } + if (mTransformation != null) { + rq.transform(mTransformation); + } + if(rq == null){ return; } @@ -325,4 +332,9 @@ public Picasso getPicasso() { public void setPicasso(Picasso picasso) { mPicasso = picasso; } + + public BaseSliderView setTransformation(Transformation transformation) { + mTransformation = transformation; + return this; + } } diff --git a/library/src/main/java/com/daimajia/slider/library/Transformations/RoundedTransformation.java b/library/src/main/java/com/daimajia/slider/library/Transformations/RoundedTransformation.java new file mode 100644 index 0000000..8ea7d41 --- /dev/null +++ b/library/src/main/java/com/daimajia/slider/library/Transformations/RoundedTransformation.java @@ -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"; + } +} +