FileDocCategorySizeDatePackage
CircularBitmapDrawable.javaAPI DocAndroid 5.1 API5748Thu Mar 12 22:22:50 GMT 2015com.android.bitmap.drawable

CircularBitmapDrawable

public class CircularBitmapDrawable extends ExtendedBitmapDrawable
Custom BasicBitmapDrawable implementation for circular images. This draws all bitmaps as a circle with an optional border stroke.

(Omit source code)

Fields Summary
private static android.graphics.Matrix
sMatrix
private final android.graphics.Paint
mBitmapPaint
private final android.graphics.Paint
mBorderPaint
private float
mBorderWidth
private android.graphics.Bitmap
mShaderBitmap
Constructors Summary
public CircularBitmapDrawable(android.content.res.Resources res, com.android.bitmap.BitmapCache cache, boolean limitDensity)


      
                
        this(res, cache, limitDensity, null);
    
public CircularBitmapDrawable(android.content.res.Resources res, com.android.bitmap.BitmapCache cache, boolean limitDensity, ExtendedOptions opts)

        super(res, cache, limitDensity, opts);

        mBitmapPaint.setAntiAlias(true);
        mBitmapPaint.setFilterBitmap(true);
        mBitmapPaint.setDither(true);

        mBorderPaint.setColor(Color.TRANSPARENT);
        mBorderPaint.setStyle(Style.STROKE);
        mBorderPaint.setStrokeWidth(mBorderWidth);
        mBorderPaint.setAntiAlias(true);
    
Methods Summary
protected voidonDrawBitmap(android.graphics.Canvas canvas, android.graphics.Rect src, android.graphics.Rect dst)

        onDrawCircularBitmap(getBitmap().bmp, canvas, src, dst, 1f);
    
protected voidonDrawCircularBitmap(android.graphics.Bitmap bitmap, android.graphics.Canvas canvas, android.graphics.Rect src, android.graphics.Rect dst)
Call this method with a given bitmap to draw it onto the given canvas, masked by a circular BitmapShader.

        onDrawCircularBitmap(bitmap, canvas, src, dst, 1f);
    
protected voidonDrawCircularBitmap(android.graphics.Bitmap bitmap, android.graphics.Canvas canvas, android.graphics.Rect src, android.graphics.Rect dst, float alpha)
Call this method with a given bitmap to draw it onto the given canvas, masked by a circular BitmapShader. The alpha parameter is the value from 0f to 1f to attenuate the alpha by.

        // Draw bitmap through shader first.
        BitmapShader shader = (BitmapShader) mBitmapPaint.getShader();
        if (shader == null || mShaderBitmap != bitmap) {
          shader = new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP);
          mShaderBitmap = bitmap;
        }

        sMatrix.reset();
        // Fit bitmap to bounds.
        float scale = Math.max((float) dst.width() / src.width(),
                (float) dst.height() / src.height());
        sMatrix.postScale(scale, scale);
        // Translate bitmap to dst bounds.
        sMatrix.postTranslate(dst.left, dst.top);
        shader.setLocalMatrix(sMatrix);
        mBitmapPaint.setShader(shader);

        int oldAlpha = mBitmapPaint.getAlpha();
        mBitmapPaint.setAlpha((int) (oldAlpha * alpha));
        canvas.drawCircle(dst.centerX(), dst.centerY(), dst.width() / 2,
                mBitmapPaint);
        mBitmapPaint.setAlpha(oldAlpha);

        // Then draw the border.
        canvas.drawCircle(dst.centerX(), dst.centerY(),
                dst.width() / 2f - mBorderWidth / 2, mBorderPaint);
    
protected voidonDrawPlaceholderOrProgress(android.graphics.Canvas canvas, TileDrawable drawable)

        BitmapDrawable placeholder = (BitmapDrawable) drawable.getInnerDrawable();
        Bitmap bitmap = placeholder.getBitmap();
        float alpha = placeholder.getPaint().getAlpha() / 255f;
        sRect.set(0, 0, bitmap.getWidth(), bitmap.getHeight());
        onDrawCircularBitmap(bitmap, canvas, sRect, getBounds(), alpha);
    
public voidsetAlpha(int alpha)

        super.setAlpha(alpha);

        final int old = mBitmapPaint.getAlpha();
        mBitmapPaint.setAlpha(alpha);
        if (alpha != old) {
            invalidateSelf();
        }
    
public voidsetBorderColor(int color)
Set the border stroke color of this drawable. Set to {@link Color#TRANSPARENT} to disable.

        final boolean changed = mBorderPaint.getColor() != color;
        mBorderPaint.setColor(color);

        if (changed) {
            invalidateSelf();
        }
    
public voidsetBorderWidth(float borderWidth)
Set the border stroke width of this drawable.

        final boolean changed = mBorderPaint.getStrokeWidth() != borderWidth;
        mBorderPaint.setStrokeWidth(borderWidth);
        mBorderWidth = borderWidth;

        if (changed) {
            invalidateSelf();
        }
    
public voidsetColorFilter(android.graphics.ColorFilter cf)

        super.setColorFilter(cf);
        mPaint.setColorFilter(cf);