Skip to content

Replace ObjectAnimator with Transitions API #2

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.2.0'
classpath 'com.android.tools.build:gradle:3.4.0-rc03'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$versions.kotlin"
}
}
Expand Down
1 change: 1 addition & 0 deletions fluidresizelayout/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ android {

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$versions.kotlin"
implementation "androidx.constraintlayout:constraintlayout:2.0.0-alpha3"
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
6 changes: 4 additions & 2 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ dependencies {
implementation project(path: ':fluidresizelayout')

implementation "androidx.appcompat:appcompat:$versions.androidx"
implementation "androidx.constraintlayout:constraintlayout:2.0.0-alpha3"

implementation "org.jetbrains.kotlin:kotlin-stdlib:$versions.kotlin"

implementation "com.jakewharton.timber:timber:$versions.timber"
implementation('com.github.JakeWharton:kotterknife:e157638df1') {
exclude group: 'com.android.support'
}
implementation 'io.reactivex.rxjava2:rxjava:2.1.9'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'io.reactivex.rxjava2:rxjava:2.2.2'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
implementation "com.jakewharton.rxbinding2:rxbinding:$versions.rxBindings"
implementation "com.jakewharton.rxrelay2:rxrelay:2.0.0"
implementation 'io.reactivex.rxjava2:rxkotlin:2.2.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,40 @@
package me.saket.fluidresize.sample

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.app.Activity
import android.transition.*
import android.view.View
import androidx.interpolator.view.animation.FastOutSlowInInterpolator
import android.view.ViewGroup
import me.saket.fluidresize.ActivityViewHolder
import me.saket.fluidresize.KeyboardVisibilityChanged
import me.saket.fluidresize.KeyboardVisibilityDetector

object FluidContentResizer {

private var heightAnimator: ValueAnimator = ObjectAnimator()

fun listen(activity: Activity) {
val viewHolder = ActivityViewHolder.createFrom(activity)

KeyboardVisibilityDetector.listen(viewHolder) {
animateHeight(viewHolder, it)
}
viewHolder.onDetach {
heightAnimator.cancel()
viewHolder.contentView.clearAnimation()
}
viewHolder.contentView.apply {
post {
setHeight(viewHolder.resizableLayout.height)
}
}
}

private fun animateHeight(viewHolder: ActivityViewHolder, event: KeyboardVisibilityChanged) {
val contentView = viewHolder.contentView
contentView.setHeight(event.contentHeightBeforeResize)

heightAnimator.cancel()
val transition = ChangeBounds()
val sceneRoot = contentView.parent as ViewGroup
TransitionManager.beginDelayedTransition(sceneRoot, transition)

// Warning: animating height might not be very performant. Try turning on
// "Profile GPI rendering" in developer options and check if the bars stay
// under 16ms in your app. Using Transitions API would be more efficient, but
// for some reason it skips the first animation and I cannot figure out why.
heightAnimator = ObjectAnimator.ofInt(event.contentHeightBeforeResize, event.contentHeight).apply {
interpolator = FastOutSlowInInterpolator()
duration = 300
}
heightAnimator.addUpdateListener { contentView.setHeight(it.animatedValue as Int) }
heightAnimator.start()
contentView.setHeight(event.contentHeight)
}

private fun View.setHeight(height: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package me.saket.fluidresize.sample

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_fluid_resize.*
import me.saket.fluidresize.ActivityViewHolder
import me.saket.fluidresize.KeyboardVisibilityDetector
import me.saket.fluidresize.R

class FluidResizeActivity : AppCompatActivity() {
Expand All @@ -10,6 +13,20 @@ class FluidResizeActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_fluid_resize)

FluidContentResizer.listen(this)
val viewHolder = ActivityViewHolder.createFrom(this)

KeyboardVisibilityDetector.listen(viewHolder) {
event ->
if (event.contentHeight <= event.contentHeightBeforeResize) {
next_button.alpha = 0f
} else {
next_button.animate()
.alpha(1f)
.setStartDelay(150)
.start()
}
}

FluidContentResizer.listen(this)
}
}
50 changes: 38 additions & 12 deletions sample/src/main/res/layout/activity_fluid_resize.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Sup world?"
tools:ignore="HardcodedText" />
</FrameLayout>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_gravity="center"
android:text="Sup world?"
tools:ignore="HardcodedText,LabelFor,UnusedAttribute"
android:importantForAutofill="no"
android:inputType="text" />

<TextView
android:id="@+id/next_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:text="Next"
android:textAllCaps="true"
android:paddingTop="18dp"
android:paddingBottom="18dp"
android:textSize="20sp"
android:textStyle="bold"
android:textAlignment="center"
android:background="@android:color/holo_green_light"
android:textColor="@android:color/white"
/>

</androidx.constraintlayout.widget.ConstraintLayout>