Blame view

app/src/main/java/com/dinhcv/lifelogpedometer/customview/CircularImageView.java 7.38 KB
6b038e443   Dinh Chu   update
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
  package com.dinhcv.lifelogpedometer.customview;
  
  
  import android.annotation.SuppressLint;
  import android.content.Context;
  import android.graphics.Bitmap;
  import android.graphics.BitmapShader;
  import android.graphics.Canvas;
  import android.graphics.Color;
  import android.graphics.Matrix;
  import android.graphics.Paint;
  import android.graphics.Shader;
  import android.graphics.drawable.BitmapDrawable;
  import android.graphics.drawable.Drawable;
  import android.util.AttributeSet;
  import android.util.Log;
  import android.widget.ImageView;
  
  
  @SuppressLint("AppCompatCustomView")
  public class CircularImageView extends ImageView {
      private static final ScaleType SCALE_TYPE = ScaleType.CENTER_CROP;
  
      private static final float DEFAULT_BORDER_WIDTH = 4;
      private static final float DEFAULT_SHADOW_RADIUS = 8.0f;
      private float borderWidth;
      private int canvasSize;
      private float shadowRadius;
      private int shadowColor = Color.BLACK;
  
      private Bitmap image;
      private Drawable drawable;
      private Paint paint;
      private Paint paintBorder;
  
      public CircularImageView(final Context context) {
          this(context, null);
      }
  
      public CircularImageView(Context context, AttributeSet attrs) {
          this(context, attrs, 0);
      }
  
      public CircularImageView(Context context, AttributeSet attrs, int defStyleAttr) {
          super(context, attrs, defStyleAttr);
          init(context, attrs, defStyleAttr);
      }
  
      private void init(Context context, AttributeSet attrs, int defStyleAttr) {
          paint = new Paint();
          paint.setAntiAlias(true);
  
          paintBorder = new Paint();
          paintBorder.setAntiAlias(true);
      }
  
      public void setBorderWidth(float borderWidth) {
          this.borderWidth = borderWidth;
          requestLayout();
          invalidate();
      }
  
      public void setBorderColor(int borderColor) {
          if (paintBorder != null)
              paintBorder.setColor(borderColor);
          invalidate();
      }
  
      public void addShadow() {
          if (shadowRadius == 0)
              shadowRadius = DEFAULT_SHADOW_RADIUS;
          drawShadow(shadowRadius, shadowColor);
          invalidate();
      }
  
      public void setShadowRadius(float shadowRadius) {
          drawShadow(shadowRadius, shadowColor);
          invalidate();
      }
  
      public void setShadowColor(int shadowColor) {
          drawShadow(shadowRadius, shadowColor);
          invalidate();
      }
  
      @Override
      public ScaleType getScaleType() {
          return SCALE_TYPE;
      }
  
      @Override
      public void setScaleType(ScaleType scaleType) {
          if (scaleType != SCALE_TYPE) {
              throw new IllegalArgumentException(String.format("ScaleType %s not supported. ScaleType.CENTER_CROP is used by default. So you don't need to use ScaleType.", scaleType));
          }
      }
  
      @Override
      public void onDraw(Canvas canvas) {
          loadBitmap();
  
          if (image == null)
              return;
  
          if (!isInEditMode()) {
              canvasSize = canvas.getWidth();
              if (canvas.getHeight() < canvasSize) {
                  canvasSize = canvas.getHeight();
              }
          }
  
          int circleCenter = (int) (canvasSize - (borderWidth * 2)) / 2;
          canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter + borderWidth - (shadowRadius + shadowRadius / 2), paintBorder);
          canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter - (shadowRadius + shadowRadius / 2), paint);
      }
  
      private void loadBitmap() {
          if (this.drawable == getDrawable())
              return;
  
          this.drawable = getDrawable();
          this.image = drawableToBitmap(this.drawable);
          updateShader();
      }
  
      @Override
      protected void onSizeChanged(int w, int h, int oldw, int oldh) {
          super.onSizeChanged(w, h, oldw, oldh);
          canvasSize = w;
          if (h < canvasSize)
              canvasSize = h;
          if (image != null)
              updateShader();
      }
  
      private void drawShadow(float shadowRadius, int shadowColor) {
          this.shadowRadius = shadowRadius;
          this.shadowColor = shadowColor;
          if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
              setLayerType(LAYER_TYPE_SOFTWARE, paintBorder);
          }
          paintBorder.setShadowLayer(shadowRadius, 0.0f, shadowRadius / 2, shadowColor);
      }
  
      private void updateShader() {
          if (image == null)
              return;
  
          image = cropBitmap(image);
  
          BitmapShader shader = new BitmapShader(image, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
  
          Matrix matrix = new Matrix();
          matrix.setScale((float) canvasSize / (float) image.getWidth(), (float) canvasSize / (float) image.getHeight());
          shader.setLocalMatrix(matrix);
  
          paint.setShader(shader);
      }
  
      private Bitmap cropBitmap(Bitmap bitmap) {
          Bitmap bmp;
          if (bitmap.getWidth() >= bitmap.getHeight()) {
              bmp = Bitmap.createBitmap(
                      bitmap,
                      bitmap.getWidth() / 2 - bitmap.getHeight() / 2,
                      0,
                      bitmap.getHeight(), bitmap.getHeight());
          } else {
              bmp = Bitmap.createBitmap(
                      bitmap,
                      0,
                      bitmap.getHeight() / 2 - bitmap.getWidth() / 2,
                      bitmap.getWidth(), bitmap.getWidth());
          }
          return bmp;
      }
  
      private Bitmap drawableToBitmap(Drawable drawable) {
          if (drawable == null) {
              return null;
          } else if (drawable instanceof BitmapDrawable) {
              return ((BitmapDrawable) drawable).getBitmap();
          }
  
          int intrinsicWidth = drawable.getIntrinsicWidth();
          int intrinsicHeight = drawable.getIntrinsicHeight();
  
          if (!(intrinsicWidth > 0 && intrinsicHeight > 0))
              return null;
  
          try {
              Bitmap bitmap = Bitmap.createBitmap(intrinsicWidth, intrinsicHeight, Bitmap.Config.ARGB_8888);
              Canvas canvas = new Canvas(bitmap);
              drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
              drawable.draw(canvas);
              return bitmap;
          } catch (OutOfMemoryError e) {
              Log.e(getClass().toString(), "Encountered OutOfMemoryError while generating bitmap!");
              return null;
          }
      }
  
      @Override
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
          int width = measureWidth(widthMeasureSpec);
          int height = measureHeight(heightMeasureSpec);
          setMeasuredDimension(width, height);
      }
  
      private int measureWidth(int measureSpec) {
          int result;
          int specMode = MeasureSpec.getMode(measureSpec);
          int specSize = MeasureSpec.getSize(measureSpec);
  
          if (specMode == MeasureSpec.EXACTLY) {
              result = specSize;
          } else if (specMode == MeasureSpec.AT_MOST) {
              result = specSize;
          } else {
              result = canvasSize;
          }
  
          return result;
      }
  
      private int measureHeight(int measureSpecHeight) {
          int result;
          int specMode = MeasureSpec.getMode(measureSpecHeight);
          int specSize = MeasureSpec.getSize(measureSpecHeight);
  
          if (specMode == MeasureSpec.EXACTLY) {
              result = specSize;
          } else if (specMode == MeasureSpec.AT_MOST) {
              result = specSize;
          } else {
              result = canvasSize;
          }
  
          return (result + 2);
      }
  }