blob: f705c164cc3fb42c3db9422e8a17d3e13fd61d3b [file] [log] [blame]
Alexandre Lision711fde92013-11-07 15:08:26 -05001package org.sflphone.views;
2
3import org.sflphone.R;
4
5import android.content.Context;
6import android.graphics.Bitmap;
7import android.graphics.Canvas;
8import android.graphics.Color;
9import android.graphics.Paint;
10import android.graphics.RectF;
11import android.graphics.drawable.BitmapDrawable;
12import android.util.AttributeSet;
13import android.widget.ImageView;
14
15public class HalfCircleImageView extends ImageView
16{
17 private int borderWidth = 0;
18 private int viewWidth;
19 private int viewHeight;
20 private Bitmap image;
21 private Paint paint;
22 private Paint paintBorder;
23 private Paint backgroundPaint;
24 private RectF viewBounds;
25
26
27 public HalfCircleImageView(Context context)
28 {
29 super(context);
30 setup();
31 }
32
33 public HalfCircleImageView(Context context, AttributeSet attrs)
34 {
35 super(context, attrs);
36 setup();
37 }
38
39 public HalfCircleImageView(Context context, AttributeSet attrs, int defStyle)
40 {
41 super(context, attrs, defStyle);
42 setup();
43 }
44
45 private void setup()
46 {
47 backgroundPaint = new Paint();
48 backgroundPaint.setColor(getResources().getColor(R.color.sfl_dark_blue));
Alexandre Lisioncdb022f2013-11-08 16:27:46 -050049 backgroundPaint.setAntiAlias(true);
Alexandre Lision711fde92013-11-07 15:08:26 -050050 // init paint
51 paint = new Paint();
52 paint.setAntiAlias(true);
53
54 viewBounds = new RectF();
55
56 paintBorder = new Paint();
57 setBorderColor(Color.WHITE);
58 paintBorder.setAntiAlias(true);
59 this.setLayerType(LAYER_TYPE_SOFTWARE, paintBorder);
60 paintBorder.setShadowLayer(4.0f, 0.0f, 2.0f, Color.BLACK);
61 }
62
63 public void setBorderWidth(int borderWidth)
64 {
65 this.borderWidth = borderWidth;
66 this.invalidate();
67 }
68
69 public void setBorderColor(int borderColor)
70 {
71 if (paintBorder != null)
72 paintBorder.setColor(borderColor);
73
74 this.invalidate();
75 }
76
77 private void loadBitmap()
78 {
79 BitmapDrawable bitmapDrawable = (BitmapDrawable) this.getDrawable();
80
81 if (bitmapDrawable != null)
82 image = bitmapDrawable.getBitmap();
83 }
84
85 @Override
86 public void onDraw(Canvas canvas)
87 {
88 // load the bitmap
89 loadBitmap();
90
91 canvas.drawArc(viewBounds, 180, 180, false, backgroundPaint);
92
93 // init shader
94 if (image != null)
95 {
96// shader = new BitmapShader(Bitmap.createScaledBitmap(image, canvas.getWidth(), canvas.getHeight(), false), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
97// paint.setShader(shader);
98// int circleCenter = viewWidth / 2;
99
100 // circleCenter is the x or y of the view's center
101 // radius is the radius in pixels of the cirle to be drawn
102 // paint contains the shader that will texture the shape
103// canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter + borderWidth - 4.0f, paintBorder);
104 canvas.drawBitmap(image, viewWidth / 2 - image.getWidth() / 2, viewHeight / 3 - image.getHeight() / 2, paint);
105
106
107 }
108 }
109
110 @Override
111 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
112 {
113 int width = measureWidth(widthMeasureSpec);
114 int height = measureHeight(heightMeasureSpec, widthMeasureSpec);
115
116 viewWidth = width - (borderWidth * 2);
117 viewHeight = height - (borderWidth * 2);
118
119 viewBounds.set(0, 0, width, height);
120
121 setMeasuredDimension(width, height);
122 }
123
124 private int measureWidth(int measureSpec)
125 {
126 int result = 0;
127 int specMode = MeasureSpec.getMode(measureSpec);
128 int specSize = MeasureSpec.getSize(measureSpec);
129
130 if (specMode == MeasureSpec.EXACTLY)
131 {
132 // We were told how big to be
133 result = specSize;
134 }
135 else
136 {
137 // Measure the text
138 result = viewWidth;
139 }
140
141 return result;
142 }
143
144 private int measureHeight(int measureSpecHeight, int measureSpecWidth)
145 {
146 int result = 0;
147 int specMode = MeasureSpec.getMode(measureSpecHeight);
148 int specSize = MeasureSpec.getSize(measureSpecHeight);
149
150 if (specMode == MeasureSpec.EXACTLY)
151 {
152 // We were told how big to be
153 result = specSize;
154 }
155 else
156 {
157 // Measure the text (beware: ascent is a negative number)
158 result = viewHeight;
159 }
160
161 return (result);
162 }
163
164}