Created
March 22, 2014 06:22
-
-
Save ixiyang/9702183 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class RoundedDrawable extends Drawable { | |
protected final float cornerRadius; | |
protected final int margin; | |
protected final RectF mRect = new RectF(), | |
mBitmapRect; | |
protected final BitmapShader bitmapShader; | |
protected final Paint paint; | |
public RoundedDrawable(Bitmap bitmap, int cornerRadius, int margin) { | |
this.cornerRadius = cornerRadius; | |
this.margin = margin; | |
bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); | |
mBitmapRect = new RectF (margin, margin, bitmap.getWidth() - margin, bitmap.getHeight() - margin); | |
paint = new Paint(); | |
paint.setAntiAlias(true); | |
paint.setShader(bitmapShader); | |
} | |
@Override | |
protected void onBoundsChange(Rect bounds) { | |
super.onBoundsChange(bounds); | |
mRect.set(margin, margin, bounds.width() - margin, bounds.height() - margin); | |
// Resize the original bitmap to fit the new bound | |
Matrix shaderMatrix = new Matrix(); | |
shaderMatrix.setRectToRect(mBitmapRect, mRect, Matrix.ScaleToFit.FILL); | |
bitmapShader.setLocalMatrix(shaderMatrix); | |
} | |
@Override | |
public void draw(Canvas canvas) { | |
canvas.drawRoundRect(mRect, cornerRadius, cornerRadius, paint); | |
} | |
@Override | |
public int getOpacity() { | |
return PixelFormat.TRANSLUCENT; | |
} | |
@Override | |
public void setAlpha(int alpha) { | |
paint.setAlpha(alpha); | |
} | |
@Override | |
public void setColorFilter(ColorFilter cf) { | |
paint.setColorFilter(cf); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment