Guest User

Untitled

a guest
Jul 25th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. /**
  2. * Created by codezjx on 2016/5/4.
  3. */
  4. public class CircleImageTransformation implements Transformation {
  5.  
  6. /**
  7. * A unique key for the transformation, used for caching purposes.
  8. */
  9. private static final String KEY = "circleImageTransformation";
  10.  
  11. @Override
  12. public Bitmap transform(Bitmap source) {
  13.  
  14. int minEdge = Math.min(source.getWidth(), source.getHeight());
  15. int dx = (source.getWidth() - minEdge) / 2;
  16. int dy = (source.getHeight() - minEdge) / 2;
  17.  
  18. // Init shader
  19. Shader shader = new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
  20. Matrix matrix = new Matrix();
  21. matrix.setTranslate(-dx, -dy); // Move the target area to center of the source bitmap
  22. shader.setLocalMatrix(matrix);
  23.  
  24. // Init paint
  25. Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  26. paint.setShader(shader);
  27.  
  28. // Create and draw circle bitmap
  29. Bitmap output = Bitmap.createBitmap(minEdge, minEdge, source.getConfig());
  30. Canvas canvas = new Canvas(output);
  31. canvas.drawOval(new RectF(0, 0, minEdge, minEdge), paint);
  32.  
  33. // Recycle the source bitmap, because we already generate a new one
  34. source.recycle();
  35.  
  36. return output;
  37. }
  38.  
  39. @Override
  40. public String key() {
  41. return KEY;
  42. }
  43. }
Add Comment
Please, Sign In to add comment