From 1c0ec7e104d7e66ff4f3112908b2c204bba922d7 Mon Sep 17 00:00:00 2001 From: Siyamed Sinir Date: Thu, 12 Jul 2018 11:52:44 -0700 Subject: Add text rounded background sample Test: Visual by building the app Bug: 110068813 Change-Id: Ic870bab8fa565ffcbc8b90bc1537d888b4928051 --- ui/text/RoundedBackground-Kotlin/app/build.gradle | 43 +++++ .../app/src/main/AndroidManifest.xml | 35 ++++ .../text/styling/roundedbg/app/MainActivity.kt | 30 +++ .../res/drawable-v24/ic_launcher_foreground.xml | 50 +++++ .../main/res/drawable/ic_launcher_background.xml | 185 ++++++++++++++++++ .../app/src/main/res/drawable/rounded.xml | 21 +++ .../app/src/main/res/drawable/rounded_left.xml | 22 +++ .../app/src/main/res/drawable/rounded_mid.xml | 20 ++ .../app/src/main/res/drawable/rounded_right.xml | 22 +++ .../app/src/main/res/layout/activity_main.xml | 131 +++++++++++++ .../src/main/res/mipmap-anydpi-v26/ic_launcher.xml | 20 ++ .../res/mipmap-anydpi-v26/ic_launcher_round.xml | 20 ++ .../app/src/main/res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 3056 bytes .../src/main/res/mipmap-hdpi/ic_launcher_round.png | Bin 0 -> 5024 bytes .../app/src/main/res/mipmap-mdpi/ic_launcher.png | Bin 0 -> 2096 bytes .../src/main/res/mipmap-mdpi/ic_launcher_round.png | Bin 0 -> 2858 bytes .../app/src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 0 -> 4569 bytes .../main/res/mipmap-xhdpi/ic_launcher_round.png | Bin 0 -> 7098 bytes .../app/src/main/res/mipmap-xxhdpi/ic_launcher.png | Bin 0 -> 6464 bytes .../main/res/mipmap-xxhdpi/ic_launcher_round.png | Bin 0 -> 10676 bytes .../src/main/res/mipmap-xxxhdpi/ic_launcher.png | Bin 0 -> 9250 bytes .../main/res/mipmap-xxxhdpi/ic_launcher_round.png | Bin 0 -> 15523 bytes .../app/src/main/res/values/colors.xml | 26 +++ .../app/src/main/res/values/dimens.xml | 23 +++ .../app/src/main/res/values/strings.xml | 32 ++++ .../app/src/main/res/values/styles.xml | 42 +++++ ui/text/RoundedBackground-Kotlin/build.gradle | 49 +++++ ui/text/RoundedBackground-Kotlin/gradle.properties | 13 ++ .../gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 54708 bytes .../gradle/wrapper/gradle-wrapper.properties | 6 + ui/text/RoundedBackground-Kotlin/gradlew | 172 +++++++++++++++++ ui/text/RoundedBackground-Kotlin/gradlew.bat | 84 +++++++++ ui/text/RoundedBackground-Kotlin/lib/.gitignore | 1 + ui/text/RoundedBackground-Kotlin/lib/build.gradle | 43 +++++ .../lib/src/main/AndroidManifest.xml | 18 ++ .../text/styling/roundedbg/LayoutExtensions.kt | 91 +++++++++ .../text/styling/roundedbg/RoundedBgTextView.kt | 61 ++++++ .../roundedbg/TextRoundedBgAttributeReader.kt | 72 +++++++ .../text/styling/roundedbg/TextRoundedBgHelper.kt | 102 ++++++++++ .../styling/roundedbg/TextRoundedBgRenderer.kt | 209 +++++++++++++++++++++ .../lib/src/main/res/drawable/rounded_text_bg.xml | 21 +++ .../src/main/res/drawable/rounded_text_bg_left.xml | 22 +++ .../src/main/res/drawable/rounded_text_bg_mid.xml | 20 ++ .../main/res/drawable/rounded_text_bg_right.xml | 22 +++ .../lib/src/main/res/values/attrs.xml | 26 +++ .../lib/src/main/res/values/colors.xml | 20 ++ .../lib/src/main/res/values/dimens.xml | 20 ++ .../lib/src/main/res/values/styles.xml | 28 +++ ui/text/RoundedBackground-Kotlin/rounded_bg.png | Bin 0 -> 141698 bytes ui/text/RoundedBackground-Kotlin/settings.gradle | 1 + ui/text/screenshots/rounded_bg.png | Bin 0 -> 130326 bytes 51 files changed, 1823 insertions(+) create mode 100644 ui/text/RoundedBackground-Kotlin/app/build.gradle create mode 100644 ui/text/RoundedBackground-Kotlin/app/src/main/AndroidManifest.xml create mode 100644 ui/text/RoundedBackground-Kotlin/app/src/main/java/com/android/example/text/styling/roundedbg/app/MainActivity.kt create mode 100644 ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable-v24/ic_launcher_foreground.xml create mode 100644 ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/ic_launcher_background.xml create mode 100644 ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/rounded.xml create mode 100644 ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/rounded_left.xml create mode 100644 ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/rounded_mid.xml create mode 100644 ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/rounded_right.xml create mode 100644 ui/text/RoundedBackground-Kotlin/app/src/main/res/layout/activity_main.xml create mode 100644 ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml create mode 100644 ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml create mode 100644 ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100644 ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-hdpi/ic_launcher_round.png create mode 100644 ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100644 ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-mdpi/ic_launcher_round.png create mode 100644 ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100644 ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png create mode 100644 ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100644 ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png create mode 100644 ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png create mode 100644 ui/text/RoundedBackground-Kotlin/app/src/main/res/values/colors.xml create mode 100644 ui/text/RoundedBackground-Kotlin/app/src/main/res/values/dimens.xml create mode 100644 ui/text/RoundedBackground-Kotlin/app/src/main/res/values/strings.xml create mode 100644 ui/text/RoundedBackground-Kotlin/app/src/main/res/values/styles.xml create mode 100644 ui/text/RoundedBackground-Kotlin/build.gradle create mode 100644 ui/text/RoundedBackground-Kotlin/gradle.properties create mode 100644 ui/text/RoundedBackground-Kotlin/gradle/wrapper/gradle-wrapper.jar create mode 100644 ui/text/RoundedBackground-Kotlin/gradle/wrapper/gradle-wrapper.properties create mode 100755 ui/text/RoundedBackground-Kotlin/gradlew create mode 100644 ui/text/RoundedBackground-Kotlin/gradlew.bat create mode 100644 ui/text/RoundedBackground-Kotlin/lib/.gitignore create mode 100644 ui/text/RoundedBackground-Kotlin/lib/build.gradle create mode 100644 ui/text/RoundedBackground-Kotlin/lib/src/main/AndroidManifest.xml create mode 100644 ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/LayoutExtensions.kt create mode 100644 ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/RoundedBgTextView.kt create mode 100644 ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/TextRoundedBgAttributeReader.kt create mode 100644 ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/TextRoundedBgHelper.kt create mode 100644 ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/TextRoundedBgRenderer.kt create mode 100644 ui/text/RoundedBackground-Kotlin/lib/src/main/res/drawable/rounded_text_bg.xml create mode 100644 ui/text/RoundedBackground-Kotlin/lib/src/main/res/drawable/rounded_text_bg_left.xml create mode 100644 ui/text/RoundedBackground-Kotlin/lib/src/main/res/drawable/rounded_text_bg_mid.xml create mode 100644 ui/text/RoundedBackground-Kotlin/lib/src/main/res/drawable/rounded_text_bg_right.xml create mode 100644 ui/text/RoundedBackground-Kotlin/lib/src/main/res/values/attrs.xml create mode 100644 ui/text/RoundedBackground-Kotlin/lib/src/main/res/values/colors.xml create mode 100644 ui/text/RoundedBackground-Kotlin/lib/src/main/res/values/dimens.xml create mode 100644 ui/text/RoundedBackground-Kotlin/lib/src/main/res/values/styles.xml create mode 100644 ui/text/RoundedBackground-Kotlin/rounded_bg.png create mode 100644 ui/text/RoundedBackground-Kotlin/settings.gradle create mode 100644 ui/text/screenshots/rounded_bg.png (limited to 'ui') diff --git a/ui/text/RoundedBackground-Kotlin/app/build.gradle b/ui/text/RoundedBackground-Kotlin/app/build.gradle new file mode 100644 index 00000000..0b78ce1c --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/build.gradle @@ -0,0 +1,43 @@ +/* + * 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. + */ +apply plugin: 'com.android.application' + +apply plugin: 'kotlin-android' + + +android { + compileSdkVersion rootProject.compileSdkVersion + + defaultConfig { + applicationId "com.android.example.text.styling.roundedbg" + minSdkVersion rootProject.minSdkVersion + targetSdkVersion rootProject.targetSdkVersion + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + implementation project(':lib') + implementation "androidx.appcompat:appcompat:$androidxVersion" + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" +} \ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/AndroidManifest.xml b/ui/text/RoundedBackground-Kotlin/app/src/main/AndroidManifest.xml new file mode 100644 index 00000000..d7429d4b --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/AndroidManifest.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/java/com/android/example/text/styling/roundedbg/app/MainActivity.kt b/ui/text/RoundedBackground-Kotlin/app/src/main/java/com/android/example/text/styling/roundedbg/app/MainActivity.kt new file mode 100644 index 00000000..53ae397c --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/java/com/android/example/text/styling/roundedbg/app/MainActivity.kt @@ -0,0 +1,30 @@ +/* + * 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.app + +import android.os.Bundle +import androidx.appcompat.app.AppCompatActivity + +/** + * Sample activity that uses [com.android.example.text.styling.roundedbg.RoundedBgTextView]. + */ +class MainActivity : AppCompatActivity() { + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + } +} diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 00000000..888da70e --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/ic_launcher_background.xml b/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 00000000..a5e49139 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,185 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/rounded.xml b/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/rounded.xml new file mode 100644 index 00000000..f69e6bf9 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/rounded.xml @@ -0,0 +1,21 @@ + + + + + + + \ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/rounded_left.xml b/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/rounded_left.xml new file mode 100644 index 00000000..6883c63e --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/rounded_left.xml @@ -0,0 +1,22 @@ + + + + + + + \ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/rounded_mid.xml b/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/rounded_mid.xml new file mode 100644 index 00000000..b1714ecd --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/rounded_mid.xml @@ -0,0 +1,20 @@ + + + + + + \ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/rounded_right.xml b/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/rounded_right.xml new file mode 100644 index 00000000..8066d972 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/drawable/rounded_right.xml @@ -0,0 +1,22 @@ + + + + + + + \ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/layout/activity_main.xml b/ui/text/RoundedBackground-Kotlin/app/src/main/res/layout/activity_main.xml new file mode 100644 index 00000000..b3738cb3 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,131 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml new file mode 100644 index 00000000..d3441ca6 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml @@ -0,0 +1,20 @@ + + + + + + \ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml new file mode 100644 index 00000000..d3441ca6 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml @@ -0,0 +1,20 @@ + + + + + + \ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-hdpi/ic_launcher.png b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-hdpi/ic_launcher.png new file mode 100644 index 00000000..a2f59082 Binary files /dev/null and b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-hdpi/ic_launcher.png differ diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-hdpi/ic_launcher_round.png b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-hdpi/ic_launcher_round.png new file mode 100644 index 00000000..1b523998 Binary files /dev/null and b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-hdpi/ic_launcher_round.png differ diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-mdpi/ic_launcher.png b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-mdpi/ic_launcher.png new file mode 100644 index 00000000..ff10afd6 Binary files /dev/null and b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-mdpi/ic_launcher.png differ diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-mdpi/ic_launcher_round.png b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-mdpi/ic_launcher_round.png new file mode 100644 index 00000000..115a4c76 Binary files /dev/null and b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-mdpi/ic_launcher_round.png differ diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xhdpi/ic_launcher.png b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xhdpi/ic_launcher.png new file mode 100644 index 00000000..dcd3cd80 Binary files /dev/null and b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xhdpi/ic_launcher.png differ diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png new file mode 100644 index 00000000..459ca609 Binary files /dev/null and b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png differ diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xxhdpi/ic_launcher.png b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xxhdpi/ic_launcher.png new file mode 100644 index 00000000..8ca12fe0 Binary files /dev/null and b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xxhdpi/ic_launcher.png differ diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png new file mode 100644 index 00000000..8e19b410 Binary files /dev/null and b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png differ diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png new file mode 100644 index 00000000..b824ebdd Binary files /dev/null and b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png differ diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png new file mode 100644 index 00000000..4c19a13c Binary files /dev/null and b/ui/text/RoundedBackground-Kotlin/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png differ diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/values/colors.xml b/ui/text/RoundedBackground-Kotlin/app/src/main/res/values/colors.xml new file mode 100644 index 00000000..932c9e93 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/values/colors.xml @@ -0,0 +1,26 @@ + + + + #DDD + #CCC + #FF7F50 + + #CCC + + #FFF176 + #CABF45 + diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/values/dimens.xml b/ui/text/RoundedBackground-Kotlin/app/src/main/res/values/dimens.xml new file mode 100644 index 00000000..02814ded --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/values/dimens.xml @@ -0,0 +1,23 @@ + + + + 8dp + 1dp + + 6dp + 2dp + diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/values/strings.xml b/ui/text/RoundedBackground-Kotlin/app/src/main/res/values/strings.xml new file mode 100644 index 00000000..35cc087d --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/values/strings.xml @@ -0,0 +1,32 @@ + + + + Rounded Multiline Bg + + + "this is a regular paragraph." + "this contains a\nline break and continues." + "this is a paragraph \nthat would cover more than\ntwo lines." + هذا هو "الحق في" الفقرة اليسرى + هذا هو"حق\n لفقرة" اليسار + "ဟ ယ်လို\nဟယ်လို ယ်လို" + "អ ៊ីតាលី\nអ៊ីតា លី"" + "नमस्ते दुनिया\nनमस्ते दुनिया" + "สวัสดี ชาวโลก\nสวัสดีชาวโ ลก" + + \ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/app/src/main/res/values/styles.xml b/ui/text/RoundedBackground-Kotlin/app/src/main/res/values/styles.xml new file mode 100644 index 00000000..1997dad9 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/app/src/main/res/values/styles.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + diff --git a/ui/text/RoundedBackground-Kotlin/build.gradle b/ui/text/RoundedBackground-Kotlin/build.gradle new file mode 100644 index 00000000..f6df7566 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/build.gradle @@ -0,0 +1,49 @@ +/* + * 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. + */ +// Top-level build file where you can add configuration options common to all sub-projects/modules. + +buildscript { + ext.kotlin_version = '1.2.51' + repositories { + google() + mavenCentral() + } + dependencies { + classpath 'com.android.tools.build:gradle:3.3.0-alpha03' + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + } +} + +allprojects { + repositories { + google() + mavenCentral() + } +} + +task clean(type: Delete) { + delete rootProject.buildDir +} + +ext { + // Sdk and tools + minSdkVersion = 15 + targetSdkVersion = 28 + compileSdkVersion = 28 + // App dependencies + androidxVersion = '1.0.0-beta01' + ktxVersion = '1.0.0-alpha1' +} \ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/gradle.properties b/ui/text/RoundedBackground-Kotlin/gradle.properties new file mode 100644 index 00000000..743d692c --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/gradle.properties @@ -0,0 +1,13 @@ +# Project-wide Gradle settings. +# IDE (e.g. Android Studio) users: +# Gradle settings configured through the IDE *will override* +# any settings specified in this file. +# For more details on how to configure your build environment visit +# http://www.gradle.org/docs/current/userguide/build_environment.html +# Specifies the JVM arguments used for the daemon process. +# The setting is particularly useful for tweaking memory settings. +org.gradle.jvmargs=-Xmx1536m +# When configured, Gradle will run in incubating parallel mode. +# This option should only be used with decoupled projects. More details, visit +# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects +# org.gradle.parallel=true diff --git a/ui/text/RoundedBackground-Kotlin/gradle/wrapper/gradle-wrapper.jar b/ui/text/RoundedBackground-Kotlin/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..7a3265ee Binary files /dev/null and b/ui/text/RoundedBackground-Kotlin/gradle/wrapper/gradle-wrapper.jar differ diff --git a/ui/text/RoundedBackground-Kotlin/gradle/wrapper/gradle-wrapper.properties b/ui/text/RoundedBackground-Kotlin/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..37fcd35b --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Wed Jul 11 10:47:22 PDT 2018 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-bin.zip diff --git a/ui/text/RoundedBackground-Kotlin/gradlew b/ui/text/RoundedBackground-Kotlin/gradlew new file mode 100755 index 00000000..cccdd3d5 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/ui/text/RoundedBackground-Kotlin/gradlew.bat b/ui/text/RoundedBackground-Kotlin/gradlew.bat new file mode 100644 index 00000000..e95643d6 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/ui/text/RoundedBackground-Kotlin/lib/.gitignore b/ui/text/RoundedBackground-Kotlin/lib/.gitignore new file mode 100644 index 00000000..796b96d1 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/.gitignore @@ -0,0 +1 @@ +/build diff --git a/ui/text/RoundedBackground-Kotlin/lib/build.gradle b/ui/text/RoundedBackground-Kotlin/lib/build.gradle new file mode 100644 index 00000000..6c499867 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/build.gradle @@ -0,0 +1,43 @@ +/* + * 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. + */ +apply plugin: 'com.android.library' + +apply plugin: 'kotlin-android' + +android { + compileSdkVersion rootProject.compileSdkVersion + + defaultConfig { + minSdkVersion rootProject.minSdkVersion + targetSdkVersion rootProject.targetSdkVersion + versionCode 1 + versionName "1.0" + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } + +} + +dependencies { + implementation "androidx.core:core-ktx:${ktxVersion}" + implementation "androidx.appcompat:appcompat:$androidxVersion" + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" +} \ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/lib/src/main/AndroidManifest.xml b/ui/text/RoundedBackground-Kotlin/lib/src/main/AndroidManifest.xml new file mode 100644 index 00000000..43a5eb06 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/src/main/AndroidManifest.xml @@ -0,0 +1,18 @@ + + + diff --git a/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/LayoutExtensions.kt b/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/LayoutExtensions.kt new file mode 100644 index 00000000..f7478eb4 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/LayoutExtensions.kt @@ -0,0 +1,91 @@ +/* + * 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.os.Build +import android.text.Layout + +// Extension functions for Layout object + +/** + * Android system default line spacing extra + */ +private const val DEFAULT_LINESPACING_EXTRA = 0f + +/** + * Android system default line spacing multiplier + */ +private const val DEFAULT_LINESPACING_MULTIPLIER = 1f + +/** + * Get the line bottom discarding the line spacing added. + */ +fun Layout.getLineBottomWithoutSpacing(line: Int): Int { + val lineBottom = getLineBottom(line) + val lastLineSpacingNotAdded = Build.VERSION.SDK_INT >= 19 + val isLastLine = line == lineCount - 1 + + val lineBottomWithoutSpacing: Int + val lineSpacingExtra = spacingAdd + val lineSpacingMultiplier = spacingMultiplier + val hasLineSpacing = lineSpacingExtra != DEFAULT_LINESPACING_EXTRA + || lineSpacingMultiplier != DEFAULT_LINESPACING_MULTIPLIER + + if (!hasLineSpacing || isLastLine && lastLineSpacingNotAdded) { + lineBottomWithoutSpacing = lineBottom + } else { + val extra: Float + if (lineSpacingMultiplier.compareTo(DEFAULT_LINESPACING_MULTIPLIER) != 0) { + val lineHeight = getLineHeight(line) + extra = lineHeight - (lineHeight - lineSpacingExtra) / lineSpacingMultiplier + } else { + extra = lineSpacingExtra + } + + lineBottomWithoutSpacing = (lineBottom - extra).toInt() + } + + return lineBottomWithoutSpacing +} + +/** + * Get the line height of a line. + */ +fun Layout.getLineHeight(line: Int): Int { + return getLineTop(line + 1) - getLineTop(line) +} + +/** + * Returns the top of the Layout after removing the extra padding applied by the Layout. + */ +fun Layout.getLineTopWithoutPadding(line: Int): Int { + var lineTop = getLineTop(line) + if (line == 0) { + lineTop -= topPadding + } + return lineTop +} + +/** + * Returns the bottom of the Layout after removing the extra padding applied by the Layout. + */ +fun Layout.getLineBottomWithoutPadding(line: Int): Int { + var lineBottom = getLineBottomWithoutSpacing(line) + if (line == lineCount - 1) { + lineBottom -= bottomPadding + } + return lineBottom +} \ No newline at end of file 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 diff --git a/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/TextRoundedBgAttributeReader.kt b/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/TextRoundedBgAttributeReader.kt new file mode 100644 index 00000000..5ecc41d9 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/TextRoundedBgAttributeReader.kt @@ -0,0 +1,72 @@ +/* + * 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.drawable.Drawable +import android.util.AttributeSet +import androidx.core.content.res.getDrawableOrThrow + +/** + * Reads default attributes that [TextRoundedBgHelper] needs from resources. The attributes read + * are: + * + * - chHorizontalPadding: the padding to be applied to left & right of the background + * - chVerticalPadding: the padding to be applied to top & bottom of the background + * - chDrawable: the drawable used to draw the background + * - chDrawableLeft: the drawable used to draw left edge of the background + * - chDrawableMid: the drawable used to draw for whole line + * - chDrawableRight: the drawable used to draw right edge of the background + */ +class TextRoundedBgAttributeReader(context: Context, attrs: AttributeSet?) { + + val horizontalPadding: Int + val verticalPadding: Int + val drawable: Drawable + val drawableLeft: Drawable + val drawableMid: Drawable + val drawableRight: Drawable + + init { + val typedArray = context.obtainStyledAttributes( + attrs, + R.styleable.TextRoundedBgHelper, + 0, + R.style.RoundedBgTextView + ) + horizontalPadding = typedArray.getDimensionPixelSize( + R.styleable.TextRoundedBgHelper_roundedTextHorizontalPadding, + 0 + ) + verticalPadding = typedArray.getDimensionPixelSize( + R.styleable.TextRoundedBgHelper_roundedTextVerticalPadding, + 0 + ) + drawable = typedArray.getDrawableOrThrow( + R.styleable.TextRoundedBgHelper_roundedTextDrawable + ) + drawableLeft = typedArray.getDrawableOrThrow( + R.styleable.TextRoundedBgHelper_roundedTextDrawableLeft + ) + drawableMid = typedArray.getDrawableOrThrow( + R.styleable.TextRoundedBgHelper_roundedTextDrawableMid + ) + drawableRight = typedArray.getDrawableOrThrow( + R.styleable.TextRoundedBgHelper_roundedTextDrawableRight + ) + typedArray.recycle() + } +} diff --git a/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/TextRoundedBgHelper.kt b/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/TextRoundedBgHelper.kt new file mode 100644 index 00000000..262a5fe9 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/TextRoundedBgHelper.kt @@ -0,0 +1,102 @@ +/* + * 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.graphics.Canvas +import android.graphics.drawable.Drawable +import android.text.Annotation +import android.text.Layout +import android.text.Spanned + +/** + * Helper class to draw multi-line rounded background to certain parts of a text. The start/end + * positions of the backgrounds are annotated with [android.text.Annotation] class. Each annotation + * should have the annotation key set to **rounded**. + * + * i.e.: + * ``` + * + * "this is a regular paragraph." + * ``` + * + * **Note:** BiDi text is not supported. + * + * @param horizontalPadding the padding to be applied to left & right of the background + * @param verticalPadding the padding to be applied to top & bottom of the background + * @param drawable the drawable used to draw the background + * @param drawableLeft the drawable used to draw left edge of the background + * @param drawableMid the drawable used to draw for whole line + * @param drawableRight the drawable used to draw right edge of the background + */ +class TextRoundedBgHelper( + val horizontalPadding: Int, + verticalPadding: Int, + drawable: Drawable, + drawableLeft: Drawable, + drawableMid: Drawable, + drawableRight: Drawable +) { + + private val singleLineRenderer: TextRoundedBgRenderer by lazy { + SingleLineRenderer( + horizontalPadding = horizontalPadding, + verticalPadding = verticalPadding, + drawable = drawable + ) + } + + private val multiLineRenderer: TextRoundedBgRenderer by lazy { + MultiLineRenderer( + horizontalPadding = horizontalPadding, + verticalPadding = verticalPadding, + drawableLeft = drawableLeft, + drawableMid = drawableMid, + drawableRight = drawableRight + ) + } + + /** + * Call this function during onDraw of another widget such as TextView. + * + * @param canvas Canvas to draw onto + * @param text + * @param layout Layout that contains the text + */ + fun draw(canvas: Canvas, text: Spanned, layout: Layout) { + // ideally the calculations here should be cached since they are not cheap. However, proper + // invalidation of the cache is required whenever anything related to text has changed. + val spans = text.getSpans(0, text.length, Annotation::class.java) + spans.forEach { span -> + if (span.value.equals("rounded")) { + val spanStart = text.getSpanStart(span) + val spanEnd = text.getSpanEnd(span) + val startLine = layout.getLineForOffset(spanStart) + val endLine = layout.getLineForOffset(spanEnd) + + // start can be on the left or on the right depending on the language direction. + val startOffset = (layout.getPrimaryHorizontal(spanStart) + + -1 * layout.getParagraphDirection(startLine) * horizontalPadding).toInt() + // end can be on the left or on the right depending on the language direction. + val endOffset = (layout.getPrimaryHorizontal(spanEnd) + + layout.getParagraphDirection(endLine) * horizontalPadding).toInt() + + val renderer = if (startLine == endLine) singleLineRenderer else multiLineRenderer + renderer.draw(canvas, layout, startLine, endLine, startOffset, endOffset) + } + } + } +} \ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/TextRoundedBgRenderer.kt b/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/TextRoundedBgRenderer.kt new file mode 100644 index 00000000..37d814a7 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/src/main/java/com/android/example/text/styling/roundedbg/TextRoundedBgRenderer.kt @@ -0,0 +1,209 @@ +/* + * 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.graphics.Canvas +import android.graphics.drawable.Drawable +import android.text.Layout +import kotlin.math.max +import kotlin.math.min + +/** + * Base class for single and multi line rounded background renderers. + * + * @param horizontalPadding the padding to be applied to left & right of the background + * @param verticalPadding the padding to be applied to top & bottom of the background + */ +internal abstract class TextRoundedBgRenderer( + val horizontalPadding: Int, + val verticalPadding: Int +) { + + /** + * Draw the background that starts at the {@code startOffset} and ends at {@code endOffset}. + * + * @param canvas Canvas to draw onto + * @param layout Layout that contains the text + * @param startLine the start line for the background + * @param endLine the end line for the background + * @param startOffset the character offset that the background should start at + * @param endOffset the character offset that the background should end at + */ + abstract fun draw( + canvas: Canvas, + layout: Layout, + startLine: Int, + endLine: Int, + startOffset: Int, + endOffset: Int + ) + + /** + * Get the top offset of the line and add padding into account so that there is a gap between + * top of the background and top of the text. + * + * @param layout Layout object that contains the text + * @param line line number + */ + protected fun getLineTop(layout: Layout, line: Int): Int { + return layout.getLineTopWithoutPadding(line) - verticalPadding + } + + /** + * Get the bottom offset of the line and add padding into account so that there is a gap between + * bottom of the background and bottom of the text. + * + * @param layout Layout object that contains the text + * @param line line number + */ + protected fun getLineBottom(layout: Layout, line: Int): Int { + return layout.getLineBottomWithoutPadding(line) + verticalPadding + } +} + +/** + * Draws the background for text that starts and ends on the same line. + * + * @param horizontalPadding the padding to be applied to left & right of the background + * @param verticalPadding the padding to be applied to top & bottom of the background + * @param drawable the drawable used to draw the background + */ +internal class SingleLineRenderer( + horizontalPadding: Int, + verticalPadding: Int, + val drawable: Drawable +) : TextRoundedBgRenderer(horizontalPadding, verticalPadding) { + + override fun draw( + canvas: Canvas, + layout: Layout, + startLine: Int, + endLine: Int, + startOffset: Int, + endOffset: Int + ) { + val lineTop = getLineTop(layout, startLine) + val lineBottom = getLineBottom(layout, startLine) + // get min of start/end for left, and max of start/end for right since we don't + // the language direction + val left = min(startOffset, endOffset) + val right = max(startOffset, endOffset) + drawable.setBounds(left, lineTop, right, lineBottom) + drawable.draw(canvas) + } +} + +/** + * Draws the background for text that starts and ends on different lines. + * + * @param horizontalPadding the padding to be applied to left & right of the background + * @param verticalPadding the padding to be applied to top & bottom of the background + * @param drawableLeft the drawable used to draw left edge of the background + * @param drawableMid the drawable used to draw for whole line + * @param drawableRight the drawable used to draw right edge of the background + */ +internal class MultiLineRenderer( + horizontalPadding: Int, + verticalPadding: Int, + val drawableLeft: Drawable, + val drawableMid: Drawable, + val drawableRight: Drawable +) : TextRoundedBgRenderer(horizontalPadding, verticalPadding) { + + override fun draw( + canvas: Canvas, + layout: Layout, + startLine: Int, + endLine: Int, + startOffset: Int, + endOffset: Int + ) { + // draw the first line + val paragDir = layout.getParagraphDirection(startLine) + val lineEndOffset = if (paragDir == Layout.DIR_RIGHT_TO_LEFT) { + layout.getLineLeft(startLine) - horizontalPadding + } else { + layout.getLineRight(startLine) + horizontalPadding + }.toInt() + + var lineBottom = getLineBottom(layout, startLine) + var lineTop = getLineTop(layout, startLine) + drawStart(canvas, startOffset, lineTop, lineEndOffset, lineBottom) + + // for the lines in the middle draw the mid drawable + for (line in startLine + 1 until endLine) { + lineTop = getLineTop(layout, line) + lineBottom = getLineBottom(layout, line) + drawableMid.setBounds( + (layout.getLineLeft(line).toInt() - horizontalPadding), + lineTop, + (layout.getLineRight(line).toInt() + horizontalPadding), + lineBottom + ) + drawableMid.draw(canvas) + } + + val lineStartOffset = if (paragDir == Layout.DIR_RIGHT_TO_LEFT) { + layout.getLineRight(startLine) + horizontalPadding + } else { + layout.getLineLeft(startLine) - horizontalPadding + }.toInt() + + // draw the last line + lineBottom = getLineBottom(layout, endLine) + lineTop = getLineTop(layout, endLine) + + drawEnd(canvas, lineStartOffset, lineTop, endOffset, lineBottom) + } + + /** + * Draw the first line of a multiline annotation. Handles LTR/RTL. + * + * @param canvas Canvas to draw onto + * @param start start coordinate for the background + * @param top top coordinate for the background + * @param end end coordinate for the background + * @param bottom bottom coordinate for the background + */ + private fun drawStart(canvas: Canvas, start: Int, top: Int, end: Int, bottom: Int) { + if (start > end) { + drawableRight.setBounds(end, top, start, bottom) + drawableRight.draw(canvas) + } else { + drawableLeft.setBounds(start, top, end, bottom) + drawableLeft.draw(canvas) + } + } + + /** + * Draw the last line of a multiline annotation. Handles LTR/RTL. + * + * @param canvas Canvas to draw onto + * @param start start coordinate for the background + * @param top top position for the background + * @param end end coordinate for the background + * @param bottom bottom coordinate for the background + */ + private fun drawEnd(canvas: Canvas, start: Int, top: Int, end: Int, bottom: Int) { + if (start > end) { + drawableLeft.setBounds(end, top, start, bottom) + drawableLeft.draw(canvas) + } else { + drawableRight.setBounds(start, top, end, bottom) + drawableRight.draw(canvas) + } + } +} \ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/lib/src/main/res/drawable/rounded_text_bg.xml b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/drawable/rounded_text_bg.xml new file mode 100644 index 00000000..0683b1b9 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/drawable/rounded_text_bg.xml @@ -0,0 +1,21 @@ + + + + + + + \ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/lib/src/main/res/drawable/rounded_text_bg_left.xml b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/drawable/rounded_text_bg_left.xml new file mode 100644 index 00000000..5d02c93d --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/drawable/rounded_text_bg_left.xml @@ -0,0 +1,22 @@ + + + + + + + \ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/lib/src/main/res/drawable/rounded_text_bg_mid.xml b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/drawable/rounded_text_bg_mid.xml new file mode 100644 index 00000000..e4c2c570 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/drawable/rounded_text_bg_mid.xml @@ -0,0 +1,20 @@ + + + + + + \ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/lib/src/main/res/drawable/rounded_text_bg_right.xml b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/drawable/rounded_text_bg_right.xml new file mode 100644 index 00000000..cf011c5c --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/drawable/rounded_text_bg_right.xml @@ -0,0 +1,22 @@ + + + + + + + \ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/lib/src/main/res/values/attrs.xml b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/values/attrs.xml new file mode 100644 index 00000000..737ae07c --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/values/attrs.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/lib/src/main/res/values/colors.xml b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/values/colors.xml new file mode 100644 index 00000000..c26ac6af --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/values/colors.xml @@ -0,0 +1,20 @@ + + + + #554fC3f7 + #DD0277BD + diff --git a/ui/text/RoundedBackground-Kotlin/lib/src/main/res/values/dimens.xml b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/values/dimens.xml new file mode 100644 index 00000000..dbc7f49e --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/values/dimens.xml @@ -0,0 +1,20 @@ + + + + 4dp + 1dp + \ No newline at end of file diff --git a/ui/text/RoundedBackground-Kotlin/lib/src/main/res/values/styles.xml b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/values/styles.xml new file mode 100644 index 00000000..9bffef3d --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/lib/src/main/res/values/styles.xml @@ -0,0 +1,28 @@ + + + + + + + diff --git a/ui/text/RoundedBackground-Kotlin/rounded_bg.png b/ui/text/RoundedBackground-Kotlin/rounded_bg.png new file mode 100644 index 00000000..a62a64ef Binary files /dev/null and b/ui/text/RoundedBackground-Kotlin/rounded_bg.png differ diff --git a/ui/text/RoundedBackground-Kotlin/settings.gradle b/ui/text/RoundedBackground-Kotlin/settings.gradle new file mode 100644 index 00000000..3cbe2493 --- /dev/null +++ b/ui/text/RoundedBackground-Kotlin/settings.gradle @@ -0,0 +1 @@ +include ':app', ':lib' diff --git a/ui/text/screenshots/rounded_bg.png b/ui/text/screenshots/rounded_bg.png new file mode 100644 index 00000000..1e9acddf Binary files /dev/null and b/ui/text/screenshots/rounded_bg.png differ -- cgit v1.2.3