content: package com.agenthun.readingroutine.utils;
  
  import android.content.Context;
  import android.graphics.Bitmap;
  import android.graphics.BitmapShader;
  import android.graphics.Canvas;
  import android.graphics.Color;
  import android.graphics.Paint;
  
  import com.squareup.picasso.Transformation;
  
  /**
   * @project ReadingRoutine
   * @authors agenthun
   * @date 15/12/6 下午6:12.
   */
  public class CircleTransformation implements Transformation {
      private Context mContext;
      private int borderWidth = 0;
      private int borderColor = Color.WHITE;
  
      public CircleTransformation() {
  
      }
  
      public CircleTransformation(Context mContext, int borderWidth) {
          this.mContext = mContext;
          this.borderWidth = borderWidth;
      }
  
      @Override
      public Bitmap transform(Bitmap source) {
          int size = Math.min(source.getWidth(), source.getHeight());
  
          int x = (source.getWidth() - size) / 2;
          int y = (source.getHeight() - size) / 2;
  
          Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
          if (squaredBitmap != source) {
              source.recycle();
          }
  
          Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());
  
          Canvas canvas = new Canvas(bitmap);
          Paint paint = new Paint();
          BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
          paint.setShader(shader);
          paint.setAntiAlias(true);
  
          float r = size / 2f;
          canvas.drawCircle(r, r, r, paint);
  
  /*        if (borderWidth > 0) {
              Paint mBorderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
              mBorderPaint.setStyle(Paint.Style.STROKE);
              mBorderPaint.setColor(borderColor);
              mBorderPaint.setStrokeWidth(UiUtils.dipToPx(mContext, borderWidth));
              canvas.drawCircle(r, r, r - UiUtils.dipToPx(mContext, borderWidth / 2), mBorderPaint);
          }*/
          squaredBitmap.recycle();
          return bitmap;
      }
  
      @Override
      public String key() {
          return "circle";
      }
  
      public int getBorderWidth() {
          return borderWidth;
      }
  
      public void setBorderWidth(int borderWidth) {
          this.borderWidth = borderWidth;
      }
  
      public int getBorderColor() {
          return borderColor;
      }
  
      public void setBorderColor(int borderColor) {
          this.borderColor = borderColor;
      }
  }
  