Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 1 | /* |
Alexandre Lision | c1024c0 | 2014-01-06 11:12:53 -0500 | [diff] [blame] | 2 | * Copyright (C) 2004-2014 Savoir-Faire Linux Inc. |
Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 3 | * |
| 4 | * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | * |
| 20 | * Additional permission under GNU GPL version 3 section 7: |
| 21 | * |
| 22 | * If you modify this program, or any covered work, by linking or |
| 23 | * combining it with the OpenSSL project's OpenSSL library (or a |
| 24 | * modified version of that library), containing parts covered by the |
| 25 | * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc. |
| 26 | * grants you additional permission to convey the resulting work. |
| 27 | * Corresponding Source for a non-source form of such a combination |
| 28 | * shall include the source code for the parts of OpenSSL used as well |
| 29 | * as that of the covered work. |
| 30 | */ |
| 31 | |
Alexandre Lision | 711fde9 | 2013-11-07 15:08:26 -0500 | [diff] [blame] | 32 | package org.sflphone.views; |
| 33 | |
| 34 | import org.sflphone.R; |
| 35 | |
| 36 | import android.content.Context; |
| 37 | import android.graphics.Bitmap; |
| 38 | import android.graphics.Canvas; |
| 39 | import android.graphics.Color; |
| 40 | import android.graphics.Paint; |
| 41 | import android.graphics.RectF; |
| 42 | import android.graphics.drawable.BitmapDrawable; |
| 43 | import android.util.AttributeSet; |
| 44 | import android.widget.ImageView; |
| 45 | |
| 46 | public class HalfCircleImageView extends ImageView |
| 47 | { |
| 48 | private int borderWidth = 0; |
| 49 | private int viewWidth; |
| 50 | private int viewHeight; |
| 51 | private Bitmap image; |
| 52 | private Paint paint; |
| 53 | private Paint paintBorder; |
| 54 | private Paint backgroundPaint; |
| 55 | private RectF viewBounds; |
| 56 | |
| 57 | |
| 58 | public HalfCircleImageView(Context context) |
| 59 | { |
| 60 | super(context); |
| 61 | setup(); |
| 62 | } |
| 63 | |
| 64 | public HalfCircleImageView(Context context, AttributeSet attrs) |
| 65 | { |
| 66 | super(context, attrs); |
| 67 | setup(); |
| 68 | } |
| 69 | |
| 70 | public HalfCircleImageView(Context context, AttributeSet attrs, int defStyle) |
| 71 | { |
| 72 | super(context, attrs, defStyle); |
| 73 | setup(); |
| 74 | } |
| 75 | |
| 76 | private void setup() |
| 77 | { |
| 78 | backgroundPaint = new Paint(); |
| 79 | backgroundPaint.setColor(getResources().getColor(R.color.sfl_dark_blue)); |
Alexandre Lision | cdb022f | 2013-11-08 16:27:46 -0500 | [diff] [blame] | 80 | backgroundPaint.setAntiAlias(true); |
Alexandre Lision | 711fde9 | 2013-11-07 15:08:26 -0500 | [diff] [blame] | 81 | // init paint |
| 82 | paint = new Paint(); |
| 83 | paint.setAntiAlias(true); |
| 84 | |
| 85 | viewBounds = new RectF(); |
| 86 | |
| 87 | paintBorder = new Paint(); |
| 88 | setBorderColor(Color.WHITE); |
| 89 | paintBorder.setAntiAlias(true); |
| 90 | this.setLayerType(LAYER_TYPE_SOFTWARE, paintBorder); |
| 91 | paintBorder.setShadowLayer(4.0f, 0.0f, 2.0f, Color.BLACK); |
| 92 | } |
| 93 | |
| 94 | public void setBorderWidth(int borderWidth) |
| 95 | { |
| 96 | this.borderWidth = borderWidth; |
| 97 | this.invalidate(); |
| 98 | } |
| 99 | |
| 100 | public void setBorderColor(int borderColor) |
| 101 | { |
| 102 | if (paintBorder != null) |
| 103 | paintBorder.setColor(borderColor); |
| 104 | |
| 105 | this.invalidate(); |
| 106 | } |
| 107 | |
| 108 | private void loadBitmap() |
| 109 | { |
| 110 | BitmapDrawable bitmapDrawable = (BitmapDrawable) this.getDrawable(); |
| 111 | |
| 112 | if (bitmapDrawable != null) |
| 113 | image = bitmapDrawable.getBitmap(); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void onDraw(Canvas canvas) |
| 118 | { |
| 119 | // load the bitmap |
| 120 | loadBitmap(); |
| 121 | |
| 122 | canvas.drawArc(viewBounds, 180, 180, false, backgroundPaint); |
| 123 | |
| 124 | // init shader |
| 125 | if (image != null) |
| 126 | { |
| 127 | // shader = new BitmapShader(Bitmap.createScaledBitmap(image, canvas.getWidth(), canvas.getHeight(), false), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); |
| 128 | // paint.setShader(shader); |
| 129 | // int circleCenter = viewWidth / 2; |
| 130 | |
| 131 | // circleCenter is the x or y of the view's center |
| 132 | // radius is the radius in pixels of the cirle to be drawn |
| 133 | // paint contains the shader that will texture the shape |
| 134 | // canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter + borderWidth - 4.0f, paintBorder); |
| 135 | canvas.drawBitmap(image, viewWidth / 2 - image.getWidth() / 2, viewHeight / 3 - image.getHeight() / 2, paint); |
| 136 | |
| 137 | |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) |
| 143 | { |
| 144 | int width = measureWidth(widthMeasureSpec); |
| 145 | int height = measureHeight(heightMeasureSpec, widthMeasureSpec); |
| 146 | |
| 147 | viewWidth = width - (borderWidth * 2); |
| 148 | viewHeight = height - (borderWidth * 2); |
| 149 | |
| 150 | viewBounds.set(0, 0, width, height); |
| 151 | |
| 152 | setMeasuredDimension(width, height); |
| 153 | } |
| 154 | |
| 155 | private int measureWidth(int measureSpec) |
| 156 | { |
| 157 | int result = 0; |
| 158 | int specMode = MeasureSpec.getMode(measureSpec); |
| 159 | int specSize = MeasureSpec.getSize(measureSpec); |
| 160 | |
| 161 | if (specMode == MeasureSpec.EXACTLY) |
| 162 | { |
| 163 | // We were told how big to be |
| 164 | result = specSize; |
| 165 | } |
| 166 | else |
| 167 | { |
| 168 | // Measure the text |
| 169 | result = viewWidth; |
| 170 | } |
| 171 | |
| 172 | return result; |
| 173 | } |
| 174 | |
| 175 | private int measureHeight(int measureSpecHeight, int measureSpecWidth) |
| 176 | { |
| 177 | int result = 0; |
| 178 | int specMode = MeasureSpec.getMode(measureSpecHeight); |
| 179 | int specSize = MeasureSpec.getSize(measureSpecHeight); |
| 180 | |
| 181 | if (specMode == MeasureSpec.EXACTLY) |
| 182 | { |
| 183 | // We were told how big to be |
| 184 | result = specSize; |
| 185 | } |
| 186 | else |
| 187 | { |
| 188 | // Measure the text (beware: ascent is a negative number) |
| 189 | result = viewHeight; |
| 190 | } |
| 191 | |
| 192 | return (result); |
| 193 | } |
| 194 | |
| 195 | } |