Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by graeme.castle on 02/07/2015.
- */
- public class CircleImageView extends ImageView {
- private static final int CIRCLE_RADIUS = 5;
- public CircleImageView(Context context) {
- super(context);
- }
- public CircleImageView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- @Override
- protected void onDraw(Canvas canvas) {
- if (getDrawable() == null) {
- return;
- }
- if (getWidth() == 0 || getHeight() == 0) {
- return;
- }
- Bitmap b = ((BitmapDrawable)getDrawable()).getBitmap() ;
- Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
- bitmap = Bitmap.createScaledBitmap(bitmap, getWidth(), getHeight(), true /* filter */);
- BitmapShader shader;
- shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
- Paint paint = new Paint();
- paint.setAntiAlias(true);
- paint.setShader(shader);
- RectF iconRect = new RectF(CIRCLE_RADIUS, CIRCLE_RADIUS, getWidth()-CIRCLE_RADIUS, getHeight()-CIRCLE_RADIUS);
- Paint whitePaint = new Paint();
- whitePaint.setColor(0xFFFFFFFF);
- whitePaint.setAntiAlias(true);
- canvas.drawCircle((getWidth()/2), (getHeight() / 2), (getWidth()/2), whitePaint);
- // rect contains the bounds of the shape
- // radius is the radius in pixels of the rounded corners
- // paint contains the shader that will texture the shape
- canvas.drawRoundRect(iconRect, (getWidth()/ 2) - CIRCLE_RADIUS, (getHeight() / 2) -CIRCLE_RADIUS, paint);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement