CircularImageViewpublic class CircularImageView extends android.widget.ImageView An ImageView class with a circle mask so that all images are drawn in a
circle instead of a square. |
Fields Summary |
---|
private static float | circularImageBorder | private final android.graphics.Matrix | matrix | private final android.graphics.RectF | source | private final android.graphics.RectF | destination | private final android.graphics.Paint | bitmapPaint | private final android.graphics.Paint | borderPaint |
Constructors Summary |
---|
public CircularImageView(android.content.Context context)
this(context, null, 0);
| public CircularImageView(android.content.Context context, android.util.AttributeSet attrs)
this(context, attrs, 0);
| public CircularImageView(android.content.Context context, android.util.AttributeSet attrs, int defStyle)
super(context, attrs, defStyle);
matrix = new Matrix();
source = new RectF();
destination = new RectF();
bitmapPaint = new Paint();
bitmapPaint.setAntiAlias(true);
bitmapPaint.setFilterBitmap(true);
bitmapPaint.setDither(true);
borderPaint = new Paint();
borderPaint.setColor(Color.TRANSPARENT);
borderPaint.setStyle(Paint.Style.STROKE);
borderPaint.setStrokeWidth(circularImageBorder);
borderPaint.setAntiAlias(true);
|
Methods Summary |
---|
public void | drawBitmapWithCircleOnCanvas(android.graphics.Bitmap bitmap, android.graphics.Canvas canvas, android.graphics.RectF source, android.graphics.RectF dest)Given the source bitmap and a canvas, draws the bitmap through a circular
mask. Only draws a circle with diameter equal to the destination width.
// Draw bitmap through shader first.
BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,
Shader.TileMode.CLAMP);
matrix.reset();
// Fit bitmap to bounds.
matrix.setRectToRect(source, dest, Matrix.ScaleToFit.FILL);
shader.setLocalMatrix(matrix);
bitmapPaint.setShader(shader);
canvas.drawCircle(dest.centerX(), dest.centerY(), dest.width() / 2f,
bitmapPaint);
// Then draw the border.
canvas.drawCircle(dest.centerX(), dest.centerY(),
dest.width() / 2f - circularImageBorder / 2, borderPaint);
| protected void | onDraw(android.graphics.Canvas canvas)
Drawable drawable = getDrawable();
BitmapDrawable bitmapDrawable = null;
// support state list drawable by getting the current state
if (drawable instanceof StateListDrawable) {
if (((StateListDrawable) drawable).getCurrent() != null) {
bitmapDrawable = (BitmapDrawable) drawable.getCurrent();
}
} else {
bitmapDrawable = (BitmapDrawable) drawable;
}
if (bitmapDrawable == null) {
return;
}
Bitmap bitmap = bitmapDrawable.getBitmap();
if (bitmap == null) {
return;
}
source.set(0, 0, bitmap.getWidth(), bitmap.getHeight());
destination.set(getPaddingLeft(), getPaddingTop(), getWidth() - getPaddingRight(),
getHeight() - getPaddingBottom());
drawBitmapWithCircleOnCanvas(bitmap, canvas, source, destination);
|
|