Advertisement
Guest User

Untitled

a guest
Aug 24th, 2015
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1.  
  2. /**
  3. * Created by graeme.castle on 02/07/2015.
  4. */
  5. public class CircleImageView extends ImageView {
  6.  
  7. private static final int CIRCLE_RADIUS = 5;
  8.  
  9. public CircleImageView(Context context) {
  10. super(context);
  11. }
  12.  
  13. public CircleImageView(Context context, AttributeSet attrs) {
  14. super(context, attrs);
  15. }
  16.  
  17. @Override
  18. protected void onDraw(Canvas canvas) {
  19. if (getDrawable() == null) {
  20. return;
  21. }
  22.  
  23. if (getWidth() == 0 || getHeight() == 0) {
  24. return;
  25. }
  26.  
  27. Bitmap b = ((BitmapDrawable)getDrawable()).getBitmap() ;
  28. Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
  29. bitmap = Bitmap.createScaledBitmap(bitmap, getWidth(), getHeight(), true /* filter */);
  30.  
  31. BitmapShader shader;
  32. shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
  33.  
  34. Paint paint = new Paint();
  35. paint.setAntiAlias(true);
  36. paint.setShader(shader);
  37.  
  38. RectF iconRect = new RectF(CIRCLE_RADIUS, CIRCLE_RADIUS, getWidth()-CIRCLE_RADIUS, getHeight()-CIRCLE_RADIUS);
  39.  
  40. Paint whitePaint = new Paint();
  41. whitePaint.setColor(0xFFFFFFFF);
  42. whitePaint.setAntiAlias(true);
  43. canvas.drawCircle((getWidth()/2), (getHeight() / 2), (getWidth()/2), whitePaint);
  44.  
  45. // rect contains the bounds of the shape
  46. // radius is the radius in pixels of the rounded corners
  47. // paint contains the shader that will texture the shape
  48. canvas.drawRoundRect(iconRect, (getWidth()/ 2) - CIRCLE_RADIUS, (getHeight() / 2) -CIRCLE_RADIUS, paint);
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement