Skip to content

Commit 1a7ccf4

Browse files
authored
Merge pull request #8 from timonknispel/develop
Develop
2 parents e87865e + 67204bc commit 1a7ccf4

4 files changed

Lines changed: 40 additions & 24 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,4 @@ test_button.reset()
119119
| failedColor | Color | Sets the background color for a failed result |[]|#F44336 |
120120
| autoResetButtonAfterResult | Boolean | Decides if the button should reset itself after the result was displayed (after 1,5 seconds) |[]| true |
121121
| progressStyle | [INTERMEDIATE or PROGRESS] | Decides if the button should intermediate or display a progress |[]| INTERMEDIATE |
122+
| border_thickness | Dimension (DP) | sets the thickness of the outline border and progress circle |[]| 1dp |

ktloadingbutton/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ android {
1313
defaultConfig {
1414
minSdkVersion 21
1515
targetSdkVersion 29
16-
versionCode 2
17-
versionName "1.0.2"
16+
versionCode 3
17+
versionName "1.1.0"
1818

1919
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
2020
consumerProguardFiles 'consumer-rules.pro'

ktloadingbutton/src/main/java/de/timonknispel/ktloadingbutton/KTLoadingButton.kt

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import android.view.animation.AccelerateInterpolator
1313
import androidx.core.graphics.ColorUtils
1414
import java.util.*
1515
import kotlin.math.ceil
16+
import kotlin.math.sqrt
1617

1718
class KTLoadingButton : View {
1819

@@ -29,6 +30,7 @@ class KTLoadingButton : View {
2930
private var textSize = 0
3031
private var _loadingBGPaint = 0
3132
private var progressStyle = ProgressStyle.INTERMEDIATE
33+
private var borderThickness = 0F
3234

3335
private var submitAnim: ValueAnimator? = null
3436
private var loadingAnim: ValueAnimator? = null
@@ -104,14 +106,21 @@ class KTLoadingButton : View {
104106
R.styleable.KTLoadingButton_loadingBackgroundColor,
105107
-1
106108
)
107-
textSize = typedArray.getDimension(
108-
R.styleable.KTLoadingButton_buttonTextSize,
109-
sp2px(16f).toFloat()
110-
).toInt()
109+
textSize = sp2px(
110+
typedArray.getDimension(
111+
R.styleable.KTLoadingButton_buttonTextSize,
112+
16F
113+
)
114+
)
111115
shouldAutoResetAfterResult = typedArray.getBoolean(
112116
R.styleable.KTLoadingButton_autoResetButtonAfterResult,
113117
true
114118
)
119+
borderThickness =
120+
typedArray.getDimension(
121+
R.styleable.KTLoadingButton_border_thickness,
122+
1F
123+
)
115124
progressStyle =
116125
ProgressStyle.formID(
117126
typedArray.getInt(
@@ -121,22 +130,22 @@ class KTLoadingButton : View {
121130
)
122131
typedArray.recycle()
123132
}
124-
this.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
133+
this.setLayerType(LAYER_TYPE_SOFTWARE, null)
125134
setup()
126135
}
127136

128137
private fun setup() {
129138
bgPaint = Paint().apply {
130139
color = buttonColor
131140
style = Paint.Style.STROKE
132-
strokeWidth = 5f
141+
strokeWidth = dp2px(borderThickness)
133142
isAntiAlias = true
134143
}
135144

136145
loadingPaint = Paint().apply {
137146
color = buttonColor
138147
style = Paint.Style.STROKE
139-
strokeWidth = 9f
148+
strokeWidth = dp2px(borderThickness)
140149
isAntiAlias = true
141150
}
142151

@@ -146,21 +155,21 @@ class KTLoadingButton : View {
146155
70
147156
)
148157
style = Paint.Style.STROKE
149-
strokeWidth = 9f
158+
strokeWidth = dp2px(borderThickness)
150159
isAntiAlias = false
151160
}
152161

153162
resultPaint = Paint().apply {
154163
color = Color.WHITE
155164
style = Paint.Style.STROKE
156-
strokeWidth = 9f
165+
strokeWidth = textSize
157166
strokeCap = Paint.Cap.ROUND
158167
isAntiAlias = true
159168
}
160169

161170
textPaint = Paint().apply {
162171
color = buttonColor
163-
strokeWidth = (this@KTLoadingButton.textSize / 6F)
172+
strokeWidth = this@KTLoadingButton.textSize / 6F
164173
this.textSize = this@KTLoadingButton.textSize.toFloat()
165174
isAntiAlias = true
166175
}
@@ -223,25 +232,25 @@ class KTLoadingButton : View {
223232
}
224233

225234
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
226-
val widthMode = View.MeasureSpec.getMode(widthMeasureSpec)
227-
var widthSize = View.MeasureSpec.getSize(widthMeasureSpec)
228-
val heightMode = View.MeasureSpec.getMode(heightMeasureSpec)
229-
var heightSize = View.MeasureSpec.getSize(heightMeasureSpec)
235+
val widthMode = MeasureSpec.getMode(widthMeasureSpec)
236+
var widthSize = MeasureSpec.getSize(widthMeasureSpec)
237+
val heightMode = MeasureSpec.getMode(heightMeasureSpec)
238+
var heightSize = MeasureSpec.getSize(heightMeasureSpec)
230239

231-
if (widthMode == View.MeasureSpec.AT_MOST) {
232-
widthSize = textWidth + 100
240+
if (widthMode == MeasureSpec.AT_MOST) {
241+
widthSize = textWidth + 100 + (dp2px(borderThickness) * 2).toInt()
233242
}
234243

235-
if (heightMode == View.MeasureSpec.AT_MOST) {
236-
heightSize = (textHeight * 3F).toInt()
244+
if (heightMode == MeasureSpec.AT_MOST) {
245+
heightSize = (textHeight * 3F).toInt() + (dp2px(borderThickness) * 2).toInt()
237246
}
238247

239248
if (heightSize > widthSize) {
240-
heightSize = (widthSize * 0.25F).toInt()
249+
heightSize = (widthSize * 0.25F).toInt() + (dp2px(borderThickness) * 2).toInt()
241250
}
242251

243-
mWidth = widthSize - 5
244-
mHeight = heightSize - 5
252+
mWidth = widthSize - (dp2px(borderThickness) * 2).toInt()
253+
mHeight = heightSize - (dp2px(borderThickness) * 2).toInt()
245254
x = (widthSize * 0.5F).toInt()
246255
y = (heightSize * 0.5F).toInt()
247256
maxWidth = mWidth
@@ -317,7 +326,7 @@ class KTLoadingButton : View {
317326
private fun drawResult(canvas: Canvas, isSucceed: Boolean) {
318327
if (isSucceed) {
319328
resultPath.moveTo((-mHeight / 6).toFloat(), 0f)
320-
resultPath.lineTo(0f, (-mHeight / 6 + (1 + Math.sqrt(5.0)) * mHeight / 12).toFloat())
329+
resultPath.lineTo(0f, (-mHeight / 6 + (1 + sqrt(5.0)) * mHeight / 12).toFloat())
321330
resultPath.lineTo((mHeight / 6).toFloat(), (-mHeight / 6).toFloat())
322331
} else {
323332
resultPath.moveTo((-mHeight / 6).toFloat(), (mHeight / 6).toFloat())
@@ -470,6 +479,11 @@ class KTLoadingButton : View {
470479
return (sp * fontScale + 0.5f).toInt()
471480
}
472481

482+
private fun dp2px(dp: Float): Float {
483+
val dpScale = context.resources.displayMetrics.density
484+
return (dp * dpScale)
485+
}
486+
473487
private fun getTextWidth(paint: Paint, str: String?): Int {
474488
var mRet = 0
475489
if (!str.isNullOrEmpty()) {

ktloadingbutton/src/main/res/values/attrs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
<enum name="PROGRESS" value="1" />
1414
</attr>
1515
<attr name="allCaps" format="boolean"/>
16+
<attr name="border_thickness" format="dimension"/>
1617
</declare-styleable>
1718
</resources>

0 commit comments

Comments
 (0)