FileDocCategorySizeDatePackage
CircleImageView.javaAPI DocAndroid 5.1 API5556Thu Mar 12 22:22:56 GMT 2015android.support.v4.widget

CircleImageView

public class CircleImageView extends android.widget.ImageView
Private class created to work around issues with AnimationListeners being called before the animation is actually complete and support shadows on older platforms.
hide

Fields Summary
private static final int
KEY_SHADOW_COLOR
private static final int
FILL_SHADOW_COLOR
private static final float
X_OFFSET
private static final float
Y_OFFSET
private static final float
SHADOW_RADIUS
private static final int
SHADOW_ELEVATION
private Animation.AnimationListener
mListener
private int
mShadowRadius
Constructors Summary
public CircleImageView(android.content.Context context, int color, float radius)


            
        super(context);
        final float density = getContext().getResources().getDisplayMetrics().density;
        final int diameter = (int) (radius * density * 2);
        final int shadowYOffset = (int) (density * Y_OFFSET);
        final int shadowXOffset = (int) (density * X_OFFSET);

        mShadowRadius = (int) (density * SHADOW_RADIUS);

        ShapeDrawable circle;
        if (elevationSupported()) {
            circle = new ShapeDrawable(new OvalShape());
            ViewCompat.setElevation(this, SHADOW_ELEVATION * density);
        } else {
            OvalShape oval = new OvalShadow(mShadowRadius, diameter);
            circle = new ShapeDrawable(oval);
            ViewCompat.setLayerType(this, ViewCompat.LAYER_TYPE_SOFTWARE, circle.getPaint());
            circle.getPaint().setShadowLayer(mShadowRadius, shadowXOffset, shadowYOffset,
                    KEY_SHADOW_COLOR);
            final int padding = mShadowRadius;
            // set padding so the inner image sits correctly within the shadow.
            setPadding(padding, padding, padding, padding);
        }
        circle.getPaint().setColor(color);
        setBackgroundDrawable(circle);
    
Methods Summary
private booleanelevationSupported()

        return android.os.Build.VERSION.SDK_INT >= 21;
    
public voidonAnimationEnd()

        super.onAnimationEnd();
        if (mListener != null) {
            mListener.onAnimationEnd(getAnimation());
        }
    
public voidonAnimationStart()

        super.onAnimationStart();
        if (mListener != null) {
            mListener.onAnimationStart(getAnimation());
        }
    
protected voidonMeasure(int widthMeasureSpec, int heightMeasureSpec)

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (!elevationSupported()) {
            setMeasuredDimension(getMeasuredWidth() + mShadowRadius*2, getMeasuredHeight()
                    + mShadowRadius*2);
        }
    
public voidsetAnimationListener(Animation.AnimationListener listener)

        mListener = listener;
    
public voidsetBackgroundColor(int color)

        if (getBackground() instanceof ShapeDrawable) {
            ((ShapeDrawable) getBackground()).getPaint().setColor(color);
        }
    
public voidsetBackgroundColorRes(int colorRes)
Update the background color of the circle image view.

param
colorRes Id of a color resource.

        setBackgroundColor(getContext().getResources().getColor(colorRes));