Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate the Target and realize the use of the square view on the basis of the circle #404

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
-Replace part of the code with Kotlin
  • Loading branch information
huaweikai committed Aug 5, 2023
commit 0021f8b17f3b8846e95bb05486a55109ede09fdd
5 changes: 5 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
compileSdkVersion defCompileSdkVersion
Expand All @@ -16,6 +17,10 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:name=".SampleApplication">
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
Expand Down

This file was deleted.

145 changes: 145 additions & 0 deletions app/src/main/java/com/getkeepsafe/taptargetviewsample/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package com.getkeepsafe.taptargetviewsample

import android.content.DialogInterface
import android.graphics.Rect
import android.graphics.Typeface
import android.os.Bundle
import android.text.SpannableString
import android.text.style.StyleSpan
import android.text.style.UnderlineSpan
import android.util.Log
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat
import com.getkeepsafe.taptargetview.TapTarget
import com.getkeepsafe.taptargetview.TapTargetSequence
import com.getkeepsafe.taptargetview.TapTargetView
import com.getkeepsafe.taptargetview.showGuideView

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar = findViewById<View>(R.id.toolbar) as Toolbar
toolbar.inflateMenu(R.menu.menu_main)
toolbar.navigationIcon =
ContextCompat.getDrawable(this, R.drawable.ic_arrow_back_white_24dp)

// We load a drawable and create a location to show a tap target here
// We need the display to get the width and height at this point in time
val display = windowManager.defaultDisplay
// Load our little droid guy
val droid = ContextCompat.getDrawable(this, R.drawable.ic_android_black_24dp)
// Tell our droid buddy where we want him to appear
val droidTarget = Rect(0, 0, droid!!.intrinsicWidth * 2, droid.intrinsicHeight * 2)
// Using deprecated methods makes you look way cool
droidTarget.offset(display.width / 2, display.height / 2)
val sassyDesc = SpannableString("It allows you to go back, sometimes")
sassyDesc.setSpan(
StyleSpan(Typeface.ITALIC),
sassyDesc.length - "sometimes".length,
sassyDesc.length,
0
)

// We have a sequence of targets, so lets build it!
val sequence = TapTargetSequence(this)
.addTarget( // This tap target will target the back button, we just need to pass its containing toolbar
TapTarget.forToolbarNavigationIcon(toolbar, "This is the back button", sassyDesc)
.id(1), // Likewise, this tap target will target the search button
TapTarget.forToolbarMenuItem(
toolbar,
R.id.search,
"This is a search icon",
"As you can see, it has gotten pretty dark around here..."
)
.dimColor(android.R.color.black)
.outerCircleColor(R.color.colorAccent)
.targetCircleColor(android.R.color.black)
.transparentTarget(true)
.textColor(android.R.color.black)
.id(2), // You can also target the overflow button in your toolbar
TapTarget.forToolbarOverflow(
toolbar,
"This will show more options",
"But they're not useful :("
).id(3), // This tap target will target our droid buddy at the given target rect
TapTarget.forBounds(
droidTarget,
"Oh look!",
"You can point to any part of the screen. You also can't cancel this one!"
)
.cancelable(false)
.icon(droid)
.id(4)
)
.listener(object : TapTargetSequence.Listener {
// This listener will tell us when interesting(tm) events happen in regards
// to the sequence
override fun onSequenceFinish() {
(findViewById<View>(R.id.educated) as TextView).text =
"Congratulations! You're educated now!"
}

override fun onSequenceStep(lastTarget: TapTarget?, targetClicked: Boolean) {
Log.d("TapTargetView", "Clicked on " + lastTarget!!.id())
}

override fun onSequenceCanceled(lastTarget: TapTarget?) {
val dialog = AlertDialog.Builder(this@MainActivity)
.setTitle("Uh oh")
.setMessage("You canceled the sequence")
.setPositiveButton("Oops", null).show()
dialog.showGuideView(TapTarget.forView(
dialog.getButton(DialogInterface.BUTTON_POSITIVE),
"Uh oh!",
"You canceled the sequence at step " + lastTarget?.id()
)
.cancelable(false)
.tintTarget(false), object : TapTargetView.Listener() {
override fun onTargetClick(view: TapTargetView) {
super.onTargetClick(view)
dialog.dismiss()
}
})
}
})

// You don't always need a sequence, and for that there's a single time tap target
val spannedDesc = SpannableString("This is the sample app for TapTargetView")
spannedDesc.setSpan(
UnderlineSpan(),
spannedDesc.length - "TapTargetView".length,
spannedDesc.length,
0
)
this.showGuideView(TapTarget.forView(findViewById(R.id.fab), "Hello, world!", spannedDesc)
.cancelable(false)
.drawShadow(true)
.titleTextDimen(R.dimen.title_text_size)
.tintTarget(false), object : TapTargetView.Listener() {
override fun onTargetClick(view: TapTargetView) {
super.onTargetClick(view)
// .. which evidently starts the sequence we defined earlier
sequence.start()
}

override fun onOuterCircleClick(view: TapTargetView) {
super.onOuterCircleClick(view)
Toast.makeText(
view.context,
"You clicked the outer circle!",
Toast.LENGTH_SHORT
).show()
}

override fun onTargetDismissed(view: TapTargetView, userInitiated: Boolean) {
Log.d("TapTargetViewSample", "You dismissed me :(")
}
})
}
}

This file was deleted.

3 changes: 2 additions & 1 deletion taptargetview/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'com.vanniktech.maven.publish'
id 'com.android.library'
id 'kotlin-android'
}

android {
Expand All @@ -21,7 +22,7 @@ dependencies {
api("androidx.appcompat:appcompat:$defAppCompatVersion") {
exclude group: 'androidx.core', module: 'core-ktx'
}
implementation ("androidx.core:core:$defAndroidXCoreVersion") {
implementation ("androidx.core:core-ktx:$defAndroidXCoreVersion") {
exclude group: 'androidx.core', module: 'core-ktx'
}
api("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.22") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,7 @@ public FloatValueAnimatorBuilder repeat(int times) {
}

public FloatValueAnimatorBuilder onUpdate(final UpdateListener listener) {
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
listener.onUpdate((float) animation.getAnimatedValue());
}
});
animator.addUpdateListener(animation -> listener.onUpdate((float) animation.getAnimatedValue()));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@file:JvmName("ReflectExtensions")
package com.getkeepsafe.taptargetview

import kotlin.jvm.Throws

@Throws(NoSuchFieldException::class, IllegalAccessException::class)
fun Any?.getPrivateField(fieldName: String?): Any? {
if (fieldName == null || this == null) return null
val field = this.javaClass.getDeclaredField(fieldName)
field.isAccessible = true
return field.get(this)
}

This file was deleted.

Loading