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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
| package liang.cxx.rommel.evaluationbar
import android.content.Context import android.graphics.* import android.util.AttributeSet import android.view.MotionEvent import android.view.View
class EvaluationProgressBar : View { private var m_density: Float = 0.0f private var img_height: Int = 30 private var m_content: Context? = null private var slider: Bitmap? = null private var m_laughing: Bitmap? = null private var m_satisfactiong: Bitmap? = null private var m_sad: Bitmap? = null var image_postion: Float = 0.0f private var image_weight: Float = 0.0f private var m_weight: Float = 0.0f private var m_height: Float = 0.0f private var m_cell: Float = 0.0f private var is_first = true private var m_color_back: Paint? = null private var m_color_back_line: Paint? = null private var m_color_back_diver_one: Paint? = null private var m_color_back_diver_two: Paint? = null private var m_color_back_wall: Paint? = null private var back_line = "#C7C7C7" private var back_sad = "#CECECE" private var back_satisfactiong = "#FFEAB0" private var back_satisfactiong_line = "#FFD68C" private var back_laughing = "#FFBBBB" private var back_laughing_line = "#FF9F9F" private var back_baseground = "#F8F8F8" private var back_satisfactiong_diver = "#FFD68C" private var back_laughing_diver = "#FF9F9F" private var is_touch_scope = false
private var volChangeListener: VolChangeListener? = null private var vol = 2
constructor(context: Context) : super(context) { init(context) }
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) { init(context) }
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { init(context) }
private fun init(context: Context) { m_density = context.resources.displayMetrics.density m_laughing = zoomImg(BitmapFactory.decodeResource(resources, R.mipmap.icon_laughing)) m_satisfactiong = zoomImg(BitmapFactory.decodeResource(resources, R.mipmap.icon_satisfaction)) m_sad = zoomImg(BitmapFactory.decodeResource(resources, R.mipmap.icon_sad)) image_weight = m_laughing!!.width.toFloat() m_content = context m_height = (m_laughing!!.height + 1).toFloat() m_color_back = Paint() m_color_back_line = Paint() m_color_back_line!!.style = Paint.Style.STROKE m_color_back_line!!.isAntiAlias = true m_color_back_line!!.strokeMiter = 1.0f m_color_back_diver_one = Paint() m_color_back_diver_two = Paint() m_color_back_diver_one!!.strokeMiter = 1.0F m_color_back_diver_two!!.strokeMiter = 1.0F m_color_back_wall = Paint() m_color_back_wall!!.color = Color.parseColor(back_line) m_color_back_wall!!.style = Paint.Style.STROKE m_color_back_wall!!.isAntiAlias = true m_color_back_wall!!.strokeWidth = 1.0f }
override fun onDraw(canvas: Canvas) { super.onDraw(canvas) canvas.drawFilter = PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG) image_postion = if (image_postion < image_weight / 2) image_weight / 2 else image_postion image_postion = if (image_postion > m_weight - image_weight / 2) m_weight - image_weight / 2 else image_postion if (image_postion <= m_cell) { slider = m_sad m_color_back!!.color = Color.parseColor(back_sad) m_color_back_line!!.color = Color.parseColor(back_line) } else if (image_postion > m_cell && image_postion <= m_cell * 2) { slider = m_satisfactiong m_color_back!!.color = Color.parseColor(back_satisfactiong) m_color_back_line!!.color = Color.parseColor(back_satisfactiong_line) } else if (image_postion > m_cell * 2) { slider = m_laughing m_color_back!!.color = Color.parseColor(back_laughing) m_color_back_line!!.color = Color.parseColor(back_laughing_line) } drawWall(canvas) drawBckground(canvas) drawDiverLine(canvas) drawImage(canvas) }
fun drawWall(canvas: Canvas) { var rectf = RectF(2f, 0f, m_weight, m_height) canvas.drawRoundRect(rectf, m_height / 2, m_height / 2, m_color_back_wall) var back_base = RectF(3f, 1f, m_weight - 1, m_height - 1) var back_base_color = Paint() back_base_color.color = Color.parseColor(back_baseground) canvas.drawRoundRect(back_base, m_height / 2, m_height / 2, back_base_color) }
fun drawBckground(canvas: Canvas) { var rectf = RectF(2f, 1f, image_postion + image_weight / 2 - 1, m_height - 1) canvas.drawRoundRect(rectf, m_height / 2, m_height / 2, m_color_back) }
fun drawDiverLine(canvas: Canvas) { var rectF = RectF(2f, 0f, image_postion + image_weight / 2, m_height) canvas.drawRoundRect(rectF, m_height / 2, m_height / 2, m_color_back_line) if (image_postion <= m_cell) { m_color_back_diver_one!!.color = Color.parseColor(back_line) canvas.drawLine(m_weight / 3, 0f, m_weight / 3, m_height, m_color_back_diver_one) m_color_back_diver_two!!.color = Color.parseColor(back_line) canvas.drawLine(m_weight * 2 / 3, 0f, m_weight * 2 / 3, m_height, m_color_back_diver_two) } else if (image_postion > m_cell && image_postion <= m_cell * 2) { m_color_back_diver_one!!.color = Color.parseColor(back_satisfactiong_diver) canvas.drawLine(m_weight / 3, 0f, m_weight / 3, m_height, m_color_back_diver_one) m_color_back_diver_two!!.color = Color.parseColor(back_line) canvas.drawLine(m_weight * 2 / 3, 0f, m_weight * 2 / 3, m_height, m_color_back_diver_two) } else if (image_postion > m_cell * 2) { m_color_back_diver_one!!.color = Color.parseColor(back_laughing_diver) canvas.drawLine(m_weight / 3, 0f, m_weight / 3, m_height, m_color_back_diver_one) m_color_back_diver_two!!.color = Color.parseColor(back_laughing_diver) canvas.drawLine(m_weight * 2 / 3, 0f, m_weight * 2 / 3, m_height, m_color_back_diver_two) } }
fun drawImage(canvas: Canvas) { canvas.drawBitmap(slider, image_postion - image_weight / 2 - 2, 1f, Paint()) }
override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) { super.onLayout(changed, left, top, right, bottom) m_weight = (width - 2).toFloat() m_cell = m_weight / 3 if (is_first) { image_postion = m_weight * 2 / 3 is_first = false }
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { super.onMeasure(widthMeasureSpec, heightMeasureSpec) var width = getSize((200 * m_density).toInt(), widthMeasureSpec) var height = getSize(m_laughing!!.width + 1, heightMeasureSpec) setMeasuredDimension(width, height) }
private fun getSize(defaultSize: Int, measureSpec: Int): Int { var m_size = defaultSize var mode = View.MeasureSpec.getMode(measureSpec) var size = MeasureSpec.getSize(measureSpec) when (mode) { MeasureSpec.AT_MOST -> { m_size = defaultSize } MeasureSpec.EXACTLY -> { m_size = size } MeasureSpec.UNSPECIFIED -> { m_size = size } } return m_size }
override fun onTouchEvent(event: MotionEvent): Boolean { var x_postion = event.x when (event.action) { MotionEvent.ACTION_DOWN -> { if (x_postion > image_postion - image_weight / 2 && x < image_postion + image_weight / 2) { image_postion = x_postion is_touch_scope = true } else { image_postion = x_postion setImgPostion() invalidate() onChange() return false } } MotionEvent.ACTION_MOVE -> { if (is_touch_scope) { image_postion = x_postion invalidate() onChange() } } MotionEvent.ACTION_UP -> { if (is_touch_scope) { is_touch_scope = false setImgPostion() } } MotionEvent.ACTION_CANCEL -> { if (is_touch_scope) { is_touch_scope = false setImgPostion() } } } return true }
private fun setImgPostion() { if (image_postion <= m_cell) { image_postion = m_cell } else if (image_postion > m_cell && image_postion <= m_cell * 2) { image_postion = m_cell * 2 } else if (image_postion > m_cell * 2) { image_postion = m_cell * 3 } invalidate() onChange() }
fun onChange() { if (volChangeListener != null) { if (image_postion <= m_cell) { vol = 1 } else if (image_postion > m_cell && image_postion <= 2 * m_cell) { vol = 2 } else if (image_postion > 2 * m_cell) { vol = 3 } volChangeListener!!.onVolChange(vol) } }
private fun zoomImg(btm: Bitmap): Bitmap { var width = btm.width var height = btm.height var scaleWidth = (img_height * m_density) / width var scaleHeight = (img_height * m_density) / height var matrix = Matrix() matrix.postScale(scaleWidth, scaleHeight) return Bitmap.createBitmap(btm, 0, 0, width, height, matrix, true) }
interface VolChangeListener { fun onVolChange(vol: Int) }
fun volVhangeListener(volChangeListener :VolChangeListener) { this.volChangeListener = volChangeListener } }
|