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
| class CircleView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.STROKE strokeCap = Paint.Cap.ROUND
strokeWidth = 15f.dp textSize = 34f.dp } val RADIUS = 150F.dp var centerX = 0f var centerY = 0f override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { super.onSizeChanged(w, h, oldw, oldh) centerX = w / 2f centerY = h / 2f }
val testStr = "abab" val textBound = Rect() val fontMetrics = Paint.FontMetricsInt() override fun onDraw(canvas: Canvas) { super.onDraw(canvas)
paint.color = Color.LTGRAY canvas.drawArc( centerX - RADIUS, centerY - RADIUS, centerX + RADIUS, centerY + RADIUS, -90f, 360f, false, paint ) paint.color = Color.BLUE canvas.drawArc( centerX - RADIUS, centerY - RADIUS, centerX + RADIUS, centerY + RADIUS, -90f, 250f, false, paint ) paint.strokeWidth = 5f.dp canvas.drawPoint(centerX, centerY, paint) paint.strokeWidth = 0f
paint.getTextBounds(testStr, 0, testStr.length, textBound) canvas.drawText( testStr, centerX - (textBound.right + textBound.left) / 2, centerY - (textBound.top + textBound.bottom) / 2, paint ) paint.getFontMetricsInt(fontMetrics) Log.d(javaClass.simpleName,fontMetrics.toString()) canvas.drawText( testStr, centerX - (textBound.right + textBound.left) / 2, centerY - (fontMetrics.top + fontMetrics.bottom) / 2, paint )
paint.color = Color.BLACK paint.textSize = 14f.dp paint.getTextBounds(testStr,0,testStr.length,textBound) canvas.drawText(testStr, -textBound.left.toFloat(),-textBound.top.toFloat(), paint)
paint.textSize = 150f.dp paint.getTextBounds(testStr,0,testStr.length,textBound) canvas.drawText(testStr, -textBound.left.toFloat(),-textBound.top.toFloat(), paint)
} }
|