美文网首页
Android 自定义Button

Android 自定义Button

作者: 因为我的心 | 来源:发表于2022-05-30 23:07 被阅读0次

一、前言:

image.png

1、使用:

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#fff"
            android:gravity="center_vertical"
            android:layout_marginLeft="@dimen/ui_dp15"
            android:layout_marginRight="@dimen/ui_dp15"
            android:orientation="horizontal"
            >

            <com.weifeng.hobby.commonui.views.HobbyShapeTextView
                android:id="@+id/follow"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:text="取消"
                android:textColor="#333333"
                android:textSize="16sp"
                android:textStyle="bold"
                app:cornersRadius="20dp"
                app:gradientNormalEndColor="#00000000"
                app:gradientNormalStartColor="#00000000"
                app:shape="rectangle"
                app:tv_strokeColor="#333333"
                app:tv_strokeWidth="1dp" />

            <View
                android:layout_width="21dp"
                android:layout_height="0dp" />

            <com.weifeng.hobby.commonui.views.HobbyShapeTextView
                android:id="@+id/chat"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:text="确定"
                android:textColor="@color/cui_color_white"
                android:textSize="16sp"
                android:textStyle="bold"
                app:cornersRadius="20dp"
                app:gradientNormalEndColor="@color/ui_black"
                app:gradientNormalStartColor="@color/ui_black"
                app:shape="rectangle" />

        </LinearLayout>

2、控件:

package com.weifeng.hobby.commonui.views

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.StateListDrawable
import android.util.AttributeSet
import android.view.Gravity
import androidx.appcompat.widget.AppCompatTextView
import com.weifeng.hobby.commonui.R

/**
 * description:
 *
 * @date 2022/3/29
 */
open class HobbyShapeTextView : AppCompatTextView {
    /***
     * Rect angel
     */
    val SHAPE_RECTANGEL = 0

    /***
     * 形状
     */
    var shape = 0

    /***
     * 按压基础颜色
     */
    var solidNormalColor = 0

    /***
     * 按压颜色
     */
    var solidPressedColor = 0

    /***
     * 圆
     */
    var cornersRadius = 0f

    /***
     * 左上
     */
    var cornersTopLeft = 0f

    /***
     * 右上
     */
    var cornersTopRight = 0f

    /***
     * 左下
     */
    var cornersBottomLeft = 0f

    /***
     * 右下
     */
    var cornersBottomRight = 0f

    /***
     * 渐变颜色属性
     */
    var gradientNormalStartColor = 0

    /***
     * 渐变颜色属性
     */
    var gradientNormalCenterColor = 0

    /***
     * 渐变颜色属性
     */
    var gradientNormalEndColor = 0

    /***
     * 按压颜色属性
     */
    var gradientPressedStartColor = 0

    /***
     * 按压颜色属性
     */
    var gradientPressedCenterColor = 0

    /***
     * 按压结束颜色
     */
    var gradientPressedEndColor = 0

    /***
     * 按压颜色属性
     */
    var gradientOrientation = 0

    /***
     *
     */
    var strokeWidth = 0f

    /***
     *
     */
    var strokeColor = 0

    /***
     * 默认颜色
     */
    val defaultColor = Color.parseColor("#00000000")

    lateinit var orientations: Array<GradientDrawable.Orientation>

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initView(attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
            context,
            attrs,
            defStyleAttr
    ) {

        initView(attrs)
    }

    private fun initView(attrs: AttributeSet?) {

        val array = context.obtainStyledAttributes(attrs, R.styleable.ShapeTextView)
        shape = array.getInteger(R.styleable.ShapeTextView_shape, SHAPE_RECTANGEL)
        solidNormalColor = array.getColor(R.styleable.ShapeTextView_solidNormal, defaultColor)
        solidPressedColor = array.getColor(R.styleable.ShapeTextView_solidPressed, defaultColor)
        cornersRadius = array.getDimension(R.styleable.ShapeTextView_cornersRadius, 0f)
        cornersTopLeft = array.getDimension(R.styleable.ShapeTextView_cornerTopLeft, 0f)
        cornersTopRight = array.getDimension(R.styleable.ShapeTextView_cornerTopRight, 0f)
        cornersBottomLeft = array.getDimension(R.styleable.ShapeTextView_cornerBottomLeft, 0f)
        cornersBottomRight = array.getDimension(R.styleable.ShapeTextView_cornerBottomRight, 0f)
        strokeWidth = array.getDimension(R.styleable.ShapeTextView_tv_strokeWidth, 0f)
        strokeColor = array.getColor(R.styleable.ShapeTextView_tv_strokeColor, defaultColor)
        gradientNormalStartColor = array.getColor(R.styleable.ShapeTextView_gradientNormalStartColor, defaultColor)
        gradientNormalCenterColor = array.getColor(R.styleable.ShapeTextView_gradientNormalCenterColor, defaultColor)
        gradientNormalEndColor = array.getColor(R.styleable.ShapeTextView_gradientNormalEndColor, defaultColor)
        gradientPressedStartColor = array.getColor(R.styleable.ShapeTextView_gradientPressedStartColor, defaultColor)
        gradientPressedCenterColor = array.getColor(R.styleable.ShapeTextView_gradientPressedCenterColor, defaultColor)
        gradientPressedEndColor = array.getColor(R.styleable.ShapeTextView_gradientPressedEndColor, defaultColor)
        val orientationArray = context.obtainStyledAttributes(attrs, R.styleable.ShapeTextView)
        gradientOrientation = orientationArray.getInteger(R.styleable.ShapeTextView_gradientOrientation, 6)
        array.recycle()
        orientations = arrayOf(
                GradientDrawable.Orientation.TOP_BOTTOM,
                GradientDrawable.Orientation.TR_BL,
                GradientDrawable.Orientation.RIGHT_LEFT,
                GradientDrawable.Orientation.BR_TL,
                GradientDrawable.Orientation.BOTTOM_TOP,
                GradientDrawable.Orientation.BL_TR,
                GradientDrawable.Orientation.LEFT_RIGHT,
                GradientDrawable.Orientation.TL_BR
        )
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setShape()
    }

    open fun setShape() {
        setGravity(Gravity.CENTER)
        setClickable(true)
        // normal state
        val drawableNormal = GradientDrawable()
        // 设置Shape
        drawableNormal.shape = shape
        // 设置圆角半径
        drawableNormal.cornerRadius = cornersRadius
        // 圆角半径(每个圆角半径的值)
        if (cornersRadius == 0f) {
            drawableNormal.cornerRadii = floatArrayOf(
                    cornersTopLeft, cornersTopLeft,
                    cornersTopRight, cornersTopRight,
                    cornersBottomRight, cornersBottomRight,
                    cornersBottomLeft, cornersBottomLeft)
        }
        //描边的宽度和颜色
        drawableNormal.setStroke(strokeWidth.toInt(), strokeColor)
        //设置填充色
        if (solidNormalColor != defaultColor) {
            drawableNormal.setColor(solidNormalColor)
        } else {
//            设置渐变色
            var gradientColors: IntArray
            if (gradientNormalStartColor != defaultColor && gradientNormalEndColor != defaultColor) {
                gradientColors = intArrayOf(gradientNormalStartColor, gradientNormalEndColor)
                if (gradientNormalCenterColor != defaultColor) {
                    gradientColors = intArrayOf(gradientNormalStartColor, gradientNormalCenterColor, gradientNormalEndColor)
                }
                drawableNormal.colors = gradientColors
                drawableNormal.orientation = orientations[gradientOrientation]
            } else {
                drawableNormal.setColor(solidNormalColor)
            }
        }


        // pressed state
        val drawablePressed = GradientDrawable()
        drawablePressed.shape = shape
        drawablePressed.cornerRadius = cornersRadius
        if (cornersRadius == 0f) {
            drawablePressed.cornerRadii = floatArrayOf(
                    cornersTopLeft, cornersTopLeft,
                    cornersTopRight, cornersTopRight,
                    cornersBottomRight, cornersBottomRight,
                    cornersBottomLeft, cornersBottomLeft)
        }
        drawablePressed.setStroke(strokeWidth.toInt(), strokeColor)
        drawablePressed.setColor(solidPressedColor)

        // 设置背景选择器
        val stateListDrawable = StateListDrawable()
        if (solidPressedColor != defaultColor) {
            stateListDrawable.addState(intArrayOf(android.R.attr.state_pressed), drawablePressed)
        }
        var gradientPressdColors: IntArray
        if (gradientPressedStartColor != defaultColor && gradientPressedEndColor != defaultColor) {
            gradientPressdColors = intArrayOf(gradientPressedStartColor, gradientPressedEndColor)
            if (gradientPressedCenterColor != defaultColor) {
                gradientPressdColors = intArrayOf(gradientPressedStartColor, gradientPressedCenterColor, gradientPressedEndColor)
            }
            drawablePressed.colors = gradientPressdColors
            drawablePressed.orientation = orientations[gradientOrientation]
            stateListDrawable.addState(intArrayOf(android.R.attr.state_pressed), drawablePressed)
        }
        if (isEnabled()) {
            setAlpha(1.0f)
        } else {
            setAlpha(0.7f)
        }
        stateListDrawable.addState(intArrayOf(), drawableNormal)
        setBackground(stateListDrawable)
    }
}

相关文章

网友评论

      本文标题:Android 自定义Button

      本文链接:https://www.haomeiwen.com/subject/xbuaprtx.html