Advertisement
Guest User

Untitled

a guest
May 30th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. public class RoundedImageView extends ImageView {
  2.  
  3. public RoundedImageView(Context context) {
  4. super(context);
  5. }
  6.  
  7. public RoundedImageView(Context context, AttributeSet attrs) {
  8. super(context, attrs);
  9. }
  10.  
  11. public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
  12. super(context, attrs, defStyle);
  13. }
  14.  
  15. @Override
  16. protected void onDraw(Canvas canvas) {
  17.  
  18. Drawable drawable = getDrawable();
  19.  
  20. if (drawable == null) {
  21. return;
  22. }
  23.  
  24. if (getWidth() == 0 || getHeight() == 0) {
  25. return;
  26. }
  27.  
  28. Bitmap b;
  29. if(drawable instanceof VectorDrawable){
  30. b = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
  31.  
  32. Canvas canvasVector = new Canvas(b);
  33. drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
  34. drawable.draw(canvasVector);
  35.  
  36. }else{
  37. b = ((BitmapDrawable) drawable).getBitmap();
  38. }
  39. Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
  40.  
  41. int w = getWidth(), h = getHeight();
  42.  
  43. Bitmap roundBitmap = getCroppedBitmap(bitmap, w);
  44. canvas.drawBitmap(roundBitmap, 0, 0, null);
  45.  
  46. }
  47.  
  48. public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
  49. Bitmap sbmp;
  50.  
  51. if (bmp.getWidth() != radius || bmp.getHeight() != radius) {
  52. float smallest = Math.min(bmp.getWidth(), bmp.getHeight());
  53. float factor = smallest / radius;
  54. sbmp = Bitmap.createScaledBitmap(bmp, (int)(bmp.getWidth() / factor), (int)(bmp.getHeight() / factor), false);
  55. } else {
  56. sbmp = bmp;
  57. }
  58.  
  59. Bitmap output = Bitmap.createBitmap(radius, radius,
  60. Bitmap.Config.ARGB_8888);
  61. Canvas canvas = new Canvas(output);
  62.  
  63. final int color = 0xffa19774;
  64. final Paint paint = new Paint();
  65. final Rect rect = new Rect(0, 0, radius, radius);
  66.  
  67. paint.setAntiAlias(true);
  68. paint.setFilterBitmap(true);
  69. paint.setDither(true);
  70. canvas.drawARGB(0, 0, 0, 0);
  71. paint.setColor(Color.parseColor("#BAB399"));
  72. canvas.drawCircle(radius / 2 + 0.7f,
  73. radius / 2 + 0.7f, radius / 2 + 0.1f, paint);
  74. paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
  75. canvas.drawBitmap(sbmp, rect, rect, paint);
  76.  
  77. return output;
  78. }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement