扩展TextView 可设置Drawable大小和事件处理

系统TextView是不能直接在xml中设置drawable大小,及drawable事件处理!!!
对此,需要自己完成扩展

1、添加自定义属性

在res -> values -> attrs 下新增自定义属性

<declare-styleable name="DrawableTextView">
        <attr name="leftDrawableWidth" format="dimension" />
        <attr name="leftDrawableHeight" format="dimension" />
        <attr name="rightDrawableWidth" format="dimension" />
        <attr name="rightDrawableHeight" format="dimension" />
        <attr name="topDrawableWidth" format="dimension" />
        <attr name="topDrawableHeight" format="dimension" />
        <attr name="bottomDrawableWidth" format="dimension" />
        <attr name="bottomDrawableHeight" format="dimension" />
    </declare-styleable>

2、继承TextView

1、新增drawable点击处理接口
2、设置drawable 大小
3、处理drwable(top 、left、right、bottom)事件

class DrawableTextView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : AppCompatTextView(context, attrs, defStyleAttr) {

    private var leftDrawableWidth: Int
    private var leftDrawableHeight: Int

    private var rightDrawableWidth: Int
    private var rightDrawableHeight: Int

    private var topDrawableWidth: Int
    private var topDrawableHeight: Int

    private var bottomDrawableWidth: Int
    private var bottomDrawableHeight: Int

    private var leftWidth = 0
    private var rightWidth = 0//左右图片宽度 = 0

    companion object {
        private const val DRAWABLE_LEFT = 0
        private const val DRAWABLE_TOP = 1
        private const val DRAWABLE_RIGHT = 2
        private const val DRAWABLE_BOTTOM = 3
    }

    //设置图片的高度和宽度
    private fun setDrawableSize(
        drawable: Drawable?,
        index: Int
    ) {
        if (drawable == null) {
            return
        }
        //左上右下
        var width = 0
        var height = 0
        when (index) {
            DRAWABLE_LEFT -> {
                width = leftDrawableWidth
                height = leftDrawableHeight
            }
            DRAWABLE_TOP -> {
                width = topDrawableWidth
                height = topDrawableHeight
            }
            DRAWABLE_RIGHT -> {
                width = rightDrawableWidth
                height = rightDrawableHeight
            }
            DRAWABLE_BOTTOM -> {
                width = bottomDrawableWidth
                height = bottomDrawableHeight
            }
        }

        //如果没有设置图片的高度和宽度具使用默认的图片高度和宽度
        if (width < 0) {
            width = drawable.intrinsicWidth
        }
        if (height < 0) {
            height = drawable.intrinsicHeight
        }
        if (index == 0) {
            leftWidth = width
        } else if (index == 2) {
            rightWidth = width
        }
        drawable.setBounds(0, 0, width, height)
    }

    init {
        //扩展属性
        val typedArray =
            context.obtainStyledAttributes(attrs, R.styleable.DrawableTextView, defStyleAttr, 0)
        leftDrawableHeight = typedArray.getDimensionPixelSize(
            R.styleable.DrawableTextView_leftDrawableHeight,
            TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                -1f,
                resources.displayMetrics
            ).toInt()
        )
        leftDrawableWidth = typedArray.getDimensionPixelSize(
            R.styleable.DrawableTextView_leftDrawableWidth,
            TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                -1f,
                resources.displayMetrics
            ).toInt()
        )
        rightDrawableHeight = typedArray.getDimensionPixelSize(
            R.styleable.DrawableTextView_rightDrawableHeight,
            TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                -1f,
                resources.displayMetrics
            ).toInt()
        )
        rightDrawableWidth = typedArray.getDimensionPixelSize(
            R.styleable.DrawableTextView_rightDrawableWidth,
            TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                -1f,
                resources.displayMetrics
            ).toInt()
        )
        topDrawableHeight = typedArray.getDimensionPixelSize(
            R.styleable.DrawableTextView_topDrawableHeight,
            TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                -1f,
                resources.displayMetrics
            ).toInt()
        )
        topDrawableWidth = typedArray.getDimensionPixelSize(
            R.styleable.DrawableTextView_topDrawableWidth,
            TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                -1f,
                resources.displayMetrics
            ).toInt()
        )
        bottomDrawableHeight = typedArray.getDimensionPixelSize(
            R.styleable.DrawableTextView_bottomDrawableHeight,
            TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                -1f,
                resources.displayMetrics
            ).toInt()
        )
        bottomDrawableWidth = typedArray.getDimensionPixelSize(
            R.styleable.DrawableTextView_bottomDrawableWidth,
            TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                -1f,
                resources.displayMetrics
            ).toInt()
        )

        typedArray.recycle()
        val drawables = compoundDrawables
        for (i in drawables.indices) {
            setDrawableSize(drawables[i], i)
        }

        //放置图片
        setCompoundDrawables(
            drawables[DRAWABLE_LEFT],
            drawables[DRAWABLE_TOP],
            drawables[DRAWABLE_RIGHT],
            drawables[DRAWABLE_BOTTOM]
        )
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {

        when (event?.action) {
            MotionEvent.ACTION_UP -> {
                //top
                mDrawableTopListener?.let {
                    val drawableTop = compoundDrawables[DRAWABLE_TOP]
                    if (drawableTop != null
                        && event.y <= (top + drawableTop.bounds.height())
                        && event.y > top
                    ) {
                        it.onDrawableTop(this)
                        return true
                    }
                }
                //left
                mDrawableLeftListener?.let {
                    val drawableLeft = compoundDrawables[DRAWABLE_LEFT]
                    if (drawableLeft != null
                        && event.x <= (left + drawableLeft.bounds.width())
                        && event.x > left
                    ) {
                        it.onDrawableLeft(this)
                        return true
                    }
                }

                //right
                mDrawableRightListener?.let {
                    val drawableRight = compoundDrawables[DRAWABLE_RIGHT]
                    if (drawableRight != null
                        && event.x >= (right - drawableRight.bounds.width())
                        && event.x < right
                    ) {
                        it.onDrawableRight(this)
                        return true
                    }
                }
                //bottom
                mDrawableBottomListener?.let {
                    val drawableBottom = compoundDrawables[DRAWABLE_BOTTOM]
                    if (drawableBottom != null
                        && event.y >= (bottom - drawableBottom.bounds.height())
                        && event.y < bottom
                    ) {
                        it.onDrawableBottom(this)
                        return true
                    }
                }
            }
        }
        return super.onTouchEvent(event)
    }

    private var mDrawableTopListener: DrawableTopListener? = null
    fun setDrawableTopListener(drawableTopListener: DrawableTopListener) {
        this.mDrawableTopListener = drawableTopListener
    }

    interface DrawableTopListener {
        fun onDrawableTop(view: DrawableTextView)
    }

    private var mDrawableLeftListener: DrawableLeftListener? = null
    fun setDrawableLeftListener(drawableLeftListener: DrawableLeftListener) {
        this.mDrawableLeftListener = drawableLeftListener
    }

    interface DrawableLeftListener {
        fun onDrawableLeft(view: DrawableTextView)
    }

    private var mDrawableRightListener: DrawableRightListener? = null
    fun setDrawableRightListener(drawableRightListener: DrawableRightListener) {
        this.mDrawableRightListener = drawableRightListener
    }

    interface DrawableRightListener {
        fun onDrawableRight(view: DrawableTextView)
    }

    private var mDrawableBottomListener: DrawableBottomListener? = null
    fun setDrawableBottomListener(drawableBottomListener: DrawableBottomListener) {
        this.mDrawableBottomListener = drawableBottomListener
    }

    interface DrawableBottomListener {
        fun onDrawableBottom(view: DrawableTextView)
    }
}

3、案例

添加布局

 <com.poso2o.common.widget.textview.DrawableTextView
            android:id="@+id/tvAddTextModule"
            style="@style/item_menu_edit"
            android:drawableTop="@mipmap/icon_public_edit_text"
            android:text="+ 文字"
            app:topDrawableHeight="20dp"
            app:topDrawableWidth="20dp" />

设置事件

 tvAddTextModule.setDrawableTopListener(object : DrawableTextView.DrawableTopListener {
            override fun onDrawableTop(view: DrawableTextView) {
                ToastUtils.show("top 点击")
            }
        })
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值