@@ -72,7 +72,7 @@ class SlidingButton : FrameLayout {
7272 field = value
7373 if (::slidingImage.isInitialized) slidingImage.scaleType = value
7474 }
75- else -> throw IllegalArgumentException (" ScaleType $value aren't allowed, please use CENTER_INSIDE,FIT_CENTER, or FIT_XY" )
75+ else -> throw IllegalArgumentException (" ScaleType $value aren't allowed, please use CENTER, CENTER_CROP, CENTER_INSIDE,FIT_CENTER, or FIT_XY" )
7676 }
7777 }
7878
@@ -297,15 +297,13 @@ class SlidingButton : FrameLayout {
297297 slidingImage.layoutParams.let { it as LayoutParams }.also {
298298 it.width = imageSize[0 ]
299299 it.height = imageSize[1 ]
300- it.setMargins(
301- buttonMargins[0 ],
302- buttonMargins[1 ],
303- buttonMargins[2 ],
304- buttonMargins[3 ]
305- )
300+ it.marginStart = buttonMargins[0 ]
301+ it.topMargin = buttonMargins[1 ]
302+ it.marginEnd = buttonMargins[2 ]
303+ it.bottomMargin = buttonMargins[3 ]
306304 slidingImage.layoutParams = it
307305 }
308- slidingImage.setPadding (
306+ slidingImage.setPaddingRelative (
309307 buttonPaddings[0 ],
310308 buttonPaddings[1 ],
311309 buttonPaddings[2 ],
@@ -316,20 +314,32 @@ class SlidingButton : FrameLayout {
316314
317315 // configure TextView
318316 slidingText.background = textBackground
319- slidingText.setPadding(textPaddings[0 ], textPaddings[1 ], textPaddings[2 ], textPaddings[3 ])
317+ slidingText.setPaddingRelative(
318+ textPaddings[0 ],
319+ textPaddings[1 ],
320+ textPaddings[2 ],
321+ textPaddings[3 ]
322+ )
320323 slidingText.text = mText
321324 slidingText.setTextSize(TypedValue .COMPLEX_UNIT_PX , mTextSize)
322325 slidingText.setTextColor(textColors)
323326 slidingText.typeface = textTypeface
324327
325328 setupSlideTouch()
329+ /* if (context.resources.configuration.layoutDirection == View.LAYOUT_DIRECTION_RTL) {
330+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
331+ slidingImage.drawable.isAutoMirrored = true
332+ } else {
333+ slidingImage.rotationY = 0F
334+ }
335+ }*/
326336 }
327337
328338 override fun onSizeChanged (w : Int , h : Int , oldw : Int , oldh : Int ) {
329339 super .onSizeChanged(w, h, oldw, oldh)
330340 startOfButton = buttonMargins[0 ].toFloat()
331341 endOfButton =
332- w.toFloat() - (imageSize[0 ].toFloat() + buttonMargins[2 ].toFloat() + ( paddingEnd * 2 ))
342+ w.toFloat() - (imageSize[0 ].toFloat() + buttonMargins[2 ].toFloat() + paddingEnd.toFloat() + paddingStart.toFloat( ))
333343 }
334344
335345 override fun removeAllViews () = throw IllegalStateException (" This method isn't allowed " )
@@ -342,9 +352,14 @@ class SlidingButton : FrameLayout {
342352 @SuppressLint(" ClickableViewAccessibility" )
343353 private fun setupSlideTouch () {
344354 setOnTouchListener { _, event ->
355+ val startTouch = paddingStart + buttonMargins[0 ]
356+ val maxStartTouch = paddingStart + paddingEnd + buttonMargins[0 ] + imageSize[0 ]
357+ val isStartTouch = event.x in startTouch.toFloat().. maxStartTouch.toFloat()
358+
359+ val maxEndTouch = this .width - paddingEnd - buttonMargins[2 ]
360+ val isEndTouch = event.x in endOfButton.. maxEndTouch.toFloat()
345361 when (event.action) {
346- MotionEvent .ACTION_DOWN -> (event.x <= slidingImage.x + slidingImage.width && slidingImage.x < slidingImage.width)
347- || (event.x >= this .width - slidingImage.width && slidingImage.x > slidingImage.width)
362+ MotionEvent .ACTION_DOWN -> (isStartTouch && ! statusActive) || (isEndTouch && statusActive)
348363 MotionEvent .ACTION_MOVE -> {
349364 onMove(event)
350365 true
@@ -358,14 +373,33 @@ class SlidingButton : FrameLayout {
358373 }
359374 }
360375
361- private fun onUp () = when {
362- slidingImage.x + slidingImage.width >= this .width * 0.58F -> {
363- animatedToEnd()
376+ private fun onUp () {
377+ // val isRtl = context.resources.configuration.layoutDirection == View.LAYOUT_DIRECTION_RTL
378+ when {
379+ slidingImage.x + slidingImage.width >= this .width * 0.58F -> {
380+ animatedToEnd()
381+ }
382+ slidingImage.x <= startOfButton -> {
383+ translateAnimation()
384+ }
385+ else -> animatedToStart()
364386 }
365- slidingImage.x <= startOfButton -> {
366- translateAnimation()
387+ }
388+
389+ fun changeStatus (active : Boolean , animated : Boolean = false) {
390+ if (animated && active) {
391+ statusActive = true
392+ animatedToEnd()
393+ } else if (animated && ! active) {
394+ statusActive = false
395+ animatedToStart()
396+ } else if (active) {
397+ slidingImage.x = endOfButton
398+ statusActive = true
399+ } else {
400+ slidingImage.x = startOfButton
401+ statusActive = false
367402 }
368- else -> animatedToStart()
369403 }
370404
371405 private fun animatedToStart () {
@@ -379,13 +413,22 @@ class SlidingButton : FrameLayout {
379413 override fun onAnimationRepeat (animation : Animator ? ) {}
380414
381415 override fun onAnimationEnd (animation : Animator ? ) {
416+ if (context.resources.configuration.layoutDirection == View .LAYOUT_DIRECTION_RTL ) {
417+ isActivated = true
418+ if (! statusActive) statusActive = true
419+ return
420+ }
382421 isActivated = false
383422 if (statusActive) statusActive = false
384423 }
385424
386425 override fun onAnimationCancel (animation : Animator ? ) {}
387426
388- override fun onAnimationStart (animation : Animator ? ) {}
427+ override fun onAnimationStart (animation : Animator ? ) {
428+ if (context.resources.configuration.layoutDirection == View .LAYOUT_DIRECTION_RTL ) {
429+ isActivated = false
430+ }
431+ }
389432 })
390433 floatAnimator.duration = 115L
391434 floatAnimator.interpolator = FastOutSlowInInterpolator ()
@@ -401,14 +444,21 @@ class SlidingButton : FrameLayout {
401444 override fun onAnimationRepeat (animation : Animator ? ) {}
402445
403446 override fun onAnimationEnd (animation : Animator ? ) {
447+ if (context.resources.configuration.layoutDirection == View .LAYOUT_DIRECTION_RTL ) {
448+ isActivated = false
449+ if (statusActive) statusActive = false
450+ return
451+ }
404452 isActivated = true
405453 if (! statusActive) statusActive = true
406454 }
407455
408456 override fun onAnimationCancel (animation : Animator ? ) {}
409457
410458 override fun onAnimationStart (animation : Animator ? ) {
411- isActivated = false
459+ if (context.resources.configuration.layoutDirection != View .LAYOUT_DIRECTION_RTL ) {
460+ isActivated = false
461+ }
412462 }
413463 })
414464 floatAnimator.duration = 115L
@@ -438,7 +488,7 @@ class SlidingButton : FrameLayout {
438488 }
439489
440490 private fun translateAnimation () {
441- val animation = TranslateAnimation (0F , measuredWidth.toFloat() , 0F , 0F )
491+ val animation = TranslateAnimation (0F , endOfButton , 0F , 0F )
442492 animation.interpolator = AccelerateDecelerateInterpolator ()
443493 animation.duration = 350L
444494 animation.setAnimationListener(object : Animation .AnimationListener {
0 commit comments