Skip to content

Commit c8e5f41

Browse files
committed
first commit
0 parents  commit c8e5f41

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1634
-0
lines changed

.gitignore

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Mac Finder hidden files
2+
.DS_Store
3+
4+
# Eclipse
5+
.project
6+
.classpath
7+
.settings
8+
.checkstyle
9+
10+
# IntelliJ IDEA
11+
.idea
12+
*.iml
13+
*.ipr
14+
*.iws
15+
classes
16+
gen-external-apklibs
17+
18+
# Gradle
19+
.gradle
20+
build
21+
22+
# Maven
23+
target
24+
release.properties
25+
pom.xml.*
26+
27+
# built application files
28+
*.apk
29+
*.ap_
30+
31+
# files for the dex VM
32+
*.dex
33+
34+
# Java class files
35+
*.class
36+
37+
# Local configuration file (sdk path, etc)
38+
local.properties
39+
40+
*.prefs

.travis.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
sudo: false
2+
3+
language: android
4+
5+
android:
6+
components:
7+
- tools
8+
- platform-tools
9+
- build-tools-27.0.3
10+
- android-22
11+
- android-27
12+
- extra-google-m2repository
13+
- extra-android-m2repository
14+
- sys-img-armeabi-v7a-android-22
15+
- sys-img-armeabi-v7a-android-26
16+
licenses:
17+
- 'android-sdk-preview-license-.+'
18+
- 'android-sdk-license-.+'
19+
- 'google-gdk-license-.+'
20+
21+
# Emulator Management: Create, Start and Wait
22+
before_script:
23+
- echo yes | sdkmanager "platforms;android-27"
24+
- echo no | android create avd --force -n test -t android-22 --abi armeabi-v7a
25+
- emulator -avd test -no-audio -no-window &
26+
- android-wait-for-emulator
27+
- adb shell input keyevent 82 &
28+
29+
script:
30+
- ./gradlew build connectedCheck -x lint
31+
32+
jdk:
33+
- oraclejdk8
34+
35+
branches:
36+
only:
37+
- master
38+
39+
before_cache:
40+
- rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
41+
- rm -fr $HOME/.gradle/caches/*/plugin-resolution/
42+
cache:
43+
directories:
44+
- $HOME/.gradle/caches/
45+
- $HOME/.gradle/wrapper/
46+
- $HOME/.android/build-cache
47+
48+
sudo: false

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# AnchoredBehavior
2+
[![Travis branch](https://img.shields.io/travis/prolificinteractive/patrons/master.svg)](https://travis-ci.org/prolificinteractive/patrons) [![](https://jitpack.io/v/prolificinteractive/patrons.svg)](https://jitpack.io/#prolificinteractive/patrons)
3+
4+
TODO: Description
5+
6+
## Installation
7+
8+
Step 1. Add the JitPack repository to your build file
9+
10+
```groovy
11+
allprojects {
12+
repositories {
13+
...
14+
maven { url 'https://jitpack.io' }
15+
}
16+
}
17+
```
18+
19+
Step 2. Add the dependency
20+
21+
```groovy
22+
dependencies {
23+
implementation 'com.github.prolificinteractive:anchored-behavior:${anchoredBehaviorVersion}'
24+
}
25+
```
26+
27+
## Usage
28+
29+
30+
31+
## Contributing to AnchoredBehavior
32+
33+
To report a bug or enhancement request, feel free to file an issue under the respective heading.
34+
35+
If you wish to contribute to the project, fork this repo and submit a pull request. Code contributions should follow the standards specified in the [Prolific Android Style Guide](https://github.com/prolificinteractive/android-code-styles).
36+
37+
## License
38+
39+
![prolific](https://s3.amazonaws.com/prolificsitestaging/logos/Prolific_Logo_Full_Color.png)
40+
41+
Copyright (c) 2018 Prolific Interactive
42+
43+
AnchoredBehavior is maintained and sponsored by Prolific Interactive. It may be redistributed under the terms specified in the [LICENSE] file.
44+
45+
[LICENSE]: ./LICENSE

anchoredbehavior/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

anchoredbehavior/build.gradle

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apply plugin: 'com.android.library'
2+
apply plugin: 'kotlin-android'
3+
apply plugin: 'com.github.dcendents.android-maven'
4+
5+
android {
6+
compileSdkVersion 27
7+
8+
defaultConfig {
9+
minSdkVersion 16
10+
targetSdkVersion 27
11+
versionCode 1
12+
versionName "1.0"
13+
14+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
15+
}
16+
}
17+
18+
dependencies {
19+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
20+
implementation 'com.android.support:appcompat-v7:27.1.1'
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<manifest package="com.prolificinteractive.anchoredbehavior"/>
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.prolificinteractive.anchoredbehavior
2+
3+
import android.support.design.widget.CoordinatorLayout
4+
import android.util.Log
5+
import android.view.View
6+
import android.view.ViewGroup
7+
import android.view.animation.DecelerateInterpolator
8+
import android.view.animation.Interpolator
9+
10+
/**
11+
* Behavior for a view to be anchored and animated according to a moving dependency.
12+
*
13+
* @param interpolator Interpolator for the animation.
14+
* @param animationHeight Height that the animation should be perform between.
15+
*/
16+
open class AnchoredBehavior<T : View>(
17+
private val animationHeight: Int,
18+
var interpolator: Interpolator = DecelerateInterpolator(2f)
19+
) : CoordinatorLayout.Behavior<T>() {
20+
21+
private var prev: Int = 0
22+
23+
override fun onDependentViewChanged(
24+
parent: CoordinatorLayout,
25+
child: T,
26+
dependency: View
27+
): Boolean {
28+
29+
val diffWidth = parent.width - dependency.width
30+
val coefY = coefficient(parent.height - child.height, child.top)
31+
val added = applyCoefWithInterpolation(diffWidth, coefY)
32+
33+
if (added != prev) {
34+
// New added size is different, button size should change.
35+
prev = added
36+
animate(parent, child, dependency, added, coefY)
37+
return true
38+
}
39+
return false
40+
}
41+
42+
/**
43+
* Animate the child view.
44+
*
45+
* @param added Margin of the button calculated based on the coefficient.
46+
* @param coefficient Coefficient to apply based on the Y position of the dependency.
47+
*/
48+
open fun animate(
49+
parent: CoordinatorLayout,
50+
child: T,
51+
dependency: View,
52+
added: Int,
53+
coefficient: Float
54+
) {
55+
val params = child.layoutParams as CoordinatorLayout.LayoutParams
56+
val dependencyParams = dependency.layoutParams as ViewGroup.MarginLayoutParams
57+
58+
// Add margin to layout.
59+
params.width = parent.width - added
60+
61+
params.leftMargin = applyCoefWithInterpolation(dependencyParams.leftMargin, coefficient)
62+
params.rightMargin = applyCoefWithInterpolation(dependencyParams.rightMargin, coefficient)
63+
64+
child.layoutParams = params
65+
}
66+
67+
/**
68+
* Apply an interpolation to a coefficient and get the result value once the coefficient applied.
69+
*
70+
* @param value The value to apply the coefficient to.
71+
* @param coefficient The coefficient input for the interpolation.
72+
* @return Return the value with coefficient and interpolation applied.
73+
*/
74+
fun applyCoefWithInterpolation(value: Int, coefficient: Float): Int {
75+
// The interpolation output needs to be reversed.
76+
return value - (value * (1f - interpolator.getInterpolation(1f - coefficient))).toInt()
77+
}
78+
79+
/**
80+
* Calculate coefficient based on position of the view ansd the dependency. It's also using the
81+
* [:animationHeight][AnchoredBehavior] for the height that the button should animate.
82+
*
83+
* @param childDefaultYPosition Child position on the Y axis.
84+
* @param dependencyYPosition Dependency position on the Y axis.
85+
* @return Coefficient from 0 to 1 (0 when the button is all the way at the bottom)
86+
*/
87+
private fun coefficient(childDefaultYPosition: Int, dependencyYPosition: Int): Float {
88+
val diff = childDefaultYPosition - dependencyYPosition
89+
90+
if (diff < 0) {
91+
return 1.0f
92+
}
93+
val coef = animationHeight - diff
94+
return if (coef < 0.0) {
95+
0.0f
96+
} else coef / animationHeight.toFloat()
97+
}
98+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<dimen name="default_animation_height">60dp</dimen>
4+
</resources>

app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

0 commit comments

Comments
 (0)