aboutsummaryrefslogtreecommitdiff
path: root/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/RoundedBgTextView.kt
diff options
context:
space:
mode:
Diffstat (limited to 'ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/RoundedBgTextView.kt')
-rw-r--r--ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/RoundedBgTextView.kt61
1 files changed, 61 insertions, 0 deletions
diff --git a/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/RoundedBgTextView.kt b/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/RoundedBgTextView.kt
new file mode 100644
index 00000000..9f0d2881
--- /dev/null
+++ b/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/RoundedBgTextView.kt
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.example.text.styling.roundedbg
+
+import android.content.Context
+import android.graphics.Canvas
+import android.text.Spanned
+import android.util.AttributeSet
+import androidx.appcompat.widget.AppCompatTextView
+import androidx.core.graphics.withTranslation
+
+/**
+ * A TextView that can draw rounded background to the portions of the text. See
+ * [TextRoundedBgHelper] for more information.
+ *
+ * See [TextRoundedBgAttributeReader] for supported attributes.
+ */
+class RoundedBgTextView : AppCompatTextView {
+
+ private val textRoundedBgHelper: TextRoundedBgHelper
+
+ @JvmOverloads
+ constructor(
+ context: Context,
+ attrs: AttributeSet? = null,
+ defStyleAttr: Int = android.R.attr.textViewStyle
+ ) : super(context, attrs, defStyleAttr) {
+ val attributeReader = TextRoundedBgAttributeReader(context, attrs)
+ textRoundedBgHelper = TextRoundedBgHelper(
+ horizontalPadding = attributeReader.horizontalPadding,
+ verticalPadding = attributeReader.verticalPadding,
+ drawable = attributeReader.drawable,
+ drawableLeft = attributeReader.drawableLeft,
+ drawableMid = attributeReader.drawableMid,
+ drawableRight = attributeReader.drawableRight
+ )
+ }
+
+ override fun onDraw(canvas: Canvas) {
+ // need to draw bg first so that text can be on top during super.onDraw()
+ if (text is Spanned && layout != null) {
+ canvas.withTranslation(totalPaddingLeft.toFloat(), totalPaddingTop.toFloat()) {
+ textRoundedBgHelper.draw(canvas, text as Spanned, layout)
+ }
+ }
+ super.onDraw(canvas)
+ }
+} \ No newline at end of file