-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
393 additions
and
274 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
kotlinknifer/src/main/java/com/devrapid/kotlinknifer/Animation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package com.devrapid.kotlinknifer | ||
|
||
import android.animation.Animator | ||
import android.animation.AnimatorListenerAdapter | ||
import android.animation.ArgbEvaluator | ||
import android.animation.ValueAnimator | ||
import android.os.Build | ||
import android.os.Parcel | ||
import android.os.Parcelable | ||
import android.view.View | ||
import android.view.ViewAnimationUtils | ||
import androidx.interpolator.view.animation.FastOutSlowInInterpolator | ||
import kotlin.math.pow | ||
|
||
fun View.registerCircularRevealAnimation( | ||
revealSettings: RevealAnimationSetting, | ||
startColor: Int, | ||
endColor: Int | ||
) { | ||
val duration = 1000L | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | ||
addOnLayoutChangeListener(object : View.OnLayoutChangeListener { | ||
override fun onLayoutChange( | ||
v: View, | ||
left: Int, | ||
top: Int, | ||
right: Int, | ||
bottom: Int, | ||
oldLeft: Int, | ||
oldTop: Int, | ||
oldRight: Int, | ||
oldBottom: Int | ||
) { | ||
v.removeOnLayoutChangeListener(this) | ||
val (cx, cy, width, height) = revealSettings | ||
|
||
// Simply use the diagonal of the view. | ||
val radius = Math.sqrt((width.toDouble().pow(2) + height.toDouble().pow(2))).toFloat() | ||
ViewAnimationUtils.createCircularReveal(v, cx, cy, 0f, radius).apply { | ||
this.duration = duration | ||
interpolator = FastOutSlowInInterpolator() | ||
}.start() | ||
} | ||
}) | ||
startColorAnimation(startColor, endColor, duration) | ||
} | ||
} | ||
|
||
fun View.startCircularExitAnimation( | ||
revealSettings: RevealAnimationSetting, | ||
startColor: Int, | ||
endColor: Int, | ||
listener: () -> Unit | ||
) { | ||
val duration = 1000L | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | ||
val (cx, cy, width, height) = revealSettings | ||
|
||
// Simply use the diagonal of the view. | ||
val radius = Math.sqrt((width.toDouble().pow(2) + height.toDouble().pow(2))).toFloat() | ||
ViewAnimationUtils.createCircularReveal(this, cx, cy, radius, 0f).apply { | ||
this.duration = duration | ||
interpolator = FastOutSlowInInterpolator() | ||
addListener(object : AnimatorListenerAdapter() { | ||
override fun onAnimationEnd(animation: Animator) { | ||
gone() | ||
listener() | ||
} | ||
}) | ||
}.start() | ||
startColorAnimation(startColor, endColor, duration) | ||
} | ||
else { | ||
listener() | ||
} | ||
} | ||
|
||
fun View.startColorAnimation( | ||
startColor: Int, | ||
endColor: Int, | ||
duration: Long | ||
) { | ||
ValueAnimator().apply { | ||
this.duration = duration | ||
setIntValues(startColor, endColor) | ||
setEvaluator(ArgbEvaluator()) | ||
addUpdateListener { setBackgroundColor(it.animatedValue as Int) } | ||
}.start() | ||
} | ||
|
||
data class RevealAnimationSetting( | ||
var centerX: Int, | ||
var centerY: Int, | ||
var width: Int, | ||
var height: Int | ||
) : Parcelable { | ||
companion object CREATOR : Parcelable.Creator<RevealAnimationSetting> { | ||
override fun createFromParcel(parcel: Parcel) = RevealAnimationSetting(parcel) | ||
|
||
override fun newArray(size: Int) = arrayOfNulls<RevealAnimationSetting>(size) | ||
} | ||
|
||
constructor(parcel: Parcel) : this(parcel.readInt(), parcel.readInt(), parcel.readInt(), parcel.readInt()) | ||
|
||
override fun writeToParcel(parcel: Parcel, flags: Int) { | ||
parcel.writeInt(centerX) | ||
parcel.writeInt(centerY) | ||
parcel.writeInt(width) | ||
parcel.writeInt(height) | ||
} | ||
|
||
override fun describeContents(): Int = 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
kotlinknifer/src/main/java/com/devrapid/kotlinknifer/Bundle.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.devrapid.kotlinknifer | ||
|
||
import android.app.Activity | ||
import androidx.fragment.app.Fragment | ||
|
||
inline fun <reified T : Any> Activity.extra(key: String, default: T? = null) = lazy { | ||
val value = intent?.extras?.get(key) | ||
if (value is T) value else default | ||
} | ||
|
||
inline fun <reified T : Any> Activity.extraNotNull(key: String, default: T? = null) = lazy { | ||
val value = intent?.extras?.get(key) | ||
requireNotNull(if (value is T) value else default) { key } | ||
} | ||
|
||
inline fun <reified T : Any> Fragment.extra(key: String, default: T? = null) = lazy { | ||
val value = arguments?.get(key) | ||
if (value is T) value else default | ||
} | ||
|
||
inline fun <reified T : Any> Fragment.extraNotNull(key: String, default: T? = null) = lazy { | ||
val value = arguments?.get(key) | ||
requireNotNull(if (value is T) value else default) { key } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
kotlinknifer/src/main/java/com/devrapid/kotlinknifer/Context.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.devrapid.kotlinknifer | ||
|
||
import android.app.ActivityManager | ||
import android.content.Context | ||
import androidx.appcompat.app.AppCompatActivity | ||
|
||
fun Context.closeCurrentTask(activity: AppCompatActivity) = | ||
(getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager).appTasks | ||
.find { it.taskInfo.id == activity.taskId } | ||
?.finishAndRemoveTask() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.