CircleImageViewpublic 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. |
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 boolean | elevationSupported()
return android.os.Build.VERSION.SDK_INT >= 21;
| public void | onAnimationEnd()
super.onAnimationEnd();
if (mListener != null) {
mListener.onAnimationEnd(getAnimation());
}
| public void | onAnimationStart()
super.onAnimationStart();
if (mListener != null) {
mListener.onAnimationStart(getAnimation());
}
| protected void | onMeasure(int widthMeasureSpec, int heightMeasureSpec)
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (!elevationSupported()) {
setMeasuredDimension(getMeasuredWidth() + mShadowRadius*2, getMeasuredHeight()
+ mShadowRadius*2);
}
| public void | setAnimationListener(Animation.AnimationListener listener)
mListener = listener;
| public void | setBackgroundColor(int color)
if (getBackground() instanceof ShapeDrawable) {
((ShapeDrawable) getBackground()).getPaint().setColor(color);
}
| public void | setBackgroundColorRes(int colorRes)Update the background color of the circle image view.
setBackgroundColor(getContext().getResources().getColor(colorRes));
|
|