Last active
December 2, 2020 20:47
-
-
Save donnfelker/46c56cab8441ecf2ee14 to your computer and use it in GitHub Desktop.
Picasso and CircularTransform Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.graphics.*; | |
import com.squareup.picasso.Transformation; | |
/** | |
* Transforms an image into a circle representation. Such as a avatar. | |
*/ | |
public class CircularTransformation implements Transformation | |
{ | |
int radius = 10; | |
public CircularTransformation(final int radius) | |
{ | |
this.radius = radius; | |
} | |
public CircularTransformation() | |
{ | |
} | |
@Override | |
public Bitmap transform(final Bitmap source) | |
{ | |
Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); | |
Canvas canvas = new Canvas(output); | |
final Paint paint = new Paint(); | |
final Rect rect = new Rect(0, 0, source.getWidth(), source.getHeight()); | |
paint.setAntiAlias(true); | |
paint.setFilterBitmap(true); | |
paint.setDither(true); | |
canvas.drawARGB(0, 0, 0, 0); | |
paint.setColor(Color.parseColor("#BAB399")); | |
canvas.drawCircle(source.getWidth() / 2 + 0.7f, source.getHeight() / 2 + 0.7f, source.getWidth() / 2 - 1.1f, paint); | |
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); | |
canvas.drawBitmap(source, rect, rect, paint); | |
if(source != output) { | |
source.recycle(); | |
} | |
return output; | |
} | |
@Override | |
public String key() | |
{ | |
return "circular" + String.valueOf(radius); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends Activity { | |
public void onCreate(Bundle savedInstanceState) { | |
// all other init code | |
ImageView avatar = findViewById(R.id.avatar); | |
Picasso.with(this) | |
.load("http://i.imgur.com/DvpvklR.png") | |
.transform(new CircularTransformation(10)) // 10 is the radius | |
.into(avatar); | |
// Do the happy dance | |
} | |
} |
Nice work! can we see an example?
Nice work. Thank you.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is what I've been using: