|
|
@@ -1,18 +1,21 @@
|
|
|
package com.adealink.weparty.commonui.widget
|
|
|
|
|
|
import android.content.Context
|
|
|
+import android.content.res.ColorStateList
|
|
|
import android.graphics.Typeface
|
|
|
import android.graphics.drawable.GradientDrawable
|
|
|
import android.graphics.drawable.StateListDrawable
|
|
|
import android.util.AttributeSet
|
|
|
import android.view.Gravity
|
|
|
import androidx.appcompat.widget.AppCompatTextView
|
|
|
+import androidx.core.content.withStyledAttributes
|
|
|
import com.adealink.frame.aab.util.getCompatColor
|
|
|
import com.adealink.frame.aab.util.getCompatFont
|
|
|
-import com.adealink.frame.util.DisplayUtil
|
|
|
import com.adealink.weparty.R
|
|
|
import com.adealink.weparty.commonui.drawabletoolbox.DrawableBuilder
|
|
|
import com.adealink.weparty.commonui.drawabletoolbox.FlipDrawable
|
|
|
+import com.adealink.weparty.commonui.ext.dp
|
|
|
+import com.adealink.weparty.commonui.ext.dpf
|
|
|
|
|
|
class CommonButton @JvmOverloads constructor(
|
|
|
context: Context,
|
|
|
@@ -20,7 +23,14 @@ class CommonButton @JvmOverloads constructor(
|
|
|
defStyleAttr: Int = 0,
|
|
|
) : AppCompatTextView(context, attrs, defStyleAttr) {
|
|
|
|
|
|
+ companion object {
|
|
|
+ const val BUTTON_NORMAL = 0
|
|
|
+ const val BUTTON_CONFIRM = 1
|
|
|
+ const val BUTTON_CANCEL = 2
|
|
|
+ }
|
|
|
+
|
|
|
private var buttonRadius = 0f
|
|
|
+ private var buttonType = BUTTON_NORMAL
|
|
|
|
|
|
init {
|
|
|
gravity = Gravity.CENTER
|
|
|
@@ -34,70 +44,139 @@ class CommonButton @JvmOverloads constructor(
|
|
|
setBackgroundResource(R.drawable.common_button_bg)
|
|
|
return
|
|
|
}
|
|
|
- val ta = context.theme.obtainStyledAttributes(
|
|
|
- attrs,
|
|
|
- R.styleable.CommonButton,
|
|
|
- 0, 0
|
|
|
- ).apply {
|
|
|
-
|
|
|
+ context.withStyledAttributes(attrs, R.styleable.CommonButton) {
|
|
|
+ buttonRadius = getDimension(
|
|
|
+ R.styleable.CommonButton_button_radius,
|
|
|
+ 24.dpf()
|
|
|
+ )
|
|
|
+ buttonType = getInt(
|
|
|
+ R.styleable.CommonButton_common_button_type,
|
|
|
+ BUTTON_NORMAL
|
|
|
+ )
|
|
|
}
|
|
|
- buttonRadius = ta.getDimension(
|
|
|
- R.styleable.CommonButton_button_radius,
|
|
|
- DisplayUtil.dp2px(24f).toFloat()
|
|
|
- )
|
|
|
- ta.recycle()
|
|
|
|
|
|
- background = StateListDrawable().apply {
|
|
|
- addState(
|
|
|
- intArrayOf(-android.R.attr.state_enabled),
|
|
|
- DrawableBuilder()
|
|
|
- .gradient(true)
|
|
|
- .orientation(FlipDrawable.ORIENTATION_HORIZONTAL)
|
|
|
- .gradientRadius(buttonRadius)
|
|
|
- .cornerRadius(buttonRadius.toInt())
|
|
|
- .gradientColors(
|
|
|
-// getCompatColor(R.color.color_FFB0F5EA),
|
|
|
-// getCompatColor(R.color.color_FFA3CDF9),
|
|
|
- getCompatColor(R.color.color_FFB0F5EA),
|
|
|
- getCompatColor(R.color.color_FFA3CDF9),
|
|
|
- null
|
|
|
+ initView()
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun initView() {
|
|
|
+ when (buttonType) {
|
|
|
+ BUTTON_CANCEL -> {
|
|
|
+ background = StateListDrawable().apply {
|
|
|
+ addState(
|
|
|
+ intArrayOf(-android.R.attr.state_enabled),
|
|
|
+ DrawableBuilder()
|
|
|
+ .cornerRadius(buttonRadius.toInt())
|
|
|
+ .solidColor(getCompatColor(R.color.white))
|
|
|
+ .strokeColor(getCompatColor(R.color.color_FF4E5969))
|
|
|
+ .strokeWidth(1.dp())
|
|
|
+ .build()
|
|
|
)
|
|
|
- .gradientType(GradientDrawable.LINEAR_GRADIENT)
|
|
|
- .angle(0)
|
|
|
- .build()
|
|
|
- )
|
|
|
- addState(
|
|
|
- intArrayOf(android.R.attr.state_pressed),
|
|
|
- DrawableBuilder()
|
|
|
- .gradient(true)
|
|
|
- .orientation(FlipDrawable.ORIENTATION_HORIZONTAL)
|
|
|
- .gradientRadius(buttonRadius)
|
|
|
- .cornerRadius(buttonRadius.toInt())
|
|
|
- .gradientColors(
|
|
|
- getCompatColor(R.color.color_FF48E6C2),
|
|
|
- getCompatColor(R.color.color_FF2A86E6),
|
|
|
- null
|
|
|
+ addState(
|
|
|
+ intArrayOf(android.R.attr.state_pressed),
|
|
|
+ DrawableBuilder()
|
|
|
+ .cornerRadius(buttonRadius.toInt())
|
|
|
+ .solidColor(getCompatColor(R.color.white))
|
|
|
+ .strokeColor(getCompatColor(R.color.color_FF4E5969))
|
|
|
+ .strokeWidth(1.dp())
|
|
|
+ .build()
|
|
|
)
|
|
|
- .gradientType(GradientDrawable.LINEAR_GRADIENT)
|
|
|
- .angle(0)
|
|
|
- .build()
|
|
|
- )
|
|
|
- addState(
|
|
|
- intArrayOf(),
|
|
|
- DrawableBuilder()
|
|
|
- .gradient(true)
|
|
|
- .orientation(FlipDrawable.ORIENTATION_HORIZONTAL)
|
|
|
- .gradientRadius(buttonRadius)
|
|
|
- .cornerRadius(buttonRadius.toInt())
|
|
|
- .gradientColors(
|
|
|
- getCompatColor(R.color.color_FF50FFD8),
|
|
|
- getCompatColor(R.color.color_FF2F95FF),
|
|
|
- null
|
|
|
+ addState(
|
|
|
+ intArrayOf(),
|
|
|
+ DrawableBuilder()
|
|
|
+ .cornerRadius(buttonRadius.toInt())
|
|
|
+ .solidColor(getCompatColor(R.color.white))
|
|
|
+ .strokeColor(getCompatColor(R.color.color_FF4E5969))
|
|
|
+ .strokeWidth(1.dp())
|
|
|
+ .build()
|
|
|
)
|
|
|
- .gradientType(GradientDrawable.LINEAR_GRADIENT)
|
|
|
- .angle(0)
|
|
|
- .build()
|
|
|
- )
|
|
|
+ }
|
|
|
+
|
|
|
+ setTextColor(
|
|
|
+ ColorStateList(
|
|
|
+ arrayOf(
|
|
|
+ intArrayOf(-android.R.attr.state_enabled), //不可点击
|
|
|
+ intArrayOf(android.R.attr.state_pressed), //按压
|
|
|
+ intArrayOf() //一般状态
|
|
|
+ ),
|
|
|
+ intArrayOf(
|
|
|
+ getCompatColor(R.color.color_FF4E5969),
|
|
|
+ getCompatColor(R.color.color_FF4E5969),
|
|
|
+ getCompatColor(R.color.color_FF4E5969),
|
|
|
+ )
|
|
|
+ )
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ else -> {
|
|
|
+ // BUTTON_NORMAL
|
|
|
+ // BUTTON_CONFIRM
|
|
|
+ background = StateListDrawable().apply {
|
|
|
+ addState(
|
|
|
+ intArrayOf(-android.R.attr.state_enabled),
|
|
|
+ DrawableBuilder()
|
|
|
+ .gradient(true)
|
|
|
+ .orientation(FlipDrawable.ORIENTATION_HORIZONTAL)
|
|
|
+ .gradientRadius(buttonRadius)
|
|
|
+ .cornerRadius(buttonRadius.toInt())
|
|
|
+ .gradientColors(
|
|
|
+ getCompatColor(R.color.color_FFB0F5EA),
|
|
|
+ getCompatColor(R.color.color_FFA3CDF9),
|
|
|
+ null
|
|
|
+ )
|
|
|
+ .gradientType(GradientDrawable.LINEAR_GRADIENT)
|
|
|
+ .angle(0)
|
|
|
+ .build()
|
|
|
+ )
|
|
|
+ addState(
|
|
|
+ intArrayOf(android.R.attr.state_pressed),
|
|
|
+ DrawableBuilder()
|
|
|
+ .gradient(true)
|
|
|
+ .orientation(FlipDrawable.ORIENTATION_HORIZONTAL)
|
|
|
+ .gradientRadius(buttonRadius)
|
|
|
+ .cornerRadius(buttonRadius.toInt())
|
|
|
+ .gradientColors(
|
|
|
+ getCompatColor(R.color.color_FF48E6C2),
|
|
|
+ getCompatColor(R.color.color_FF2A86E6),
|
|
|
+ null
|
|
|
+ )
|
|
|
+ .gradientType(GradientDrawable.LINEAR_GRADIENT)
|
|
|
+ .angle(0)
|
|
|
+ .build()
|
|
|
+ )
|
|
|
+ addState(
|
|
|
+ intArrayOf(),
|
|
|
+ DrawableBuilder()
|
|
|
+ .gradient(true)
|
|
|
+ .orientation(FlipDrawable.ORIENTATION_HORIZONTAL)
|
|
|
+ .gradientRadius(buttonRadius)
|
|
|
+ .cornerRadius(buttonRadius.toInt())
|
|
|
+ .gradientColors(
|
|
|
+ getCompatColor(R.color.color_FF50FFD8),
|
|
|
+ getCompatColor(R.color.color_FF2F95FF),
|
|
|
+ null
|
|
|
+ )
|
|
|
+ .gradientType(GradientDrawable.LINEAR_GRADIENT)
|
|
|
+ .angle(0)
|
|
|
+ .build()
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ setTextColor(
|
|
|
+ ColorStateList(
|
|
|
+ arrayOf(
|
|
|
+ intArrayOf(-android.R.attr.state_enabled), //不可点击
|
|
|
+ intArrayOf(android.R.attr.state_pressed), //按压
|
|
|
+ intArrayOf() //一般状态
|
|
|
+ ),
|
|
|
+ intArrayOf(
|
|
|
+ getCompatColor(R.color.white),
|
|
|
+ getCompatColor(R.color.white),
|
|
|
+ getCompatColor(R.color.white),
|
|
|
+ )
|
|
|
+ )
|
|
|
+ )
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|