Skip to content

Instantly share code, notes, and snippets.

@jmhend
Created July 15, 2013 13:32
Show Gist options
  • Select an option

  • Save jmhend/5999958 to your computer and use it in GitHub Desktop.

Select an option

Save jmhend/5999958 to your computer and use it in GitHub Desktop.
Two methods from CircleImageView.java, which aims to display a circular image, with a white border and some shadow. Calling Canvas.saveLayer() on line 56 makes the CircleImageView display properly, but causes lots of UI lag (such as scrolling when the CircleImageView is in a ListView, or when a DrawerLayout is opened/closed on top of a View cont…
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (mRectFRound == null) {
return;
}
mRectFRound.set(0, 0, w, h);
mRectFRound.set(0, 0, w, h);
}
@Override
public void onDraw(Canvas canvas) {
Drawable drawable = getDrawable();
if (drawable == null) {
return;
}
if (!(drawable instanceof BitmapDrawable)) {
super.onDraw(canvas);
return;
}
if (getWidth() == 0 || getHeight() == 0) {
return;
}
if (mRectF == null) {
mRectF = new RectF(0, 0, getWidth(), getHeight());
}
if (mRectFRound == null) {
mRectFRound = new RectF(0, 0, getWidth(), getHeight());
}
// Find dimensions
float scaledWidth = getWidth();
float scaledHeight = getHeight();
float length = Math.min(scaledHeight, scaledWidth);
float centerX = (scaledWidth / 2) + (scaledWidth * .03f);
float centerY = (scaledHeight / 2) + (scaledHeight * .03f);
float radius = (length / 2) - (length * .04f);
canvas.drawCircle(centerX, centerY, radius, mShadowPaint);
centerX = (scaledWidth / 2);
centerY = (scaledHeight / 2);
radius = (length / 2) - (length * .04f);
canvas.drawCircle(centerX, centerY, radius, mWhitePaint);
final int xfer = 0xFF99CC00;
final Paint xferPaint = ((BitmapDrawable) drawable).getPaint();
xferPaint.setAntiAlias(true);
xferPaint.setColor(xfer);
xferPaint.setAlpha(255);
mRectF.set(drawable.getBounds());
int saveCount = canvas.saveLayer(mRectFRound, null,
Canvas.MATRIX_SAVE_FLAG |
Canvas.CLIP_SAVE_FLAG |
Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
Canvas.FULL_COLOR_LAYER_SAVE_FLAG |
Canvas.CLIP_TO_LAYER_SAVE_FLAG);
getImageMatrix().mapRect(mRectF);
centerX = (scaledWidth / 2);
centerY = (scaledHeight / 2);
radius = (length / 2) - (length * .075f);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawCircle(centerX, centerY, radius, xferPaint);
Xfermode oldMode = xferPaint.getXfermode();
xferPaint.setXfermode(XferMode);
super.onDraw(canvas);
xferPaint.setXfermode(oldMode);
canvas.restoreToCount(saveCount);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment