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