From 15cf4fbe63ccda8b04b3224436af63470f8e88fd Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Fri, 15 Dec 2017 01:45:06 +0800 Subject: [PATCH] improved TextView compound drawables size --- .../twidere/view/DrawableTintTextView.kt | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/view/DrawableTintTextView.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/view/DrawableTintTextView.kt index cb9ef2bf9..e2b6cac2f 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/view/DrawableTintTextView.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/view/DrawableTintTextView.kt @@ -21,7 +21,6 @@ package org.mariotaku.twidere.view import android.content.Context import android.content.res.ColorStateList -import android.graphics.drawable.Drawable import android.os.Build import android.support.v4.graphics.drawable.DrawableCompat import android.util.AttributeSet @@ -48,25 +47,29 @@ class DrawableTintTextView( val a = context.obtainStyledAttributes(attrs, R.styleable.DrawableTintTextView) if (a.hasValue(R.styleable.DrawableTintTextView_drawableTint)) { compoundDrawableTintListCompat = a.getColorStateList(R.styleable.DrawableTintTextView_drawableTint) + iconWidth = a.getDimensionPixelSize(R.styleable.DrawableTintTextView_iabIconWidth, 0) + iconHeight = a.getDimensionPixelSize(R.styleable.DrawableTintTextView_iabIconHeight, 0) } a.recycle() + updateDrawableCompat() } - override fun setCompoundDrawablesRelative(start: Drawable?, top: Drawable?, end: Drawable?, bottom: Drawable?) { - super.setCompoundDrawablesRelative(start, top, end, bottom) - updateDrawableTintCompat() + override fun drawableStateChanged() { + super.drawableStateChanged() + updateDrawableCompat() } - override fun setCompoundDrawables(left: Drawable?, top: Drawable?, right: Drawable?, bottom: Drawable?) { - super.setCompoundDrawables(left, top, right, bottom) - updateDrawableTintCompat() - } - - private fun updateDrawableTintCompat() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) return + private fun updateDrawableCompat() { compoundDrawables.forEach { drawable -> if (drawable == null) return@forEach - DrawableCompat.setTintList(drawable, compoundDrawableTintListCompat) + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { + DrawableCompat.setTintList(drawable, compoundDrawableTintListCompat) + } + if (iconWidth > 0 && iconHeight > 0) { + val top = (drawable.intrinsicHeight - iconHeight) / 2 + val left = (drawable.intrinsicWidth - iconWidth) / 2 + drawable.setBounds(left, top, left + iconWidth, top + iconHeight) + } } } }