├── app ├── .gitignore ├── src │ └── main │ │ ├── res │ │ ├── drawable │ │ │ ├── photo.jpg │ │ │ ├── avatar.jpg │ │ │ ├── chip_background_active.xml │ │ │ ├── chip_background_inactive.xml │ │ │ ├── chip_image.xml │ │ │ ├── chip_background.xml │ │ │ ├── ic_like.xml │ │ │ ├── ic_share.xml │ │ │ └── ic_launcher_background.xml │ │ ├── mipmap-hdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-anydpi-v26 │ │ │ ├── ic_launcher.xml │ │ │ └── ic_launcher_round.xml │ │ ├── values │ │ │ ├── attrs.xml │ │ │ ├── colors.xml │ │ │ ├── styles.xml │ │ │ └── strings.xml │ │ ├── menu │ │ │ └── toolbar_menu.xml │ │ ├── layout │ │ │ ├── tooltip_image.xml │ │ │ ├── tooltip_share.xml │ │ │ ├── tooltip_author.xml │ │ │ ├── activity_main.xml │ │ │ └── content_main.xml │ │ └── drawable-v24 │ │ │ └── ic_launcher_foreground.xml │ │ ├── java │ │ └── github │ │ │ └── com │ │ │ └── st235 │ │ │ └── samuraiview │ │ │ ├── utils │ │ │ ├── Dimens.kt │ │ │ └── BitmapHelper.kt │ │ │ ├── NextButtonViewController.kt │ │ │ ├── MainActivity.kt │ │ │ └── components │ │ │ └── CircularImageView.kt │ │ └── AndroidManifest.xml ├── proguard-rules.pro └── build.gradle ├── lib-samurai ├── .gitignore ├── consumer-rules.pro ├── proguard-rules.pro ├── gradle.properties ├── src │ └── main │ │ ├── res │ │ └── values │ │ │ └── strings.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ └── github │ │ └── com │ │ └── st235 │ │ └── lib_samurai │ │ ├── geometry │ │ ├── Vector2.kt │ │ └── Basis.kt │ │ ├── utils │ │ ├── ViewExt.kt │ │ ├── SamuraiAnimation.kt │ │ ├── RectExt.kt │ │ ├── Dimens.kt │ │ └── ViewLoadingRequest.kt │ │ ├── SamuraiTooltip.kt │ │ ├── Harakiri.kt │ │ └── SamuraiView.kt ├── build.gradle └── maven.gradle ├── images ├── author.png ├── share.png ├── showcase.gif └── introduction.png ├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradle.properties ├── LICENSE ├── .gitignore ├── gradlew.bat ├── README.md └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /lib-samurai/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /lib-samurai/consumer-rules.pro: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /lib-samurai/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /images/author.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/st235/SamuraiView/HEAD/images/author.png -------------------------------------------------------------------------------- /images/share.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/st235/SamuraiView/HEAD/images/share.png -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':lib-samurai' 2 | rootProject.name='SamuraiView' 3 | -------------------------------------------------------------------------------- /images/showcase.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/st235/SamuraiView/HEAD/images/showcase.gif -------------------------------------------------------------------------------- /images/introduction.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/st235/SamuraiView/HEAD/images/introduction.png -------------------------------------------------------------------------------- /lib-samurai/gradle.properties: -------------------------------------------------------------------------------- 1 | POM_NAME=samuraiview 2 | POM_ARTIFACT_ID=samuraiview 3 | POM_PACKAGING=aar 4 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/photo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/st235/SamuraiView/HEAD/app/src/main/res/drawable/photo.jpg -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/st235/SamuraiView/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/drawable/avatar.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/st235/SamuraiView/HEAD/app/src/main/res/drawable/avatar.jpg -------------------------------------------------------------------------------- /lib-samurai/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | lib-showcase 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/st235/SamuraiView/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/st235/SamuraiView/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/st235/SamuraiView/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/st235/SamuraiView/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/st235/SamuraiView/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/st235/SamuraiView/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/st235/SamuraiView/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/st235/SamuraiView/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/st235/SamuraiView/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/st235/SamuraiView/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /lib-samurai/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sat Feb 06 09:32:45 EET 2021 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip 7 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/chip_background_active.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/chip_background_inactive.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /lib-samurai/src/main/java/github/com/st235/lib_samurai/geometry/Vector2.kt: -------------------------------------------------------------------------------- 1 | package github.com.st235.lib_samurai.geometry 2 | 3 | internal data class Vector2(val x: Float, val y: Float) { 4 | 5 | operator fun minus(another: Vector2): Vector2 { 6 | return Vector2(x - another.x, y - another.y) 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /lib-samurai/src/main/java/github/com/st235/lib_samurai/utils/ViewExt.kt: -------------------------------------------------------------------------------- 1 | package github.com.st235.lib_samurai.utils 2 | 3 | import android.graphics.RectF 4 | import android.view.View 5 | import android.view.ViewTreeObserver 6 | import github.com.st235.lib_samurai.geometry.Vector2 7 | 8 | internal fun View.transformGeometry(origin: Vector2) = 9 | RectF(origin.x, origin.y, origin.x + width, origin.y + height) 10 | -------------------------------------------------------------------------------- /app/src/main/res/menu/toolbar_menu.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/chip_image.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 13 | 14 | -------------------------------------------------------------------------------- /app/src/main/java/github/com/st235/samuraiview/utils/Dimens.kt: -------------------------------------------------------------------------------- 1 | package github.com.st235.samuraiview.utils 2 | 3 | import android.content.res.Resources 4 | import android.util.TypedValue 5 | import androidx.annotation.Dimension 6 | import androidx.annotation.Px 7 | 8 | import androidx.annotation.Dimension.DP 9 | 10 | @Px 11 | fun dpToPx(@Dimension(unit = DP) dp: Int): Int { 12 | return TypedValue.applyDimension( 13 | TypedValue.COMPLEX_UNIT_DIP, dp.toFloat(), 14 | Resources.getSystem().displayMetrics 15 | ).toInt() 16 | } 17 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | @color/colorWhite 4 | @color/colorGreyOp80 5 | @color/colorGreyOp80 6 | 7 | #FFFFFF 8 | #14000000 9 | #28000000 10 | #CC000000 11 | 12 | #F2FFFFFF 13 | 14 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/chip_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_like.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 10 | 13 | 14 | -------------------------------------------------------------------------------- /lib-samurai/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | apply from: './maven.gradle' 5 | 6 | android { 7 | compileSdkVersion 30 8 | 9 | defaultConfig { 10 | minSdkVersion 16 11 | targetSdkVersion 30 12 | versionCode project.property('VERSION_CODE') as int 13 | versionName project.property('VERSION_NAME') 14 | } 15 | } 16 | 17 | dependencies { 18 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 19 | implementation 'androidx.appcompat:appcompat:1.2.0' 20 | } 21 | -------------------------------------------------------------------------------- /lib-samurai/src/main/java/github/com/st235/lib_samurai/geometry/Basis.kt: -------------------------------------------------------------------------------- 1 | package github.com.st235.lib_samurai.geometry 2 | 3 | import android.view.View 4 | 5 | internal class Basis(originView: View) { 6 | 7 | private val origin = originView.toWindowBasis() 8 | 9 | fun moveViewInto(view: View): Vector2 = view.toWindowBasis() - origin 10 | 11 | private fun View.toWindowBasis(): Vector2 { 12 | val onScreenCoordinates = IntArray(2) 13 | getLocationInWindow(onScreenCoordinates) 14 | return Vector2( 15 | onScreenCoordinates[0].toFloat(), 16 | onScreenCoordinates[1].toFloat() 17 | ) 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | org.gradle.jvmargs=-Xmx1536m 2 | android.useAndroidX=true 3 | android.enableJetifier=true 4 | kotlin.code.style=official 5 | 6 | GROUP=com.github.st235 7 | 8 | VERSION_CODE=8 9 | VERSION_NAME=1.0.0 10 | 11 | POM_DESCRIPTION=Simple showcase library. Made Samurai great again! 12 | POM_URL=https://github.com/st235/SamuraiView 13 | POM_SCM_URL=https://github.com/st235/SamuraiView.git 14 | POM_SCM_CONNECTION=git@github.com:st235/SamuraiView.git 15 | POM_SCM_DEV_CONNECTION=git@github.com:st235/SamuraiView.git 16 | 17 | POM_LICENCE_NAME=The MIT License 18 | POM_LICENCE_URL=https://opensource.org/licenses/MIT 19 | POM_LICENCE_DIST=repo 20 | POM_DEVELOPER_ID=st235 21 | POM_DEVELOPER_NAME=Alex Dadukin 22 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_share.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 10 | 13 | 14 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/github/com/st235/samuraiview/NextButtonViewController.kt: -------------------------------------------------------------------------------- 1 | package github.com.st235.samuraiview 2 | 3 | import android.view.View 4 | 5 | class NextButtonViewController(nextButton: View, private val onStep: (Int) -> Unit) { 6 | 7 | private var currentStep = 0 8 | 9 | init { 10 | nextButton.setOnClickListener { 11 | currentStep += 1 12 | if (currentStep >= LAST) currentStep = LAST 13 | onStep(currentStep) 14 | } 15 | 16 | onStep(currentStep) 17 | } 18 | 19 | fun back(): Boolean { 20 | if (currentStep <= INTRODUCTION) { 21 | return false 22 | } 23 | currentStep -= 1 24 | onStep(currentStep) 25 | return true 26 | } 27 | 28 | companion object { 29 | const val INTRODUCTION = 0 30 | const val AUTHOR = 1 31 | const val SHARE = 2 32 | const val LAST = 3 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 20 | 21 | -------------------------------------------------------------------------------- /lib-samurai/src/main/java/github/com/st235/lib_samurai/utils/SamuraiAnimation.kt: -------------------------------------------------------------------------------- 1 | package github.com.st235.lib_samurai.utils 2 | 3 | import android.animation.ValueAnimator 4 | import android.graphics.RectF 5 | import android.view.animation.AccelerateDecelerateInterpolator 6 | import github.com.st235.lib_samurai.SamuraiView 7 | 8 | internal fun SamuraiView.highlight( 9 | duration: Long, extraMargins: RectF = 10 | RectF(12.toFloatPx(), 12.toFloatPx(), 12.toFloatPx(), 12.toFloatPx()) 11 | ) { 12 | val animator = ValueAnimator.ofFloat(0F, 0.5F, 0F) 13 | animator.repeatCount = ValueAnimator.INFINITE 14 | animator.interpolator = AccelerateDecelerateInterpolator() 15 | animator.duration = duration 16 | animator.addUpdateListener { 17 | val multipliedMargins = 18 | showcaseMargins.offsetFor(extraMargins.multiply(it.animatedValue as Float)) 19 | applyShowcaseFrame(multipliedMargins) 20 | invalidate() 21 | } 22 | animator.start() 23 | } 24 | -------------------------------------------------------------------------------- /app/src/main/res/layout/tooltip_image.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 16 | 17 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /app/src/main/res/layout/tooltip_share.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 16 | 17 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /app/src/main/res/layout/tooltip_author.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 16 | 17 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /lib-samurai/src/main/java/github/com/st235/lib_samurai/utils/RectExt.kt: -------------------------------------------------------------------------------- 1 | package github.com.st235.lib_samurai.utils 2 | 3 | import android.graphics.RectF 4 | 5 | internal enum class Relation { 6 | INDEPENDENT, CONTAINS, INTERSECT 7 | } 8 | internal fun RectF.multiply(times: Float) = 9 | RectF(left * times, top * times, right * times, bottom * times) 10 | 11 | internal fun RectF.offsetFor(another: RectF) = 12 | RectF(left + another.left, top + another.top, right + another.right, bottom + another.bottom) 13 | 14 | internal fun RectF.applyMargins(margins: RectF) { 15 | left -= margins.left 16 | right += margins.right 17 | top -= margins.top 18 | bottom += margins.bottom 19 | } 20 | 21 | internal fun RectF.calculateRelation(another: RectF): Relation { 22 | if (this.contains(another)) { 23 | return Relation.CONTAINS 24 | } 25 | 26 | if (this.left < another.right && another.left < this.right 27 | && this.top < another.bottom && another.top < this.bottom) { 28 | return Relation.INTERSECT 29 | } 30 | 31 | return Relation.INDEPENDENT 32 | } 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Alexander Dadukin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | 5 | android { 6 | compileSdkVersion 29 7 | defaultConfig { 8 | applicationId "github.com.st235.samuraiview" 9 | minSdkVersion 16 10 | targetSdkVersion 29 11 | versionCode 2 12 | versionName "1.1.0" 13 | 14 | vectorDrawables.useSupportLibrary = true 15 | } 16 | buildTypes { 17 | release { 18 | minifyEnabled false 19 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | } 23 | 24 | dependencies { 25 | implementation project(':lib-samurai') 26 | 27 | implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 28 | implementation 'androidx.appcompat:appcompat:1.1.0' 29 | implementation 'androidx.core:core-ktx:1.2.0' 30 | implementation 'com.google.android.material:material:1.1.0' 31 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 32 | implementation 'com.github.st235:flow-layout:1.1.1' 33 | } 34 | -------------------------------------------------------------------------------- /lib-samurai/src/main/java/github/com/st235/lib_samurai/utils/Dimens.kt: -------------------------------------------------------------------------------- 1 | package github.com.st235.lib_samurai.utils 2 | 3 | import android.content.res.Resources 4 | import android.util.TypedValue 5 | import androidx.annotation.Px 6 | 7 | /** 8 | * Converts values to its real pixel size 9 | * using system density factor 10 | * 11 | * @return value in pixels 12 | */ 13 | @Px 14 | internal fun Int.toPx(): Int { 15 | return TypedValue.applyDimension( 16 | TypedValue.COMPLEX_UNIT_DIP, this.toFloat(), 17 | Resources.getSystem().displayMetrics).toInt() 18 | } 19 | 20 | /** 21 | * Converts values to its real pixel size 22 | * using system density factor 23 | * 24 | * @return value in pixels 25 | */ 26 | @Px 27 | internal fun Int.toFloatPx(): Float { 28 | return TypedValue.applyDimension( 29 | TypedValue.COMPLEX_UNIT_DIP, this.toFloat(), 30 | Resources.getSystem().displayMetrics).toFloat() 31 | } 32 | 33 | /** 34 | * Converts values to its real pixel size 35 | * using system density factor 36 | * 37 | * @return value in pixels 38 | */ 39 | internal fun Float.toPx(): Float { 40 | return TypedValue.applyDimension( 41 | TypedValue.COMPLEX_UNIT_DIP, this, 42 | Resources.getSystem().displayMetrics) 43 | } 44 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | # IntelliJ 36 | *.iml 37 | .idea/workspace.xml 38 | .idea/tasks.xml 39 | .idea/gradle.xml 40 | .idea/assetWizardSettings.xml 41 | .idea/dictionaries 42 | .idea/libraries 43 | .idea/caches 44 | 45 | # Keystore files 46 | # Uncomment the following line if you do not want to check your keystore files in. 47 | #*.jks 48 | 49 | # External native build folder generated in Android Studio 2.2 and later 50 | .externalNativeBuild 51 | 52 | # Google Services (e.g. APIs or Firebase) 53 | google-services.json 54 | 55 | # Freeline 56 | freeline.py 57 | freeline/ 58 | freeline_project_description.json 59 | 60 | # fastlane 61 | fastlane/report.xml 62 | fastlane/Preview.html 63 | fastlane/screenshots 64 | fastlane/test_output 65 | fastlane/readme.md 66 | 67 | .idea/ 68 | -------------------------------------------------------------------------------- /lib-samurai/src/main/java/github/com/st235/lib_samurai/utils/ViewLoadingRequest.kt: -------------------------------------------------------------------------------- 1 | package github.com.st235.lib_samurai.utils 2 | 3 | import android.view.View 4 | import android.view.ViewTreeObserver 5 | 6 | class ViewLoadingRequest( 7 | private val view: V, 8 | private val onViewReady: (view: View) -> Unit 9 | ): View.OnAttachStateChangeListener, ViewTreeObserver.OnPreDrawListener { 10 | 11 | init { 12 | view.addOnAttachStateChangeListener(this) 13 | 14 | if (view.windowToken != null) { 15 | onViewAttachedToWindow(view) 16 | } 17 | } 18 | 19 | override fun onViewAttachedToWindow(p0: View?) { 20 | view.viewTreeObserver.addOnPreDrawListener(this) 21 | } 22 | 23 | override fun onViewDetachedFromWindow(p0: View?) { 24 | view.viewTreeObserver.removeOnPreDrawListener(this) 25 | } 26 | 27 | override fun onPreDraw(): Boolean { 28 | val viewTreeObserver = view.viewTreeObserver 29 | 30 | if (!viewTreeObserver.isAlive) { 31 | return true 32 | } 33 | 34 | if (view.width <= 0 || view.height <= 0) { 35 | return true 36 | } 37 | 38 | view.removeOnAttachStateChangeListener(this) 39 | viewTreeObserver.removeOnPreDrawListener(this) 40 | 41 | onViewReady(view) 42 | 43 | return true 44 | } 45 | } -------------------------------------------------------------------------------- /app/src/main/java/github/com/st235/samuraiview/utils/BitmapHelper.kt: -------------------------------------------------------------------------------- 1 | package github.com.st235.samuraiview.utils 2 | 3 | import android.content.res.Resources 4 | import android.graphics.Bitmap 5 | import android.graphics.BitmapFactory 6 | import androidx.annotation.CheckResult 7 | import androidx.annotation.DrawableRes 8 | import androidx.annotation.IntRange 9 | 10 | object BitmapHelper { 11 | 12 | @CheckResult 13 | fun decodeSampledBitmapFromResource( 14 | res: Resources, 15 | @DrawableRes resId: Int, 16 | @IntRange(from = 0) reqWidth: Int, 17 | @IntRange(from = 0) reqHeight: Int 18 | ): Bitmap { 19 | val options = BitmapFactory.Options() 20 | options.inJustDecodeBounds = true 21 | BitmapFactory.decodeResource(res, resId, options) 22 | 23 | options.inSampleSize = 24 | calculateInSampleSize(options, reqWidth, reqHeight) 25 | 26 | options.inJustDecodeBounds = false 27 | return BitmapFactory.decodeResource(res, resId, options) 28 | } 29 | 30 | @CheckResult 31 | private fun calculateInSampleSize( 32 | options: BitmapFactory.Options, 33 | @IntRange(from = 0) reqWidth: Int, 34 | @IntRange(from = 0) reqHeight: Int 35 | ): Int { 36 | val height = options.outHeight 37 | val width = options.outWidth 38 | var inSampleSize = 1 39 | 40 | if (height > reqHeight || width > reqWidth) { 41 | 42 | val halfHeight = height / 2 43 | val halfWidth = width / 2 44 | 45 | while (halfHeight / inSampleSize >= reqHeight && halfWidth / inSampleSize >= reqWidth) { 46 | inSampleSize *= 2 47 | } 48 | } 49 | 50 | return inSampleSize 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | SamuraiView Demo App 3 | Share 4 | Share Github link via: 5 | Do u use Kotlin in your project and need a showcase library? Take a look for: https://github.com/st235/SamuraiView 6 | 7 | If a Samurai was likely to be captured by an enemy or did not follow bushido he was supposed to kill himself in a ritual called seppuku that involved using a sword or blade to pierce the stomach, followed by beheading. It could be performed quickly if needed or as a long ritual. 8 | 9 | samurai 10 | view 11 | fun facts 12 | showcase 13 | highlight 14 | capturing 15 | samuraiview 16 | github 17 | st235 18 | ⚔️ 19 | library 20 | android 21 | kotlin 22 | 23 | 24 | Next 25 | Finish 26 | 27 | Hey, I am Samurai View ⚔️ 28 | I can highlight some UI elements, may add useful tooltip and show animations 29 | 30 | This is my author 31 | Follow him on Github: @st235 32 | 33 | Share it faster, faster! 34 | Share my repos link with your friends 35 | 36 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 15 | 16 | 21 | 22 | 23 | 24 | 28 | 29 | 30 | 31 | 32 | 36 | 37 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /lib-samurai/src/main/java/github/com/st235/lib_samurai/SamuraiTooltip.kt: -------------------------------------------------------------------------------- 1 | package github.com.st235.lib_samurai 2 | 3 | import android.content.Context 4 | import android.graphics.RectF 5 | import android.view.LayoutInflater 6 | import android.view.View 7 | import android.view.ViewGroup 8 | import androidx.annotation.LayoutRes 9 | 10 | internal enum class Fit { 11 | BOTTOM, TOP, NOWHERE 12 | } 13 | 14 | abstract class SamuraiTooltip( 15 | private val width: Int = ViewGroup.LayoutParams.WRAP_CONTENT, 16 | private val height: Int = ViewGroup.LayoutParams.WRAP_CONTENT 17 | ) { 18 | abstract fun getView(context: Context, parent: ViewGroup): View 19 | 20 | fun getLayoutParams(): ViewGroup.MarginLayoutParams = 21 | ViewGroup.MarginLayoutParams(width, height) 22 | 23 | companion object { 24 | fun createForLayout( 25 | @LayoutRes layoutId: Int, 26 | width: Int = ViewGroup.LayoutParams.WRAP_CONTENT, 27 | height: Int = ViewGroup.LayoutParams.WRAP_CONTENT 28 | ): SamuraiTooltip = ResourceTooltip(layoutId, width, height) 29 | 30 | fun createForView( 31 | view: View, 32 | width: Int = ViewGroup.LayoutParams.WRAP_CONTENT, 33 | height: Int = ViewGroup.LayoutParams.WRAP_CONTENT 34 | ): SamuraiTooltip = ViewTooltip(view, width, height) 35 | } 36 | } 37 | 38 | internal class ViewTooltip(val view: View, width: Int, height: Int): SamuraiTooltip(width, height) { 39 | override fun getView(context: Context, parent: ViewGroup): View = view 40 | } 41 | 42 | internal class ResourceTooltip(@LayoutRes val layoutId: Int, width: Int, height: Int): SamuraiTooltip(width, height) { 43 | private var v: View? = null 44 | 45 | override fun getView(context: Context, parent: ViewGroup): View { 46 | if (v == null) { 47 | v = LayoutInflater.from(context).inflate(layoutId, parent, false) 48 | } 49 | return v!! 50 | } 51 | } 52 | 53 | internal fun calculateFitModeForTooltip(v: View, into: RectF, showcase: RectF): Fit { 54 | v.measure( 55 | View.MeasureSpec.makeMeasureSpec(into.width().toInt(), View.MeasureSpec.AT_MOST), 56 | View.MeasureSpec.makeMeasureSpec(into.height().toInt(), View.MeasureSpec.AT_MOST) 57 | ) 58 | 59 | val height = v.measuredHeight 60 | 61 | val fromTop = showcase.top - into.top 62 | val fromBottom = into.bottom - showcase.bottom 63 | 64 | return when { 65 | height < fromTop -> Fit.TOP 66 | height < fromBottom -> Fit.BOTTOM 67 | else -> Fit.NOWHERE 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /lib-samurai/maven.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 Chris Banes 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | apply plugin: 'maven' 18 | apply plugin: 'signing' 19 | 20 | def isReleaseBuild() { 21 | return VERSION_NAME.contains("SNAPSHOT") == false 22 | } 23 | 24 | def getReleaseRepositoryUrl() { 25 | return hasProperty('RELEASE_REPOSITORY_URL') ? RELEASE_REPOSITORY_URL 26 | : "https://oss.sonatype.org/service/local/staging/deploy/maven2/" 27 | } 28 | 29 | def getSnapshotRepositoryUrl() { 30 | return hasProperty('SNAPSHOT_REPOSITORY_URL') ? SNAPSHOT_REPOSITORY_URL 31 | : "https://oss.sonatype.org/content/repositories/snapshots/" 32 | } 33 | 34 | def getRepositoryUsername() { 35 | return hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : "" 36 | } 37 | 38 | def getRepositoryPassword() { 39 | return hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : "" 40 | } 41 | 42 | afterEvaluate { project -> 43 | uploadArchives { 44 | repositories { 45 | mavenDeployer { 46 | beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } 47 | 48 | pom.groupId = GROUP 49 | pom.artifactId = POM_ARTIFACT_ID 50 | pom.version = VERSION_NAME 51 | 52 | repository(url: getReleaseRepositoryUrl()) { 53 | authentication(userName: getRepositoryUsername(), password: getRepositoryPassword()) 54 | } 55 | snapshotRepository(url: getSnapshotRepositoryUrl()) { 56 | authentication(userName: getRepositoryUsername(), password: getRepositoryPassword()) 57 | } 58 | 59 | pom.project { 60 | name POM_NAME 61 | packaging POM_PACKAGING 62 | description POM_DESCRIPTION 63 | url POM_URL 64 | 65 | scm { 66 | url POM_SCM_URL 67 | connection POM_SCM_CONNECTION 68 | developerConnection POM_SCM_DEV_CONNECTION 69 | } 70 | 71 | licenses { 72 | license { 73 | name POM_LICENCE_NAME 74 | url POM_LICENCE_URL 75 | distribution POM_LICENCE_DIST 76 | } 77 | } 78 | 79 | developers { 80 | developer { 81 | id POM_DEVELOPER_ID 82 | name POM_DEVELOPER_NAME 83 | } 84 | } 85 | } 86 | } 87 | } 88 | } 89 | 90 | signing { 91 | required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") } 92 | sign configurations.archives 93 | } 94 | 95 | 96 | task androidSourcesJar(type: Jar) { 97 | classifier = 'sources' 98 | from android.sourceSets.main.java.sourceFiles 99 | } 100 | 101 | artifacts { 102 | archives androidSourcesJar 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # SamuraiView 4 | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.st235/samuraiview/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.st235/samuraiview) 5 | 6 | Simple showcase library. Made Samurai great again! ⚔️ 7 | 8 | ## Download 9 | 10 | __Important: library was migrated from JCenter to MavenCentral__ 11 | 12 | It means that it may be necessary to add __mavenCentral__ repository to your repositories list 13 | 14 | ```groovy 15 | allprojects { 16 | repositories { 17 | // your repositories 18 | 19 | mavenCentral() 20 | } 21 | } 22 | ``` 23 | 24 | - Maven 25 | 26 | ```text 27 | 28 | com.github.st235 29 | samuraiview 30 | X.X 31 | pom 32 | 33 | ``` 34 | 35 | - Gradle 36 | 37 | ```text 38 | implementation 'com.github.st235:samuraiview:X.X' 39 | ``` 40 | 41 | - Ivy 42 | 43 | ```text 44 | 45 | 46 | 47 | ``` 48 | 49 | 50 | ## Usage 51 | 52 | First of all, you should declare your SamuraiView at xml file 53 | 54 | ```xml 55 | 59 | 60 | 68 | 69 | ``` 70 | 71 | Yeap, forget to say: SamuraiView is a viewgroup, so you may add children to it directly in xml 72 | 73 | Then to highlight some views just `capture` it! 74 | 75 | ```kotlin 76 | Harakiri(into = samuraiView) 77 | .circle() 78 | .overlayColorRes(R.color.colorWhite95) 79 | .frameColorRes(R.color.colorPrimaryDark) 80 | .frameThickness(2F) 81 | .margins(20, 20, 20, 20) 82 | .withTooltip(R.layout.tooltip_share, width = ViewGroup.LayoutParams.MATCH_PARENT) 83 | .capture(toolbar.findViewById(R.id.action_share)) 84 | ``` 85 | 86 | ## Screens 87 | 88 | 89 | 90 | ### Licenses 91 | 92 | Photos were taken from [unsplash](https://unsplash.com/) 93 | 94 | ```text 95 | MIT License 96 | 97 | Copyright (c) 2019 Alexander Dadukin 98 | 99 | Permission is hereby granted, free of charge, to any person obtaining a copy 100 | of this software and associated documentation files (the "Software"), to deal 101 | in the Software without restriction, including without limitation the rights 102 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 103 | copies of the Software, and to permit persons to whom the Software is 104 | furnished to do so, subject to the following conditions: 105 | 106 | The above copyright notice and this permission notice shall be included in all 107 | copies or substantial portions of the Software. 108 | 109 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 110 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 111 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 112 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 113 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 114 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 115 | SOFTWARE. 116 | ``` 117 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 10 | 12 | 14 | 16 | 18 | 20 | 22 | 24 | 26 | 28 | 30 | 32 | 34 | 36 | 38 | 40 | 42 | 44 | 46 | 48 | 50 | 52 | 54 | 56 | 58 | 60 | 62 | 64 | 66 | 68 | 70 | 72 | 74 | 75 | -------------------------------------------------------------------------------- /app/src/main/res/layout/content_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 23 | 24 | 37 | 38 | 43 | 44 | 56 | 57 | 58 | 71 | 72 | 79 | 80 | 89 | 90 | 91 | 106 | 107 | 122 | 123 | -------------------------------------------------------------------------------- /app/src/main/java/github/com/st235/samuraiview/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package github.com.st235.samuraiview 2 | 3 | import android.content.Intent 4 | import android.os.Bundle 5 | import android.view.Menu 6 | import android.view.MenuItem 7 | import android.view.View 8 | import android.view.ViewGroup 9 | import android.widget.LinearLayout 10 | import android.widget.TextView 11 | import androidx.annotation.Px 12 | import androidx.appcompat.app.AppCompatActivity 13 | import androidx.appcompat.view.ContextThemeWrapper 14 | import androidx.appcompat.widget.AppCompatImageView 15 | import androidx.appcompat.widget.Toolbar 16 | import github.com.st235.lib_samurai.Harakiri 17 | import github.com.st235.lib_samurai.SamuraiView 18 | import github.com.st235.samuraiview.components.CircularImageView 19 | import github.com.st235.samuraiview.utils.BitmapHelper 20 | import github.com.st235.samuraiview.utils.dpToPx 21 | import st235.com.github.flow_layout.FlowLayout 22 | 23 | class MainActivity : AppCompatActivity() { 24 | 25 | private lateinit var userName: TextView 26 | private lateinit var samuraiView: SamuraiView 27 | private lateinit var avatar: CircularImageView 28 | private lateinit var feedImage: AppCompatImageView 29 | 30 | private lateinit var nextButtonViewController: NextButtonViewController 31 | 32 | override fun onCreate(savedInstanceState: Bundle?) { 33 | super.onCreate(savedInstanceState) 34 | setContentView(R.layout.activity_main) 35 | val toolbar = findViewById(R.id.toolbar) 36 | setSupportActionBar(toolbar) 37 | supportActionBar?.setDisplayHomeAsUpEnabled(true) 38 | 39 | avatar = findViewById(R.id.avatar) 40 | userName = findViewById(R.id.user) 41 | feedImage = findViewById(R.id.insta_image) 42 | feedImage.setImageBitmap( 43 | BitmapHelper.decodeSampledBitmapFromResource( 44 | resources, 45 | R.drawable.photo, PROFILE_PICTURE_SIZE, PROFILE_PICTURE_SIZE 46 | ) 47 | ) 48 | 49 | val tagsChipLayout = findViewById(R.id.tag_layout) 50 | val tags = resources.getStringArray(R.array.tags) 51 | 52 | for (tag in tags) { 53 | addChildTag(tagsChipLayout, tag) 54 | } 55 | 56 | samuraiView = findViewById(R.id.samurai_view) 57 | samuraiView.visibility = View.GONE 58 | 59 | nextButtonViewController = NextButtonViewController(findViewById(R.id.samurai_next_view)) { 60 | when(it) { 61 | NextButtonViewController.INTRODUCTION -> 62 | Harakiri(into = samuraiView) 63 | .overlayColorRes(R.color.colorWhite95) 64 | .withTooltip(R.layout.tooltip_image, width = ViewGroup.LayoutParams.MATCH_PARENT) 65 | .capture(feedImage) 66 | NextButtonViewController.AUTHOR -> 67 | Harakiri(into = samuraiView) 68 | .rect(16F) 69 | .overlayColorRes(R.color.colorWhite95) 70 | .frameColorRes(R.color.colorPrimaryDark) 71 | .frameThickness(1F) 72 | .margins(0, 0, 6, 0) 73 | .withTooltip(R.layout.tooltip_author, width = ViewGroup.LayoutParams.MATCH_PARENT) 74 | .capture(avatar, userName) 75 | NextButtonViewController.SHARE -> 76 | Harakiri(into = samuraiView) 77 | .circle() 78 | .overlayColorRes(R.color.colorWhite95) 79 | .frameColorRes(R.color.colorPrimaryDark) 80 | .frameThickness(2F) 81 | .margins(20, 20, 20, 20) 82 | .withTooltip(R.layout.tooltip_share, width = ViewGroup.LayoutParams.MATCH_PARENT) 83 | .capture(toolbar.findViewById(R.id.action_share)) 84 | NextButtonViewController.LAST -> 85 | samuraiView.visibility = View.GONE 86 | } 87 | } 88 | } 89 | 90 | override fun onCreateOptionsMenu(menu: Menu): Boolean { 91 | menuInflater.inflate(R.menu.toolbar_menu, menu) 92 | return true 93 | } 94 | 95 | override fun onOptionsItemSelected(item: MenuItem): Boolean { 96 | when (item.itemId) { 97 | R.id.action_share -> { 98 | shareLibrary() 99 | return true 100 | } 101 | else -> return super.onOptionsItemSelected(item) 102 | } 103 | } 104 | 105 | private fun addChildTag( 106 | tagLayout: ViewGroup, 107 | tag: String 108 | ) { 109 | val tagView = TextView(ContextThemeWrapper(this, R.style.ChipViewTextAppearance)) 110 | tagView.text = tag 111 | 112 | val params = LinearLayout.LayoutParams( 113 | ViewGroup.LayoutParams.WRAP_CONTENT, 114 | ViewGroup.LayoutParams.WRAP_CONTENT 115 | ) 116 | 117 | params.setMargins(dpToPx(2), dpToPx(2), dpToPx(2), dpToPx(2)) 118 | 119 | tagLayout.addView(tagView, params) 120 | } 121 | 122 | override fun onBackPressed() { 123 | if (!nextButtonViewController.back()) { 124 | super.onBackPressed() 125 | } 126 | } 127 | 128 | private fun shareLibrary() { 129 | val shareBody = getString(R.string.share_text) 130 | val sharingIntent = Intent(Intent.ACTION_SEND) 131 | sharingIntent.type = "text/plain" 132 | sharingIntent.putExtra(Intent.EXTRA_TEXT, shareBody) 133 | startActivity( 134 | Intent.createChooser( 135 | sharingIntent, 136 | getString(R.string.share_chooser) 137 | ) 138 | ) 139 | } 140 | 141 | companion object { 142 | @Px 143 | private val PROFILE_PICTURE_SIZE = 512 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /lib-samurai/src/main/java/github/com/st235/lib_samurai/Harakiri.kt: -------------------------------------------------------------------------------- 1 | package github.com.st235.lib_samurai 2 | 3 | import android.graphics.Color 4 | import android.graphics.Rect 5 | import android.graphics.RectF 6 | import android.view.View 7 | import android.view.ViewGroup 8 | import androidx.annotation.ColorInt 9 | import androidx.annotation.ColorRes 10 | import androidx.annotation.Dimension 11 | import androidx.annotation.LayoutRes 12 | import androidx.core.content.ContextCompat 13 | import github.com.st235.lib_samurai.geometry.Basis 14 | import github.com.st235.lib_samurai.utils.* 15 | 16 | class Harakiri(private val into: SamuraiView) { 17 | 18 | private var highlightDuration = NO_ANIMATION 19 | 20 | private var collisionStrategy = CollisionStrategy.CRASH 21 | 22 | private var tooltip: SamuraiTooltip? = null 23 | 24 | private var type: SamuraiView.Type = SamuraiView.Type.RECT 25 | 26 | @ColorInt 27 | private var overlayColor: Int = Color.TRANSPARENT 28 | 29 | @ColorInt 30 | private var frameColor: Int = Color.TRANSPARENT 31 | 32 | @Dimension(unit = Dimension.DP) 33 | private var thickness: Float = 0F 34 | 35 | @Dimension(unit = Dimension.DP) 36 | private var roundRadius: Float = 0F 37 | 38 | private val margins = Rect() 39 | 40 | private val basis by lazy { 41 | Basis(into) 42 | } 43 | 44 | fun overlayColor(@ColorInt color: Int): Harakiri { 45 | this.overlayColor = color 46 | return this 47 | } 48 | 49 | fun overlayColorRes(@ColorRes color: Int): Harakiri { 50 | this.overlayColor = ContextCompat.getColor(into.context, color) 51 | return this 52 | } 53 | 54 | fun frameColor(@ColorInt color: Int): Harakiri { 55 | this.frameColor = color 56 | return this 57 | } 58 | 59 | fun frameColorRes(@ColorRes color: Int): Harakiri { 60 | this.frameColor = ContextCompat.getColor(into.context, color) 61 | return this 62 | } 63 | 64 | fun frameThickness(@Dimension(unit = Dimension.DP) thickness: Float): Harakiri { 65 | this.thickness = thickness 66 | return this 67 | } 68 | 69 | fun rect(@Dimension(unit = Dimension.DP) roundRadius: Float = 0F): Harakiri { 70 | type = SamuraiView.Type.RECT 71 | this.roundRadius = roundRadius 72 | return this 73 | } 74 | 75 | fun circle(): Harakiri { 76 | type = SamuraiView.Type.CIRCLE 77 | return this 78 | } 79 | 80 | fun withTooltip( 81 | @LayoutRes layoutId: Int, 82 | width: Int = ViewGroup.MarginLayoutParams.WRAP_CONTENT, 83 | height: Int = ViewGroup.MarginLayoutParams.WRAP_CONTENT 84 | ): Harakiri { 85 | this.tooltip = SamuraiTooltip.createForLayout(layoutId, width, height) 86 | return this 87 | } 88 | 89 | fun withTooltip( 90 | view: View, 91 | width: Int = ViewGroup.MarginLayoutParams.WRAP_CONTENT, 92 | height: Int = ViewGroup.MarginLayoutParams.WRAP_CONTENT 93 | ): Harakiri { 94 | this.tooltip = SamuraiTooltip.createForView(view, width, height) 95 | return this 96 | } 97 | 98 | fun collisionStrategy(strategy: CollisionStrategy = CollisionStrategy.CRASH): Harakiri { 99 | this.collisionStrategy = strategy 100 | return this 101 | } 102 | 103 | fun highlightAnimationWithDuration(highlightDuration: Long = NO_ANIMATION): Harakiri { 104 | this.highlightDuration = highlightDuration 105 | return this 106 | } 107 | 108 | fun margins( 109 | @Dimension(unit = Dimension.DP) left: Int, 110 | @Dimension(unit = Dimension.DP) top: Int, 111 | @Dimension(unit = Dimension.DP) right: Int, 112 | @Dimension(unit = Dimension.DP) bottom: Int 113 | ): Harakiri { 114 | margins.set(left, top, right, bottom) 115 | return this 116 | } 117 | 118 | fun capture(vararg views: View) { 119 | initOrigin() 120 | 121 | ViewLoadingRequest(into) { 122 | val resultRect = RectF(Float.MAX_VALUE, Float.MAX_VALUE, 0F, 0F) 123 | 124 | for (view in views) { 125 | val position = basis.moveViewInto(view) 126 | val bounds = view.transformGeometry(position) 127 | 128 | var isSkip = false 129 | if (into.viewFrame.calculateRelation(bounds) != Relation.CONTAINS) { 130 | isSkip = collisionStrategy.shouldSkipOnCollision(view) 131 | } 132 | 133 | if (!isSkip) { 134 | if (bounds.left < resultRect.left) resultRect.left = bounds.left 135 | if (bounds.right > resultRect.right) resultRect.right = bounds.right 136 | if (bounds.top < resultRect.top) resultRect.top = bounds.top 137 | if (bounds.bottom > resultRect.bottom) resultRect.bottom = bounds.bottom 138 | } 139 | 140 | into.setShowcase(resultRect) 141 | if (highlightDuration > 0L) { 142 | into.highlight(highlightDuration) 143 | } 144 | } 145 | } 146 | } 147 | 148 | private fun initOrigin() { 149 | if (into.visibility != View.VISIBLE) { 150 | into.visibility = View.VISIBLE 151 | } 152 | into.overlayColor = overlayColor 153 | into.frameColor = frameColor 154 | into.frameThickness = thickness 155 | into.tooltip = tooltip 156 | into.type = type 157 | into.rectRoundRadius = roundRadius 158 | into.setShowcaseMargins(margins.left, margins.top, margins.right, margins.bottom) 159 | } 160 | 161 | enum class CollisionStrategy { 162 | CRASH { 163 | override fun shouldSkipOnCollision(view: View): Boolean { 164 | throw IllegalStateException("$view has intersect or out of the overlay bound") 165 | } 166 | }, 167 | SKIP { 168 | override fun shouldSkipOnCollision(view: View): Boolean = true 169 | }, 170 | FORCE_ADD { 171 | override fun shouldSkipOnCollision(view: View): Boolean = false 172 | }; 173 | 174 | abstract fun shouldSkipOnCollision(view: View): Boolean 175 | } 176 | 177 | companion object { 178 | const val NO_ANIMATION = 0L 179 | } 180 | } -------------------------------------------------------------------------------- /lib-samurai/src/main/java/github/com/st235/lib_samurai/SamuraiView.kt: -------------------------------------------------------------------------------- 1 | package github.com.st235.lib_samurai 2 | 3 | import android.content.Context 4 | import android.graphics.* 5 | import android.util.AttributeSet 6 | import android.widget.FrameLayout 7 | import androidx.annotation.ColorInt 8 | import androidx.annotation.ColorRes 9 | import androidx.annotation.Dimension 10 | import github.com.st235.lib_samurai.utils.applyMargins 11 | import github.com.st235.lib_samurai.utils.toFloatPx 12 | import github.com.st235.lib_samurai.utils.toPx 13 | 14 | 15 | class SamuraiView @JvmOverloads constructor( 16 | context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 17 | ) : FrameLayout(context, attrs, defStyleAttr) { 18 | 19 | enum class Type { 20 | CIRCLE, 21 | RECT 22 | } 23 | 24 | private val showcaseBounds = RectF() 25 | internal val showcaseMargins = RectF() 26 | 27 | internal val viewFrame = RectF() 28 | private val showcaseFrame = RectF() 29 | 30 | private val framePath = Path() 31 | private val overlayPath = Path() 32 | 33 | private val framePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { 34 | style = Paint.Style.STROKE 35 | } 36 | 37 | private val overlayPaint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG).apply { 38 | style = Paint.Style.FILL 39 | } 40 | 41 | @ColorInt 42 | var overlayColor = Color.TRANSPARENT 43 | set(value) { 44 | field = value 45 | invalidate() 46 | } 47 | 48 | @ColorRes 49 | var overlayColorRes = 0 50 | set(value) { 51 | field = value 52 | overlayColor = context.resources.getColor(value) 53 | } 54 | 55 | @ColorInt 56 | var frameColor = Color.TRANSPARENT 57 | set(value) { 58 | field = value 59 | invalidate() 60 | } 61 | 62 | @ColorRes 63 | var frameColorRes = 0 64 | set(value) { 65 | field = value 66 | frameColor = context.resources.getColor(value) 67 | } 68 | 69 | @Dimension(unit = Dimension.DP) 70 | var frameThickness = 1F 71 | set(value) { 72 | field = value 73 | invalidate() 74 | } 75 | 76 | var rectRoundRadius = 0F 77 | set(value) { 78 | field = value 79 | invalidate() 80 | } 81 | 82 | var type: Type = Type.RECT 83 | set(value) { 84 | field = value 85 | invalidate() 86 | } 87 | 88 | var tooltip: SamuraiTooltip? = null 89 | set(value) { 90 | if (field != null) { 91 | removeView(field?.getView(context, this)) 92 | } 93 | field = value 94 | } 95 | 96 | init { 97 | isFocusable = true 98 | isClickable = true 99 | setWillNotDraw(false) 100 | } 101 | 102 | fun setShowcase(bounds: RectF) { 103 | if (bounds.width() <= 0 || 104 | bounds.height() <= 0 105 | ) { 106 | throw IllegalStateException("bounds are invalid") 107 | } 108 | 109 | showcaseBounds.set(bounds) 110 | applyShowcaseFrame() 111 | recalculateTooltip() 112 | invalidate() 113 | } 114 | 115 | fun setShowcaseMargins( 116 | @Dimension(unit = Dimension.DP) left: Int, 117 | @Dimension(unit = Dimension.DP) top: Int, 118 | @Dimension(unit = Dimension.DP) right: Int, 119 | @Dimension(unit = Dimension.DP) bottom: Int 120 | ) { 121 | if (left < 0 || 122 | top < 0 || 123 | right < 0 || 124 | bottom < 0 125 | ) { 126 | throw IllegalStateException("margins should not be smaller than 0") 127 | } 128 | 129 | showcaseMargins.set(left.toFloatPx(), top.toFloatPx(), right.toFloatPx(), bottom.toFloatPx()) 130 | 131 | if (showcaseBounds.isEmpty) { 132 | return 133 | } 134 | 135 | applyShowcaseFrame() 136 | invalidate() 137 | } 138 | 139 | override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { 140 | super.onSizeChanged(w, h, oldw, oldh) 141 | viewFrame.set(0F, 0F, w.toFloat(), h.toFloat()) 142 | } 143 | 144 | override fun onDraw(canvas: Canvas?) { 145 | super.onDraw(canvas) 146 | 147 | if (viewFrame.isEmpty || showcaseBounds.isEmpty) { 148 | return 149 | } 150 | 151 | drawOverlay(canvas) 152 | drawFrame(canvas) 153 | } 154 | 155 | private fun drawOverlay(canvas: Canvas?) { 156 | if (overlayColor == Color.TRANSPARENT) { 157 | return 158 | } 159 | 160 | overlayPaint.color = overlayColor 161 | overlayPath.rewind() 162 | 163 | overlayPath.addRect(viewFrame, Path.Direction.CW) 164 | 165 | if (type == Type.CIRCLE) { 166 | overlayPath.addCircle( 167 | showcaseFrame.centerX(), 168 | showcaseFrame.centerY(), 169 | Math.max(showcaseFrame.width(), showcaseFrame.height()) / 2, 170 | Path.Direction.CCW 171 | ) 172 | } else { 173 | overlayPath.addRoundRect( 174 | showcaseFrame, 175 | rectRoundRadius.toPx(), 176 | rectRoundRadius.toPx(), 177 | Path.Direction.CCW 178 | ) 179 | } 180 | 181 | 182 | canvas?.drawPath(overlayPath, overlayPaint) 183 | } 184 | 185 | private fun drawFrame(canvas: Canvas?) { 186 | if (frameColor == Color.TRANSPARENT) { 187 | return 188 | } 189 | 190 | framePaint.color = frameColor 191 | framePaint.strokeWidth = frameThickness.toPx() 192 | framePath.rewind() 193 | 194 | if (type == Type.CIRCLE) { 195 | framePath.addCircle( 196 | showcaseFrame.centerX(), 197 | showcaseFrame.centerY(), 198 | Math.max(showcaseFrame.width(), showcaseFrame.height()) / 2, 199 | Path.Direction.CW 200 | ) 201 | } else { 202 | framePath.addRoundRect( 203 | showcaseFrame, 204 | rectRoundRadius.toPx(), 205 | rectRoundRadius.toPx(), 206 | Path.Direction.CW 207 | ) 208 | } 209 | 210 | canvas?.drawPath(framePath, framePaint) 211 | } 212 | 213 | internal fun applyShowcaseFrame(margins: RectF = showcaseMargins) { 214 | showcaseFrame.set(showcaseBounds) 215 | showcaseFrame.applyMargins(margins) 216 | } 217 | 218 | private fun recalculateTooltip() { 219 | val currentTooltip = tooltip ?: return 220 | removeView(currentTooltip.getView(context, this)) 221 | 222 | val v = currentTooltip.getView(context, this) 223 | val p = currentTooltip.getLayoutParams() 224 | 225 | when (calculateFitModeForTooltip(v, viewFrame, showcaseFrame)) { 226 | Fit.TOP -> { 227 | p.topMargin = (showcaseFrame.top - v.measuredHeight).toInt() 228 | } 229 | Fit.BOTTOM -> { 230 | p.topMargin = showcaseFrame.bottom.toInt() 231 | } 232 | else -> 233 | throw IllegalStateException("There is no space for tooltip") 234 | } 235 | 236 | addView(v, p) 237 | } 238 | } 239 | -------------------------------------------------------------------------------- /app/src/main/java/github/com/st235/samuraiview/components/CircularImageView.kt: -------------------------------------------------------------------------------- 1 | package github.com.st235.samuraiview.components 2 | 3 | import android.annotation.TargetApi 4 | import android.content.Context 5 | import android.graphics.* 6 | import android.graphics.drawable.BitmapDrawable 7 | import android.graphics.drawable.Drawable 8 | import android.os.Build 9 | import android.util.AttributeSet 10 | import android.util.Log 11 | import android.view.View 12 | import androidx.annotation.* 13 | import github.com.st235.samuraiview.R 14 | 15 | /** 16 | * Displays image resources, for example [Drawable] resources 17 | * with a circular mask. 18 | */ 19 | class CircularImageView : View { 20 | 21 | private val paint = Paint(Paint.ANTI_ALIAS_FLAG) 22 | private val center = PointF() 23 | 24 | private var extraText = DEFAULT_PLACEHOLDER 25 | 26 | @DrawableRes 27 | private var drawableId = -1 28 | 29 | private var targetImage: Bitmap? = null 30 | 31 | @ColorInt 32 | private var textColor = Color.BLACK 33 | 34 | @Px 35 | private var textSize = 0 36 | 37 | @FloatRange(from = 0.0) 38 | private var radius: Float = 0.toFloat() 39 | 40 | /** 41 | * Creates new one from xml with style from theme attribute 42 | * @param attrs an xml attributes set 43 | * @param defStyleAttr a style from theme 44 | */ 45 | @JvmOverloads 46 | constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : super( 47 | context, 48 | attrs, 49 | defStyleAttr 50 | ) { 51 | 52 | init(context, attrs) 53 | } 54 | 55 | /** 56 | * Creates new one from xml with a style from theme attribute or style resource 57 | * Api 21 and above 58 | * @param attrs an xml attributes set 59 | * @param defStyleAttr a style from theme 60 | * @param defStyleRes a style resource 61 | */ 62 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) 63 | constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super( 64 | context, 65 | attrs, 66 | defStyleAttr, 67 | defStyleRes 68 | ) { 69 | 70 | init(context, attrs) 71 | } 72 | 73 | /** 74 | * Initialize current [CircularImageView] with attributes from xml 75 | */ 76 | private fun init(context: Context, attrs: AttributeSet?) { 77 | val ta = context.obtainStyledAttributes(attrs, R.styleable.CircularImageView) 78 | 79 | val drawableId = ta.getResourceId(R.styleable.CircularImageView_cl_foreground, -1) 80 | if (drawableId != -1) { 81 | loadDrawable(drawableId) 82 | } 83 | 84 | textColor = ta.getColor(R.styleable.CircularImageView_cl_text_color, Color.BLACK) 85 | textSize = ta.getDimensionPixelSize(R.styleable.CircularImageView_cl_text_size, 0) 86 | 87 | val t = ta.getString(R.styleable.CircularImageView_cl_text) 88 | extraText = t ?: extraText 89 | 90 | ta.recycle() 91 | } 92 | 93 | /** 94 | * Set extra text 95 | * @param extraText is a text which will be displayed at center of image view if exists 96 | */ 97 | fun setExtraText(extraText: String) { 98 | this.extraText = extraText 99 | invalidate() 100 | } 101 | 102 | /** 103 | * Set current image drawable resource 104 | * @param drawableId is identifier of drawable which will be displayed at image view 105 | */ 106 | fun setDrawableResource(@DrawableRes drawableId: Int) { 107 | loadDrawable(drawableId) 108 | invalidate() 109 | } 110 | 111 | /** 112 | * {@inheritDoc} 113 | */ 114 | override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { 115 | val fontMetrics = paint.fontMetrics 116 | 117 | val textWidth = paint.measureText(extraText) 118 | val textHeight = -fontMetrics.top + fontMetrics.bottom 119 | 120 | val desiredWidth = Math.round(textWidth + paddingLeft.toFloat() + paddingRight.toFloat()) 121 | val desiredHeight = Math.round(textHeight * 2f + paddingTop.toFloat() + paddingBottom.toFloat()) 122 | 123 | val measuredWidth = reconcileSize(desiredWidth, widthMeasureSpec) 124 | val measuredHeight = reconcileSize(desiredHeight, heightMeasureSpec) 125 | 126 | setMeasuredDimension(measuredWidth, measuredHeight) 127 | } 128 | 129 | /** 130 | * Reconcile a desired size for the view contents with a [MeasureSpec] 131 | * constraint passed by the parent. 132 | * 133 | * This is a simplified version of [View.resolveSize] 134 | * 135 | * @param contentSize Size of the view's contents. 136 | * @param measureSpec A [MeasureSpec] passed by the parent. 137 | * @return A size that best fits `contentSize` while respecting the parent's constraints. 138 | */ 139 | private fun reconcileSize(contentSize: Int, measureSpec: Int): Int { 140 | val mode = View.MeasureSpec.getMode(measureSpec) 141 | val specSize = View.MeasureSpec.getSize(measureSpec) 142 | when (mode) { 143 | View.MeasureSpec.EXACTLY -> return specSize 144 | View.MeasureSpec.AT_MOST -> { 145 | return if (contentSize < specSize) { 146 | contentSize 147 | } else specSize 148 | } 149 | View.MeasureSpec.UNSPECIFIED -> return contentSize 150 | else -> return contentSize 151 | } 152 | } 153 | 154 | /** 155 | * {@inheritDoc} 156 | */ 157 | override fun onDraw(canvas: Canvas) { 158 | paint.style = Paint.Style.FILL 159 | 160 | radius = Math.min( 161 | measuredWidth / 2.0f - paddingLeft.toFloat() - paddingRight.toFloat(), 162 | measuredHeight / 2.0f - paddingTop.toFloat() - paddingBottom.toFloat() 163 | ) 164 | center.set(measuredWidth / 2.0f, measuredHeight / 2.0f) 165 | 166 | if (targetImage == null && drawableId != -1) { 167 | loadDrawable(drawableId) 168 | } 169 | 170 | updateShader() 171 | canvas.drawCircle(center.x, center.y, radius, paint) 172 | 173 | paint.shader = null 174 | paint.color = textColor 175 | paint.textSize = textSize.toFloat() 176 | 177 | val textWidth = paint.measureText(extraText) 178 | val textY = (center.y - (paint.descent() + paint.ascent()) / 2).toInt() 179 | canvas.drawText(extraText, center.x - textWidth / 2, textY.toFloat(), paint) 180 | } 181 | 182 | /** 183 | * Loads targetImage to be shown into memory 184 | * @param drawableId which will be loaded as target 185 | */ 186 | private fun loadDrawable(@DrawableRes drawableId: Int) { 187 | this.drawableId = drawableId 188 | val drawable = resources.getDrawable(drawableId) 189 | targetImage = drawableToBitmap(drawable) 190 | targetImage = cropBitmap(targetImage) 191 | } 192 | 193 | /** 194 | * Updates paint shader with custom targetImage target 195 | */ 196 | private fun updateShader() { 197 | if (targetImage == null) { 198 | return 199 | } 200 | 201 | val shader = BitmapShader(targetImage!!, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP) 202 | 203 | val matrix = Matrix() 204 | matrix.setScale( 205 | measuredWidth.toFloat() / targetImage!!.width.toFloat(), 206 | measuredHeight.toFloat() / targetImage!!.height.toFloat() 207 | ) 208 | shader.setLocalMatrix(matrix) 209 | paint.shader = shader 210 | } 211 | 212 | /** 213 | * Creates center cropped targetImage from origin 214 | * @param bitmap which need to be cropped 215 | * @return targetImage instance 216 | */ 217 | @CheckResult 218 | private fun cropBitmap(bitmap: Bitmap?): Bitmap? { 219 | if (bitmap == null) { 220 | return null 221 | } 222 | 223 | return if (bitmap.width >= bitmap.height) { 224 | Bitmap.createBitmap( 225 | bitmap, 226 | bitmap.width / 2 - bitmap.height / 2, 227 | 0, 228 | bitmap.height, bitmap.height 229 | ) 230 | } else Bitmap.createBitmap( 231 | bitmap, 232 | 0, 233 | bitmap.height / 2 - bitmap.width / 2, 234 | bitmap.width, bitmap.width 235 | ) 236 | 237 | } 238 | 239 | /** 240 | * Converts drawable to targetImage. 241 | * If the drawable has no intrinsic 242 | * width or height the laid out sizes will be set up as current viewport. 243 | * @param drawable which need to be shown 244 | * @return targetImage instance of drawable 245 | */ 246 | @CheckResult 247 | private fun drawableToBitmap(drawable: Drawable?): Bitmap? { 248 | if (drawable == null) { 249 | return null 250 | } 251 | 252 | if (drawable is BitmapDrawable) { 253 | return drawable.bitmap 254 | } 255 | 256 | var intrinsicWidth = drawable.intrinsicWidth 257 | var intrinsicHeight = drawable.intrinsicHeight 258 | 259 | if (intrinsicWidth == -1 || intrinsicHeight == -1) { 260 | intrinsicWidth = measuredWidth 261 | intrinsicHeight = measuredHeight 262 | } 263 | 264 | if (intrinsicWidth <= 0 || intrinsicHeight <= 0) { 265 | return null 266 | } 267 | 268 | try { 269 | val bitmap = Bitmap.createBitmap(intrinsicWidth, intrinsicHeight, Bitmap.Config.ARGB_8888) 270 | val canvas = Canvas(bitmap) 271 | drawable.setBounds(0, 0, canvas.width, canvas.height) 272 | drawable.draw(canvas) 273 | return bitmap 274 | } catch (e: OutOfMemoryError) { 275 | Log.e(TAG, "OutOfMemory while creating targetImage!") 276 | return null 277 | } 278 | 279 | } 280 | 281 | companion object { 282 | private val TAG = "CircularImageView" 283 | private val DEFAULT_PLACEHOLDER = "Ex" 284 | } 285 | } 286 | /** 287 | * Creates new one from code 288 | */ 289 | /** 290 | * Creates new one from xml 291 | * @param attrs an xml attributes set 292 | */ 293 | --------------------------------------------------------------------------------