Skip to content

Commit b377037

Browse files
committed
caculate radius of the button
1 parent c12f4e0 commit b377037

File tree

7 files changed

+101
-63
lines changed

7 files changed

+101
-63
lines changed

.idea/assetWizardSettings.xml

Lines changed: 4 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ fab.setPaddingInsideButton(resources.getDimensionPixelSize(R.dimen.padding_fab))
4444
// pixel
4545
fab.setTextSize(resources.getDimensionPixelSize(R.dimen.text_size_fab).toFloat())
4646
// or
47-
fab.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14)
47+
fab.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f)
48+
// text style
49+
fab.setTypeface(Typeface.BOLD)
50+
// font
51+
val typeface = Typeface.createFromAsset(assets, "fonts/roboto.ttf")
52+
fab.setTypeface(typeface)
4853
```
4954

5055
### Layout xml
@@ -70,7 +75,7 @@ add `xmlns:app="http://schemas.android.com/apk/res-auto"`
7075
## Attributes
7176

7277
|attribute name|description|
73-
|:-:|:-:|
78+
|----------|----------|
7479
|fab_content|The content of the button|
7580
|fab_padding_text_icon|The padding between the text and icon|
7681
|fab_text_color|The color of the text|
@@ -95,7 +100,7 @@ allprojects {
95100

96101
Step 2. Add the dependency
97102
```
98-
implementation 'com.github.imtuann:FloatingActionButtonExpandable:1.1.0'
103+
implementation 'com.github.imtuann:FloatingActionButtonExpandable:1.1.1'
99104
```
100105

101106
## License

app/build.gradle

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ apply plugin: 'kotlin-android'
55
apply plugin: 'kotlin-android-extensions'
66

77
android {
8-
compileSdkVersion 28
8+
compileSdkVersion rootProject.COMPILE_SDK_VERSION
99
defaultConfig {
1010
applicationId "com.tuann.sample.floatingactionbuttonexpandable"
11-
minSdkVersion 21
12-
targetSdkVersion 28
13-
versionCode 1
14-
versionName "1.0.0"
11+
minSdkVersion rootProject.MIN_SDK_VERSION
12+
targetSdkVersion rootProject.TARGET_SDK_VERSION
13+
versionCode rootProject.VERSION_CODE
14+
versionName rootProject.VERSION_NAME
1515
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1616
}
1717
buildTypes {
@@ -25,8 +25,8 @@ android {
2525
dependencies {
2626
implementation fileTree(dir: 'libs', include: ['*.jar'])
2727
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
28-
implementation 'com.android.support:appcompat-v7:28.0.0'
29-
implementation 'com.android.support:recyclerview-v7:28.0.0'
28+
implementation "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
29+
implementation "com.android.support:recyclerview-v7:$rootProject.supportLibraryVersion"
3030
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
3131
testImplementation 'junit:junit:4.12'
3232
androidTestImplementation 'com.android.support.test:runner:1.0.2'
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24.0"
5+
android:viewportHeight="24.0">
6+
<path
7+
android:fillColor="#ffffffff"
8+
android:pathData="M20,2L4,2c-1.1,0 -1.99,0.9 -1.99,2L2,22l4,-4h14c1.1,0 2,-0.9 2,-2L22,4c0,-1.1 -0.9,-2 -2,-2zM18,14L6,14v-2h12v2zM18,11L6,11L6,9h12v2zM18,8L6,8L6,6h12v2z"/>
9+
</vector>

build.gradle

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,13 @@ allprojects {
2525
task clean(type: Delete) {
2626
delete rootProject.buildDir
2727
}
28+
29+
ext {
30+
COMPILE_SDK_VERSION = 28
31+
MIN_SDK_VERSION = 21
32+
TARGET_SDK_VERSION = 28
33+
VERSION_CODE = 1
34+
VERSION_NAME = '1.0.0'
35+
36+
supportLibraryVersion = '28.0.0'
37+
}

floatingactionbuttonexpandable/build.gradle

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ apply plugin: 'maven'
44
group = 'com.github.imtuann'
55

66
android {
7-
compileSdkVersion 28
7+
compileSdkVersion rootProject.COMPILE_SDK_VERSION
88

99
defaultConfig {
10-
minSdkVersion 21
11-
targetSdkVersion 28
12-
versionCode 1
13-
versionName "1.0.0"
10+
minSdkVersion rootProject.MIN_SDK_VERSION
11+
targetSdkVersion rootProject.TARGET_SDK_VERSION
12+
versionCode rootProject.VERSION_CODE
13+
versionName rootProject.VERSION_NAME
1414

1515
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1616
}
@@ -25,9 +25,9 @@ android {
2525

2626
dependencies {
2727
implementation fileTree(dir: 'libs', include: ['*.jar'])
28-
implementation 'com.android.support:appcompat-v7:28.0.0'
29-
implementation 'com.android.support:cardview-v7:28.0.0'
3028
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
29+
implementation "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
30+
implementation "com.android.support:cardview-v7:$rootProject.supportLibraryVersion"
3131
}
3232
repositories {
3333
mavenCentral()

floatingactionbuttonexpandable/src/main/java/com/tuann/floatingactionbuttonexpandable/FloatingActionButtonExpandable.kt

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.tuann.floatingactionbuttonexpandable
22

33
import android.content.Context
44
import android.graphics.Bitmap
5+
import android.graphics.Typeface
56
import android.graphics.drawable.Drawable
67
import android.os.Parcel
78
import android.os.Parcelable
@@ -99,36 +100,7 @@ class FloatingActionButtonExpandable @JvmOverloads constructor(
99100
.inflateTransition(R.transition.float_action_button_toggle)
100101
setExpanded(expanded)
101102

102-
viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
103-
override fun onGlobalLayout() {
104-
viewTreeObserver.removeOnGlobalLayoutListener(this)
105-
cardView.radius = (height / 2).toFloat() - 2 * resources.getDimensionPixelSize(R.dimen.card_elevation)
106-
}
107-
})
108-
}
109-
110-
fun expand(anim: Boolean = true) {
111-
if (expanded) return
112-
expanded = true
113-
execute(anim)
114-
}
115-
116-
fun collapse(anim: Boolean = true) {
117-
if (!expanded) return
118-
expanded = false
119-
execute(anim)
120-
}
121-
122-
fun toggle(anim: Boolean = true) {
123-
expanded = !expanded
124-
execute(anim)
125-
}
126-
127-
private fun execute(anim: Boolean = true) {
128-
toggle.duration = if (anim) duration else 0
129-
TransitionManager.beginDelayedTransition(root.parent as ViewGroup, toggle)
130-
tvContent.visibility = if (expanded) View.VISIBLE else View.GONE
131-
ivIcon.isActivated = expanded
103+
calculateRadius()
132104
}
133105

134106
fun setDuration(duration: Long) {
@@ -137,18 +109,22 @@ class FloatingActionButtonExpandable @JvmOverloads constructor(
137109

138110
fun setContent(content: String) {
139111
tvContent.text = content
112+
calculateRadius()
140113
}
141114

142115
fun setIconActionButton(drawable: Drawable) {
143116
ivIcon.setImageDrawable(drawable)
117+
calculateRadius()
144118
}
145119

146120
fun setIconActionButton(resId: Int) {
147121
ivIcon.setImageResource(resId)
122+
calculateRadius()
148123
}
149124

150125
fun setIconActionButton(bitmap: Bitmap) {
151126
ivIcon.setImageBitmap(bitmap)
127+
calculateRadius()
152128
}
153129

154130
fun setTextColor(color: Int) {
@@ -165,14 +141,27 @@ class FloatingActionButtonExpandable @JvmOverloads constructor(
165141

166142
fun setTextSize(size: Float) {
167143
tvContent.setTextSize(TypedValue.COMPLEX_UNIT_SP, size)
144+
calculateRadius()
168145
}
169146

170147
fun setTextSize(unit: Int, size: Float) {
171148
tvContent.setTextSize(unit, size)
149+
calculateRadius()
172150
}
173151

174152
fun setPaddingInsideButton(padding: Int) {
175153
buttonLayout.setPadding(padding, padding, padding, padding)
154+
calculateRadius()
155+
}
156+
157+
fun setTypeface(style: Int) {
158+
tvContent.setTypeface(tvContent.typeface, style)
159+
calculateRadius()
160+
}
161+
162+
fun setTypeface(typeface: Typeface) {
163+
tvContent.typeface = typeface
164+
calculateRadius()
176165
}
177166

178167
fun setExpanded(expanded: Boolean) {
@@ -181,6 +170,43 @@ class FloatingActionButtonExpandable @JvmOverloads constructor(
181170
ivIcon.isActivated = expanded
182171
}
183172

173+
fun expand(anim: Boolean = true) {
174+
if (expanded) return
175+
expanded = true
176+
execute(anim)
177+
}
178+
179+
fun collapse(anim: Boolean = true) {
180+
if (!expanded) return
181+
expanded = false
182+
execute(anim)
183+
}
184+
185+
fun toggle(anim: Boolean = true) {
186+
expanded = !expanded
187+
execute(anim)
188+
}
189+
190+
private fun execute(anim: Boolean = true) {
191+
toggle.duration = if (anim) duration else 0
192+
TransitionManager.beginDelayedTransition(root.parent as ViewGroup, toggle)
193+
tvContent.visibility = if (expanded) View.VISIBLE else View.GONE
194+
ivIcon.isActivated = expanded
195+
calculateRadius()
196+
}
197+
198+
/**
199+
* Calculates the radius of the button
200+
*/
201+
private fun calculateRadius() {
202+
cardView.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
203+
override fun onGlobalLayout() {
204+
cardView.viewTreeObserver.removeOnGlobalLayoutListener(this)
205+
cardView.radius = ((height / 2) - 2 * resources.getDimensionPixelSize(R.dimen.card_elevation)).toFloat()
206+
}
207+
})
208+
}
209+
184210
override fun onSaveInstanceState(): Parcelable {
185211
val savedState = SavedState(super.onSaveInstanceState())
186212
savedState.expanded = expanded

0 commit comments

Comments
 (0)