Skip to content

Instantly share code, notes, and snippets.

@briangriffey
Last active December 25, 2015 20:29
Show Gist options
  • Save briangriffey/7035642 to your computer and use it in GitHub Desktop.
Save briangriffey/7035642 to your computer and use it in GitHub Desktop.
Use this drawable to to create a circular image like the one found here: https://www.evernote.com/shard/s36/sh/9f22cbd4-9e01-4f7a-9125-2470c4f21e6c/be052171b03a4ad8dc308560971c5944 with any bitmap.
package com.sceneTap.views;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
/**
* Created by @briangriffey on 10/17/13.
* http://www.briangriffey.com
*/
public class CircleImageDrawable extends Drawable {
private final Bitmap mBitmap;
private Bitmap mTransformedBitmap;
private Paint mPaint;
public CircleImageDrawable(Bitmap bitmap) {
mBitmap = bitmap;
mPaint = new Paint();
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(Color.BLACK);
}
@Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
if(mTransformedBitmap != null)
mTransformedBitmap.recycle();
mTransformedBitmap = Bitmap.createScaledBitmap(mBitmap, bounds.width(), bounds.height(), false);
BitmapShader shader = new BitmapShader(mTransformedBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mPaint.setShader(shader);
}
@Override
public void draw(Canvas canvas) {
Rect bounds = getBounds();
canvas.drawCircle(bounds.centerX(), bounds.centerY(), bounds.width() / 2, mPaint);
}
@Override
public void setAlpha(int alpha) {
mPaint.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter cf) {
}
@Override
public int getOpacity() {
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment