From 437884e384184a55d86ba105df27136c2f43209d Mon Sep 17 00:00:00 2001 From: nuarz71 Date: Thu, 9 Apr 2020 14:50:17 +0800 Subject: [PATCH] up to 1.0.2 --- .../sample/swipebuttonexample/MainActivity.kt | 9 ++ app/src/main/res/layout/activity_main.xml | 5 +- slidingbutton/build.gradle | 4 +- .../ss564/lib/slidingbutton/SlidingButton.kt | 92 ++++++++++++++----- 4 files changed, 86 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/id/ss564/sample/swipebuttonexample/MainActivity.kt b/app/src/main/java/id/ss564/sample/swipebuttonexample/MainActivity.kt index d28cd01..d9a7078 100644 --- a/app/src/main/java/id/ss564/sample/swipebuttonexample/MainActivity.kt +++ b/app/src/main/java/id/ss564/sample/swipebuttonexample/MainActivity.kt @@ -2,11 +2,20 @@ package id.ss564.sample.swipebuttonexample import androidx.appcompat.app.AppCompatActivity import android.os.Bundle +import android.os.Handler +import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) + slidingButton.setOnStatusChangeListener { + if(it){ + Handler().postDelayed({ + slidingButton.changeStatus(false,true) + },3000L) + } + } } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 4902e7e..ba335f9 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -13,6 +13,10 @@ android:layout_marginStart="16dp" android:layout_marginEnd="16dp" android:background="@drawable/sliding_shape_background" + app:sliding_button_marginTop="4dp" + app:sliding_button_marginStart="4dp" + app:sliding_button_marginEnd="4dp" + app:sliding_button_marginBottom="4dp" app:layout_constraintBottom_toTopOf="@id/slidingButton1" app:layout_constraintTop_toTopOf="parent" /> @@ -74,5 +78,4 @@ app:sliding_button_width="48dp" app:sliding_icon_scaleType="centerInside" app:sliding_text="@null" /> - \ No newline at end of file diff --git a/slidingbutton/build.gradle b/slidingbutton/build.gradle index 45888ac..84358e7 100644 --- a/slidingbutton/build.gradle +++ b/slidingbutton/build.gradle @@ -9,8 +9,8 @@ android { defaultConfig { minSdkVersion 17 targetSdkVersion 29 - versionCode 3 - versionName "1.0.1" + versionCode 4 + versionName "1.0.2" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles 'consumer-rules.pro' diff --git a/slidingbutton/src/main/java/id/ss564/lib/slidingbutton/SlidingButton.kt b/slidingbutton/src/main/java/id/ss564/lib/slidingbutton/SlidingButton.kt index 76a77ee..bd92d18 100644 --- a/slidingbutton/src/main/java/id/ss564/lib/slidingbutton/SlidingButton.kt +++ b/slidingbutton/src/main/java/id/ss564/lib/slidingbutton/SlidingButton.kt @@ -72,7 +72,7 @@ class SlidingButton : FrameLayout { field = value if (::slidingImage.isInitialized) slidingImage.scaleType = value } - else -> throw IllegalArgumentException("ScaleType $value aren't allowed, please use CENTER_INSIDE,FIT_CENTER, or FIT_XY") + else -> throw IllegalArgumentException("ScaleType $value aren't allowed, please use CENTER, CENTER_CROP, CENTER_INSIDE,FIT_CENTER, or FIT_XY") } } @@ -297,15 +297,13 @@ class SlidingButton : FrameLayout { slidingImage.layoutParams.let { it as LayoutParams }.also { it.width = imageSize[0] it.height = imageSize[1] - it.setMargins( - buttonMargins[0], - buttonMargins[1], - buttonMargins[2], - buttonMargins[3] - ) + it.marginStart = buttonMargins[0] + it.topMargin = buttonMargins[1] + it.marginEnd = buttonMargins[2] + it.bottomMargin = buttonMargins[3] slidingImage.layoutParams = it } - slidingImage.setPadding( + slidingImage.setPaddingRelative( buttonPaddings[0], buttonPaddings[1], buttonPaddings[2], @@ -316,20 +314,32 @@ class SlidingButton : FrameLayout { //configure TextView slidingText.background = textBackground - slidingText.setPadding(textPaddings[0], textPaddings[1], textPaddings[2], textPaddings[3]) + slidingText.setPaddingRelative( + textPaddings[0], + textPaddings[1], + textPaddings[2], + textPaddings[3] + ) slidingText.text = mText slidingText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize) slidingText.setTextColor(textColors) slidingText.typeface = textTypeface setupSlideTouch() + /*if (context.resources.configuration.layoutDirection == View.LAYOUT_DIRECTION_RTL) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + slidingImage.drawable.isAutoMirrored = true + } else { + slidingImage.rotationY = 0F + } + }*/ } override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { super.onSizeChanged(w, h, oldw, oldh) startOfButton = buttonMargins[0].toFloat() endOfButton = - w.toFloat() - (imageSize[0].toFloat() + buttonMargins[2].toFloat() + (paddingEnd * 2)) + w.toFloat() - (imageSize[0].toFloat() + buttonMargins[2].toFloat() + paddingEnd.toFloat() + paddingStart.toFloat()) } override fun removeAllViews() = throw IllegalStateException("This method isn't allowed ") @@ -342,9 +352,14 @@ class SlidingButton : FrameLayout { @SuppressLint("ClickableViewAccessibility") private fun setupSlideTouch() { setOnTouchListener { _, event -> + val startTouch = paddingStart + buttonMargins[0] + val maxStartTouch = paddingStart + paddingEnd + buttonMargins[0] + imageSize[0] + val isStartTouch = event.x in startTouch.toFloat()..maxStartTouch.toFloat() + + val maxEndTouch = this.width - paddingEnd - buttonMargins[2] + val isEndTouch = event.x in endOfButton..maxEndTouch.toFloat() when (event.action) { - MotionEvent.ACTION_DOWN -> (event.x <= slidingImage.x + slidingImage.width && slidingImage.x < slidingImage.width) - || (event.x >= this.width - slidingImage.width && slidingImage.x > slidingImage.width) + MotionEvent.ACTION_DOWN -> (isStartTouch && !statusActive) || (isEndTouch && statusActive) MotionEvent.ACTION_MOVE -> { onMove(event) true @@ -358,14 +373,33 @@ class SlidingButton : FrameLayout { } } - private fun onUp() = when { - slidingImage.x + slidingImage.width >= this.width * 0.58F -> { - animatedToEnd() + private fun onUp() { +// val isRtl = context.resources.configuration.layoutDirection == View.LAYOUT_DIRECTION_RTL + when { + slidingImage.x + slidingImage.width >= this.width * 0.58F -> { + animatedToEnd() + } + slidingImage.x <= startOfButton -> { + translateAnimation() + } + else -> animatedToStart() } - slidingImage.x <= startOfButton -> { - translateAnimation() + } + + fun changeStatus(active: Boolean, animated: Boolean = false) { + if (animated && active) { + statusActive = true + animatedToEnd() + } else if (animated && !active) { + statusActive = false + animatedToStart() + } else if (active) { + slidingImage.x = endOfButton + statusActive = true + } else { + slidingImage.x = startOfButton + statusActive = false } - else -> animatedToStart() } private fun animatedToStart() { @@ -379,13 +413,22 @@ class SlidingButton : FrameLayout { override fun onAnimationRepeat(animation: Animator?) {} override fun onAnimationEnd(animation: Animator?) { + if (context.resources.configuration.layoutDirection == View.LAYOUT_DIRECTION_RTL) { + isActivated = true + if (!statusActive) statusActive = true + return + } isActivated = false if (statusActive) statusActive = false } override fun onAnimationCancel(animation: Animator?) {} - override fun onAnimationStart(animation: Animator?) {} + override fun onAnimationStart(animation: Animator?) { + if (context.resources.configuration.layoutDirection == View.LAYOUT_DIRECTION_RTL) { + isActivated = false + } + } }) floatAnimator.duration = 115L floatAnimator.interpolator = FastOutSlowInInterpolator() @@ -401,6 +444,11 @@ class SlidingButton : FrameLayout { override fun onAnimationRepeat(animation: Animator?) {} override fun onAnimationEnd(animation: Animator?) { + if (context.resources.configuration.layoutDirection == View.LAYOUT_DIRECTION_RTL) { + isActivated = false + if (statusActive) statusActive = false + return + } isActivated = true if (!statusActive) statusActive = true } @@ -408,7 +456,9 @@ class SlidingButton : FrameLayout { override fun onAnimationCancel(animation: Animator?) {} override fun onAnimationStart(animation: Animator?) { - isActivated = false + if (context.resources.configuration.layoutDirection != View.LAYOUT_DIRECTION_RTL) { + isActivated = false + } } }) floatAnimator.duration = 115L @@ -438,7 +488,7 @@ class SlidingButton : FrameLayout { } private fun translateAnimation() { - val animation = TranslateAnimation(0F, measuredWidth.toFloat(), 0F, 0F) + val animation = TranslateAnimation(0F, endOfButton, 0F, 0F) animation.interpolator = AccelerateDecelerateInterpolator() animation.duration = 350L animation.setAnimationListener(object : Animation.AnimationListener {