From 3ec80f986e8ba9572d833311381639a70c1eb43f Mon Sep 17 00:00:00 2001 From: Anoop Date: Tue, 30 Jul 2019 21:03:37 +0530 Subject: [PATCH 1/3] Added layerType for doing animations in HW/SW layer --- .../rxanimation/RxAnimationExtension.kt | 264 +++++++++++------- 1 file changed, 162 insertions(+), 102 deletions(-) diff --git a/rxanimation/src/main/java/com/mikhaellopez/rxanimation/RxAnimationExtension.kt b/rxanimation/src/main/java/com/mikhaellopez/rxanimation/RxAnimationExtension.kt index 764906e..203cdb7 100644 --- a/rxanimation/src/main/java/com/mikhaellopez/rxanimation/RxAnimationExtension.kt +++ b/rxanimation/src/main/java/com/mikhaellopez/rxanimation/RxAnimationExtension.kt @@ -47,24 +47,33 @@ fun View.animate(alpha: Float? = null, z: Float? = null, duration: Long? = null, interpolator: TimeInterpolator? = null, - startDelay: Long? = null): Completable = + startDelay: Long? = null, + layerType: Int? = null): Completable = Completable.create { animate().apply { - alpha?.also { alpha(it) } - translationX?.also { translationX(it.dpToPx()) } - translationY?.also { translationY(it.dpToPx()) } - scaleX?.also { scaleX(it) } - scaleY?.also { scaleY(it) } - rotation?.also { rotation(it) } - rotationX?.also { rotationX(it) } - rotationY?.also { rotationY(it) } - x?.also { x(it) } - y?.also { x(it) } - z?.also { x(it) } - duration?.also { this.duration = it } - interpolator?.also { this.interpolator = it } - startDelay?.also { this.startDelay = it } - }.animate { it.onComplete() } + alpha?.also { alpha(it) } + translationX?.also { translationX(it.dpToPx()) } + translationY?.also { translationY(it.dpToPx()) } + scaleX?.also { scaleX(it) } + scaleY?.also { scaleY(it) } + rotation?.also { rotation(it) } + rotationX?.also { rotationX(it) } + rotationY?.also { rotationY(it) } + x?.also { x(it) } + y?.also { x(it) } + z?.also { x(it) } + duration?.also { this.duration = it } + interpolator?.also { this.interpolator = it } + startDelay?.also { this.startDelay = it } + layerType?.also { + tag = getLayerType() + setLayerType(it, null) + withLayer() + } + }.animate { + (tag as? Int)?.let {setLayerType(it, null)} + it.onComplete() + } } //endregion @@ -154,15 +163,17 @@ fun View.alpha(alpha: Float, duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Completable = + reverse: Boolean = false, + layerType: Int? = null): Completable = Observable.just(this.alpha) .flatMapCompletable { defaultAlpha -> animate(alpha = alpha, duration = duration, interpolator = interpolator, - startDelay = startDelay) + startDelay = startDelay, + layerType = layerType) .reverse(reverse) { - alpha(defaultAlpha, duration, interpolator) + alpha(defaultAlpha, duration, interpolator, layerType = layerType) } } @@ -170,32 +181,37 @@ fun Observable.alpha(alpha: Float, duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Observable = - doCompletable { it.alpha(alpha, duration, interpolator, startDelay, reverse) } + reverse: Boolean = false, + layerType: Int? = null): Observable = + doCompletable { it.alpha(alpha, duration, interpolator, startDelay, reverse, layerType) } fun View.fadeIn(duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Completable = - alpha(1f, duration, interpolator, startDelay, reverse) + reverse: Boolean = false, + layerType: Int?= null): Completable = + alpha(1f, duration, interpolator, startDelay, reverse, layerType) fun Observable.fadeIn(duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Observable = - doCompletable { it.fadeIn(duration, interpolator, startDelay, reverse) } + reverse: Boolean = false, + layerType: Int? = null): Observable = + doCompletable { it.fadeIn(duration, interpolator, startDelay, reverse, layerType) } fun View.fadeOut(duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Completable = - alpha(0f, duration, interpolator, startDelay, reverse) + reverse: Boolean = false, + layerType: Int? = null): Completable = + alpha(0f, duration, interpolator, startDelay, reverse, layerType) fun Observable.fadeOut(duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Observable = - doCompletable { it.fadeOut(duration, interpolator, startDelay, reverse) } + reverse: Boolean = false, + layerType: Int? = null): Observable = + doCompletable { it.fadeOut(duration, interpolator, startDelay, reverse, layerType) } //endregion //region TRANSLATION @@ -204,16 +220,18 @@ fun View.translation(translationX: Float, duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Completable = + reverse: Boolean = false, + layerType: Int? = null): Completable = Observable.just(this.translationX to this.translationY) .flatMapCompletable { (defaultTranslationX, defaultTranslationY) -> animate(translationX = translationX, translationY = translationY, duration = duration, interpolator = interpolator, - startDelay = startDelay) + startDelay = startDelay, + layerType = layerType) .reverse(reverse) { - translation(defaultTranslationX, defaultTranslationY, duration, interpolator) + translation(defaultTranslationX, defaultTranslationY, duration, interpolator, layerType = layerType) } } @@ -222,20 +240,23 @@ fun Observable.translation(translationX: Float, duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Observable = - doCompletable { it.translation(translationX, translationY, duration, interpolator, startDelay, reverse) } + reverse: Boolean = false, + layerType: Int? = null): Observable = + doCompletable { it.translation(translationX, translationY, duration, interpolator, startDelay, reverse, layerType) } fun View.translationX(translationX: Float, duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Completable = + reverse: Boolean = false, + layerType: Int? = null): Completable = Observable.just(this.translationX) .flatMapCompletable { defaultTranslationX -> animate(translationX = translationX, duration = duration, interpolator = interpolator, - startDelay = startDelay) + startDelay = startDelay, + layerType = layerType) .reverse(reverse) { translationX(defaultTranslationX, duration, interpolator) } @@ -245,22 +266,25 @@ fun Observable.translationX(translationX: Float, duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Observable = - doCompletable { it.translationX(translationX, duration, interpolator, startDelay, reverse) } + reverse: Boolean = false, + layerType: Int? = null): Observable = + doCompletable { it.translationX(translationX, duration, interpolator, startDelay, reverse, layerType) } fun View.translationY(translationY: Float, duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Completable = + reverse: Boolean = false, + layerType: Int? = null): Completable = Observable.just(this.translationY) .flatMapCompletable { defaultTranslationY -> animate(translationY = translationY, duration = duration, interpolator = interpolator, - startDelay = startDelay) + startDelay = startDelay, + layerType = layerType) .reverse(reverse) { - translationY(defaultTranslationY, duration, interpolator) + translationY(defaultTranslationY, duration, interpolator, layerType = layerType) } } @@ -268,8 +292,9 @@ fun Observable.translationY(translationY: Float, duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Observable = - doCompletable { it.translationY(translationY, duration, interpolator, startDelay, reverse) } + reverse: Boolean = false, + layerType: Int? = null): Observable = + doCompletable { it.translationY(translationY, duration, interpolator, startDelay, reverse, layerType) } //endregion //region SCALE @@ -277,19 +302,22 @@ fun View.scale(scale: Float, duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Completable = + reverse: Boolean = false, + layerType: Int? = null): Completable = Observable.just(this.scaleX to this.scaleY) .flatMapCompletable { (defaultScaleX, defaultScaleY) -> animate(scaleX = scale, scaleY = scale, duration = duration, interpolator = interpolator, - startDelay = startDelay) + startDelay = startDelay, + layerType = layerType) .reverse(reverse) { animate(scaleX = defaultScaleX, scaleY = defaultScaleY, duration = duration, - interpolator = interpolator) + interpolator = interpolator, + layerType = layerType) } } @@ -297,22 +325,25 @@ fun Observable.scale(scale: Float, duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Observable = - doCompletable { it.scale(scale, duration, interpolator, startDelay, reverse) } + reverse: Boolean = false, + layerType: Int? = null): Observable = + doCompletable { it.scale(scale, duration, interpolator, startDelay, reverse, layerType) } fun View.scaleX(scaleX: Float, duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Completable = + reverse: Boolean = false, + layerType: Int? = null): Completable = Observable.just(this.scaleX) .flatMapCompletable { defaultScaleX -> animate(scaleX = scaleX, duration = duration, interpolator = interpolator, - startDelay = startDelay) + startDelay = startDelay, + layerType = layerType) .reverse(reverse) { - scaleX(defaultScaleX, duration, interpolator) + scaleX(defaultScaleX, duration, interpolator, layerType = layerType) } } @@ -320,22 +351,25 @@ fun Observable.scaleX(scaleX: Float, duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Observable = - doCompletable { it.scaleX(scaleX, duration, interpolator, startDelay, reverse) } + reverse: Boolean = false, + layerType: Int? = null): Observable = + doCompletable { it.scaleX(scaleX, duration, interpolator, startDelay, reverse, layerType) } fun View.scaleY(scaleY: Float, duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Completable = + reverse: Boolean = false, + layerType: Int? = null): Completable = Observable.just(this.scaleY) .flatMapCompletable { defaultScaleY -> animate(scaleY = scaleY, duration = duration, interpolator = interpolator, - startDelay = startDelay) + startDelay = startDelay, + layerType = layerType) .reverse(reverse) { - scaleY(defaultScaleY, duration, interpolator) + scaleY(defaultScaleY, duration, interpolator, layerType = layerType) } } @@ -343,8 +377,9 @@ fun Observable.scaleY(scaleY: Float, duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Observable = - doCompletable { it.scaleY(scaleY, duration, interpolator, startDelay, reverse) } + reverse: Boolean = false, + layerType: Int? = null): Observable = + doCompletable { it.scaleY(scaleY, duration, interpolator, startDelay, reverse, layerType) } //endregion //region ROTATION @@ -352,15 +387,17 @@ fun View.rotation(rotation: Float, duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Completable = + reverse: Boolean = false, + layerType: Int? = null): Completable = Observable.just(this.rotation) .flatMapCompletable { defaultRotation -> animate(rotation = rotation, duration = duration, interpolator = interpolator, - startDelay = startDelay) + startDelay = startDelay, + layerType = layerType) .reverse(reverse) { - rotation(defaultRotation, duration, interpolator) + rotation(defaultRotation, duration, interpolator, layerType = layerType) } } @@ -368,22 +405,25 @@ fun Observable.rotation(rotation: Float, duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Observable = - doCompletable { it.rotation(rotation, duration, interpolator, startDelay, reverse) } + reverse: Boolean = false, + layerType: Int? = null): Observable = + doCompletable { it.rotation(rotation, duration, interpolator, startDelay, reverse, layerType) } fun View.rotationX(rotationX: Float, duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Completable = + reverse: Boolean = false, + layerType: Int? = null): Completable = Observable.just(this.rotationX) .flatMapCompletable { defaultRotationX -> animate(rotationX = rotationX, duration = duration, interpolator = interpolator, - startDelay = startDelay) + startDelay = startDelay, + layerType = layerType) .reverse(reverse) { - rotationX(defaultRotationX, duration, interpolator) + rotationX(defaultRotationX, duration, interpolator, layerType = layerType) } } @@ -391,20 +431,23 @@ fun Observable.rotationX(rotationX: Float, duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Observable = - doCompletable { it.rotationX(rotationX, duration, interpolator, startDelay, reverse) } + reverse: Boolean = false, + layerType: Int? = null): Observable = + doCompletable { it.rotationX(rotationX, duration, interpolator, startDelay, reverse, layerType) } fun View.rotationY(rotationY: Float, duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Completable = + reverse: Boolean = false, + layerType: Int? = null): Completable = Observable.just(this.rotationY) .flatMapCompletable { defaultRotationY -> animate(rotationY = rotationY, duration = duration, interpolator = interpolator, - startDelay = startDelay) + startDelay = startDelay, + layerType = layerType) .reverse(reverse) { rotationY(defaultRotationY, duration, interpolator) } @@ -414,8 +457,9 @@ fun Observable.rotationY(rotationY: Float, duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Observable = - doCompletable { it.rotationY(rotationY, duration, interpolator, startDelay, reverse) } + reverse: Boolean = false, + layerType: Int? = null): Observable = + doCompletable { it.rotationY(rotationY, duration, interpolator, startDelay, reverse, layerType) } //endregion //region X, Y, Z @@ -426,7 +470,8 @@ fun View.xyz(x: Float? = null, duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Completable = + reverse: Boolean = false, + layerType: Int? = null): Completable = Observable.just(Triple(this.x, this.y, this.z)) .flatMapCompletable { (defaultX, defaultY, defaultZ) -> animate(x = x, @@ -434,9 +479,10 @@ fun View.xyz(x: Float? = null, z = z, duration = duration, interpolator = interpolator, - startDelay = startDelay) + startDelay = startDelay, + layerType = layerType) .reverse(reverse) { - xyz(defaultX, defaultY, defaultZ, duration, interpolator) + xyz(defaultX, defaultY, defaultZ, duration, interpolator, layerType = layerType) } } @@ -447,22 +493,25 @@ fun Observable.xyz(x: Float? = null, duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Observable = - doCompletable { it.xyz(x, y, z, duration, interpolator, startDelay, reverse) } + reverse: Boolean = false, + layerType: Int? = null): Observable = + doCompletable { it.xyz(x, y, z, duration, interpolator, startDelay, reverse, layerType) } fun View.x(x: Float, duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Completable = + reverse: Boolean = false, + layerType: Int? = null): Completable = Observable.just(this.x) .flatMapCompletable { defaultX -> animate(x = x, duration = duration, interpolator = interpolator, - startDelay = startDelay) + startDelay = startDelay, + layerType = layerType) .reverse(reverse) { - x(defaultX, duration, interpolator) + x(defaultX, duration, interpolator, layerType = layerType) } } @@ -470,22 +519,25 @@ fun Observable.x(x: Float, duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Observable = - doCompletable { it.x(x, duration, interpolator, startDelay, reverse) } + reverse: Boolean = false, + layerType: Int? = null): Observable = + doCompletable { it.x(x, duration, interpolator, startDelay, reverse, layerType) } fun View.y(y: Float, duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Completable = + reverse: Boolean = false, + layerType: Int? = null): Completable = Observable.just(this.y) .flatMapCompletable { defaultY -> animate(y = y, duration = duration, interpolator = interpolator, - startDelay = startDelay) + startDelay = startDelay, + layerType = layerType) .reverse(reverse) { - y(defaultY, duration, interpolator) + y(defaultY, duration, interpolator, layerType = layerType) } } @@ -493,23 +545,26 @@ fun Observable.y(y: Float, duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Observable = - doCompletable { it.y(y, duration, interpolator, startDelay, reverse) } + reverse: Boolean = false, + layerType: Int? = null): Observable = + doCompletable { it.y(y, duration, interpolator, startDelay, reverse, layerType) } @TargetApi(Build.VERSION_CODES.LOLLIPOP) fun View.z(z: Float, duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Completable = + reverse: Boolean = false, + layerType: Int? = null): Completable = Observable.just(this.z) .flatMapCompletable { defaultZ -> animate(z = z, duration = duration, interpolator = interpolator, - startDelay = startDelay) + startDelay = startDelay, + layerType = layerType) .reverse(reverse) { - z(defaultZ, duration, interpolator) + z(defaultZ, duration, interpolator, layerType = layerType) } } @@ -518,8 +573,9 @@ fun Observable.z(z: Float, duration: Long? = null, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Observable = - doCompletable { it.z(z, duration, interpolator, startDelay, reverse) } + reverse: Boolean = false, + layerType: Int? = null): Observable = + doCompletable { it.z(z, duration, interpolator, startDelay, reverse, layerType) } //endregion //region RESIZE @@ -657,14 +713,16 @@ fun Observable.shake(duration: Long = 300, fun View.press(depth: Float = 0.95f, duration: Long? = null, interpolator: TimeInterpolator? = null, - startDelay: Long? = null): Completable = - scale(depth, (duration ?: 300) / 2, interpolator, startDelay, reverse = true) + startDelay: Long? = null, + layerType: Int? = null): Completable = + scale(depth, (duration ?: 300) / 2, interpolator, startDelay, reverse = true, layerType = layerType) fun Observable.press(depth: Float = 0.95f, duration: Long? = null, interpolator: TimeInterpolator? = null, - startDelay: Long? = null): Observable = - doCompletable { it.press(depth, duration, interpolator, startDelay) } + startDelay: Long? = null, + layerType: Int? = null): Observable = + doCompletable { it.press(depth, duration, interpolator, startDelay, layerType) } //endregion //region TEXT @@ -672,14 +730,15 @@ fun TextView.text(text: String, duration: Long = 300L, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Completable = + reverse: Boolean = false, + layerType: Int? = null): Completable = Observable.just(this.text.toString()) .flatMapCompletable { defaultText -> - fadeOut(duration / 2, interpolator, startDelay) + fadeOut(duration / 2, interpolator, startDelay, layerType = layerType) .doOnComplete { this.text = text } - .andThen(fadeIn(duration / 2, interpolator, startDelay = 300L)) + .andThen(fadeIn(duration / 2, interpolator, startDelay = 300L, layerType = layerType)) .reverse(reverse) { - text(defaultText, duration, interpolator) + text(defaultText, duration, interpolator, layerType = layerType) } } @@ -687,6 +746,7 @@ fun Observable.text(text: String, duration: Long = 300L, interpolator: TimeInterpolator? = null, startDelay: Long? = null, - reverse: Boolean = false): Observable = - flatMap { it.text(text, duration, interpolator, startDelay, reverse).toSingleDefault(it).toObservable() } + reverse: Boolean = false, + layerType: Int? = null): Observable = + flatMap { it.text(text, duration, interpolator, startDelay, reverse, layerType).toSingleDefault(it).toObservable() } //endregion \ No newline at end of file From 7c9fe037f33da5850eb7d9b8ff9a92b20593fc88 Mon Sep 17 00:00:00 2001 From: Anoop Date: Tue, 30 Jul 2019 21:10:53 +0530 Subject: [PATCH 2/3] Using no layerType as default value when setting back after animation --- .../java/com/mikhaellopez/rxanimation/RxAnimationExtension.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rxanimation/src/main/java/com/mikhaellopez/rxanimation/RxAnimationExtension.kt b/rxanimation/src/main/java/com/mikhaellopez/rxanimation/RxAnimationExtension.kt index 203cdb7..b0bfb6f 100644 --- a/rxanimation/src/main/java/com/mikhaellopez/rxanimation/RxAnimationExtension.kt +++ b/rxanimation/src/main/java/com/mikhaellopez/rxanimation/RxAnimationExtension.kt @@ -5,6 +5,7 @@ import android.annotation.TargetApi import android.content.res.Resources import android.os.Build import android.view.View +import android.view.View.LAYER_TYPE_NONE import android.view.ViewPropertyAnimator import android.view.animation.CycleInterpolator import android.view.animation.Interpolator @@ -71,7 +72,7 @@ fun View.animate(alpha: Float? = null, withLayer() } }.animate { - (tag as? Int)?.let {setLayerType(it, null)} + setLayerType((tag as? Int) ?: LAYER_TYPE_NONE, null) it.onComplete() } } From eb920421b495dabb31840e3ba5cc43ee4cf2d254 Mon Sep 17 00:00:00 2001 From: Anoop Date: Tue, 30 Jul 2019 21:11:18 +0530 Subject: [PATCH 3/3] Readme updated --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 571fe39..cda1f11 100644 --- a/README.md +++ b/README.md @@ -162,6 +162,8 @@ ALL PROPERTIES :information_source: All the functions have `duration: Long`, `interpolator: TimeInterpolator`, `startDelay: Long` and `reverse: Boolean` properties. +:information_source: Hardware/Software acceleration can be specified using the `layerType: Int` field. Accepted values are `View.LAYER_TYPE_SOFTWARE`, `View.LAYER_TYPE_HARDWARE`, `View.LAYER_TYPE_NONE`. Once the animation is finished the view will be set to the previous layer type value. + LICENCE -----