UserAvatarViewpublic class UserAvatarView extends android.view.View A view that displays a user image cropped to a circle with a frame. |
Fields Summary |
---|
private int | mActiveFrameColor | private int | mFrameColor | private float | mFrameWidth | private float | mFramePadding | private android.graphics.Bitmap | mBitmap | private android.graphics.drawable.Drawable | mDrawable | private final android.graphics.Paint | mFramePaint | private final android.graphics.Paint | mBitmapPaint | private final android.graphics.Matrix | mDrawMatrix | private float | mScale |
Constructors Summary |
---|
public UserAvatarView(android.content.Context context, android.util.AttributeSet attrs, int defStyleAttr, int defStyleRes)
super(context, attrs, defStyleAttr, defStyleRes);
final TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.UserAvatarView, defStyleAttr, defStyleRes);
final int N = a.getIndexCount();
for (int i = 0; i < N; i++) {
int attr = a.getIndex(i);
switch (attr) {
case R.styleable.UserAvatarView_frameWidth:
setFrameWidth(a.getDimension(attr, 0));
break;
case R.styleable.UserAvatarView_framePadding:
setFramePadding(a.getDimension(attr, 0));
break;
case R.styleable.UserAvatarView_activeFrameColor:
setActiveFrameColor(a.getColor(attr, 0));
break;
case R.styleable.UserAvatarView_frameColor:
setFrameColor(a.getColor(attr, 0));
break;
}
}
a.recycle();
mFramePaint.setAntiAlias(true);
mFramePaint.setStyle(Paint.Style.STROKE);
mBitmapPaint.setAntiAlias(true);
| public UserAvatarView(android.content.Context context, android.util.AttributeSet attrs, int defStyleAttr)
this(context, attrs, defStyleAttr, 0);
| public UserAvatarView(android.content.Context context, android.util.AttributeSet attrs)
this(context, attrs, 0);
| public UserAvatarView(android.content.Context context)
this(context, null);
|
Methods Summary |
---|
public void | configureBounds()
int vwidth = getWidth() - mPaddingLeft - mPaddingRight;
int vheight = getHeight() - mPaddingTop - mPaddingBottom;
int dwidth;
int dheight;
if (mBitmap != null) {
dwidth = mBitmap.getWidth();
dheight = mBitmap.getHeight();
} else if (mDrawable != null) {
vwidth -= 2 * (mFrameWidth - 1);
vheight -= 2 * (mFrameWidth - 1);
dwidth = vwidth;
dheight = vheight;
mDrawable.setBounds(0, 0, dwidth, dheight);
} else {
return;
}
float scale;
float dx;
float dy;
scale = Math.min((float) vwidth / (float) dwidth,
(float) vheight / (float) dheight);
dx = (int) ((vwidth - dwidth * scale) * 0.5f + 0.5f);
dy = (int) ((vheight - dheight * scale) * 0.5f + 0.5f);
mDrawMatrix.setScale(scale, scale);
mDrawMatrix.postTranslate(dx, dy);
mScale = scale;
| protected void | drawableStateChanged()
super.drawableStateChanged();
if (mDrawable != null && mDrawable.isStateful()) {
mDrawable.setState(getDrawableState());
}
| public void | invalidateDrawable(android.graphics.drawable.Drawable dr)
if (dr == mDrawable) {
invalidate();
} else {
super.invalidateDrawable(dr);
}
| protected void | onDraw(android.graphics.Canvas canvas)
int frameColor = isActivated() ? mActiveFrameColor : mFrameColor;
float halfW = getWidth() / 2f;
float halfH = getHeight() / 2f;
float halfSW = Math.min(halfH, halfW);
if (mBitmap != null && mScale > 0) {
int saveCount = canvas.getSaveCount();
canvas.save();
canvas.translate(mPaddingLeft, mPaddingTop);
canvas.concat(mDrawMatrix);
float halfBW = mBitmap.getWidth() / 2f;
float halfBH = mBitmap.getHeight() / 2f;
float halfBSW = Math.min(halfBH, halfBW);
canvas.drawCircle(halfBW, halfBH, halfBSW - mFrameWidth / mScale + 1, mBitmapPaint);
canvas.restoreToCount(saveCount);
} else if (mDrawable != null && mScale > 0) {
int saveCount = canvas.getSaveCount();
canvas.save();
canvas.translate(mPaddingLeft, mPaddingTop);
canvas.translate(mFrameWidth - 1, mFrameWidth - 1);
canvas.concat(mDrawMatrix);
mDrawable.draw(canvas);
canvas.restoreToCount(saveCount);
}
if (frameColor != 0) {
mFramePaint.setColor(frameColor);
mFramePaint.setStrokeWidth(mFrameWidth);
canvas.drawCircle(halfW, halfH, halfSW + (mFramePadding - mFrameWidth) / 2f,
mFramePaint);
}
| protected void | onLayout(boolean changed, int left, int top, int right, int bottom)
super.onLayout(changed, left, top, right, bottom);
configureBounds();
| public void | setActiveFrameColor(int activeFrameColor)
mActiveFrameColor = activeFrameColor;
invalidate();
| public void | setBitmap(android.graphics.Bitmap bitmap)
setDrawable(null);
mBitmap = bitmap;
if (mBitmap != null) {
mBitmapPaint.setShader(new BitmapShader(
bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
} else {
mBitmapPaint.setShader(null);
}
configureBounds();
invalidate();
| public void | setDrawable(android.graphics.drawable.Drawable d)
if (mDrawable != null) {
mDrawable.setCallback(null);
unscheduleDrawable(mDrawable);
}
mDrawable = d;
if (d != null) {
d.setCallback(this);
if (d.isStateful()) {
d.setState(getDrawableState());
}
d.setLayoutDirection(getLayoutDirection());
configureBounds();
}
if (d != null) {
mBitmap = null;
}
configureBounds();
invalidate();
| public void | setFrameColor(int frameColor)
mFrameColor = frameColor;
invalidate();
| public void | setFramePadding(float framePadding)
mFramePadding = framePadding;
invalidate();
| public void | setFrameWidth(float frameWidth)
mFrameWidth = frameWidth;
invalidate();
| protected boolean | verifyDrawable(android.graphics.drawable.Drawable who)
return who == mDrawable || super.verifyDrawable(who);
|
|