美文网首页程序员
可以自由设置drawable大小的TextView

可以自由设置drawable大小的TextView

作者: csdpz | 来源:发表于2017-10-27 00:46 被阅读0次

感觉,注释比源码精彩!直接拿到项目里试试吧,方便、高效、减少布局层级!

1.源码以及注释

/**
 *
 * TextView的setCompoundDrawables()可以在其四周设置图片,但是有个众所周知的问题,即无法设置drawable大小。
 * 这就导致在实际的使用中有很大的局限性,必须用代码去控制,就略显麻烦了。
 *
 * 这个自定义控件虽然简单,也非常不起眼,但是用处还真不少:
 * 1.解决了主要矛盾,无法在布局里设置TextView图片大小问题,使用更加简单。
 *   除了设置图片大小,其它的TextView可以的事情这个一样也都可以
 *
 * 2.图片加文字的简单组合非常常见,原本为了适配图片大小不得不用一个xxxLayout+ImageView+TextView
 *   才能搞定的事,现在用一个控件即可搞定。
 *   在方便、高效使用的同时,也有效的减少了布局层。===》千万不要瞧不上这点苍蝇肉
 *
 * 原理真的简单到只有两句话:
 * 1.通过Drawable的setBound()设置显示区域,也就是图片大小
 * 2.通过TextView的setCompoundDrawables()设置要显示的图片
 *
 * 高能预警:
 * 在网上看到有一个版本,在控件的onMeasure()设置Drawable.setBound(), 在onDraw()里设置    setCompoundDrawables()。
 * 看setCompoundDrawables()源码可以知道,这个方法最终会调用invalide()和requestLayout(),会导致严重的后果就是,
 * onMeasure()和onDraw()会无限循环的互调下去,有点浪费
 */

public class DrawableTextView extends TextView{

    public DrawableTextView(Context context) {
        super(context);
    }

    public DrawableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public DrawableTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.DrawableTextView);
        int width = ta.getDimensionPixelOffset(R.styleable.DrawableTextView_drawable_width, -1);
        int height = ta.getDimensionPixelOffset(R.styleable.DrawableTextView_drawable_height, -1);

        SizeWrap sizeWrap = new SizeWrap();
        Drawable leftDrawable = ta.getDrawable(R.styleable.DrawableTextView_left_drawable);
        if (leftDrawable != null) {
            int lwidth = ta.getDimensionPixelOffset(R.styleable.DrawableTextView_leftdrawable_width, -1);
            int lheight = ta.getDimensionPixelOffset(R.styleable.DrawableTextView_lefttdrawable_height, -1);
            if (sizeWrap.checkWidthAndHeight(width, height, lwidth, lheight)) {
                leftDrawable.setBounds(0, 0, sizeWrap.width, sizeWrap.height);
            } else {
                throw new IllegalArgumentException("error left drawable size setting");
            }
        }

        Drawable rightDrawable = ta.getDrawable(R.styleable.DrawableTextView_right_drawable);
        if (rightDrawable != null) {
            int rwidth = ta.getDimensionPixelOffset(R.styleable.DrawableTextView_rightdrawable_width, -1);
            int rheight = ta.getDimensionPixelOffset(R.styleable.DrawableTextView_rightdrawable_height, -1);
            if (sizeWrap.checkWidthAndHeight(width,height, rwidth, rheight)) {
                rightDrawable.setBounds(0, 0, sizeWrap.width, sizeWrap.height);
            } else {
                throw new IllegalArgumentException("error right drawable size setting");
            }
        }

        Drawable topDrawable = ta.getDrawable(R.styleable.DrawableTextView_top_drawable);
        if (topDrawable != null) {
            int twidth = ta.getDimensionPixelOffset(R.styleable.DrawableTextView_topdrawable_width, -1);
            int theight = ta.getDimensionPixelOffset(R.styleable.DrawableTextView_topdrawable_height, -1);
            if (sizeWrap.checkWidthAndHeight(width,height, twidth, theight)) {
                topDrawable.setBounds(0, 0, sizeWrap.width, sizeWrap.height);
            } else {
                throw new IllegalArgumentException("error top drawable size setting");
            }
        }

        Drawable bottomDrawable = ta.getDrawable(R.styleable.DrawableTextView_bottom_drawable);
        if (bottomDrawable != null) {
            int bwidth = ta.getDimensionPixelOffset(R.styleable.DrawableTextView_bottomdrawable_width, -1);
            int bheight = ta.getDimensionPixelOffset(R.styleable.DrawableTextView_bottomdrawable_height, -1);
            if (sizeWrap.checkWidthAndHeight(width, height, bwidth, bheight)) {
                bottomDrawable.setBounds(0, 0, sizeWrap.width, sizeWrap.height);
            } else {
                throw new IllegalArgumentException("error bottom drawable size setting");
            }
        }

        this.setCompoundDrawables(leftDrawable, topDrawable, rightDrawable, bottomDrawable);
        ta.recycle();
        ta = null;
    }

    /**
     *
     */
    public static class SizeWrap {
        int width;
        int height;

        public boolean checkWidthAndHeight(int globalWidth, int globalHeight, int localWidth, int localHeight) {
            width = 0;
            height = 0;

            //局部的大小设置均正常的情况
            if (localWidth > 0 && localHeight > 0) {
                width = localWidth;
                height = localHeight;
                return true;
            }

            //局部大小没设置时,看全局的大小是否正确设置
            if (localWidth == -1 && localHeight == -1) {
                if (globalWidth > 0 && globalHeight > 0) {
                    width = globalWidth;
                    height = globalHeight;
                    return true;
                }
            }

            return false;
        }
    }
}

2. attrs.xml中的自定义属性

<declare-styleable name="DrawableTextView">
        <attr name="left_drawable" format="reference"/>
        <attr name="right_drawable" format="reference"/>
        <attr name="top_drawable" format="reference"/>
        <attr name="bottom_drawable" format="reference"/>

        <attr name="drawable_width" format="dimension"/>
        <attr name="drawable_height" format="dimension"/>

        <attr name="leftdrawable_width" format="dimension"/>
        <attr name="lefttdrawable_height" format="dimension"/>

        <attr name="rightdrawable_width" format="dimension"/>
        <attr name="rightdrawable_height" format="dimension"/>

        <attr name="topdrawable_width" format="dimension"/>
        <attr name="topdrawable_height" format="dimension"/>

        <attr name="bottomdrawable_width" format="dimension"/>
        <attr name="bottomdrawable_height" format="dimension"/>
    </declare-styleable>

3. 使用示例

在布局文件根布局下定义好app命名空间,
xmlns:app="http://schemas.android.com/apk/res-auto"

    <com.example.xxx.myapplication.view.DrawableTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:text="首页"
        android:background="@drawable/bg_select"
        app:left_drawable="@drawable/image"
        app:right_drawable="@drawable/dca99cdd9a9270e77ae6bb62bd80ca"
        app:bottom_drawable="@drawable/a201609025013"
        android:gravity="center"
        app:drawable_width="50px"
        app:drawable_height="50px"
        app:rightdrawable_width="20px"
        app:rightdrawable_height="20px"
        android:paddingRight="10dp"
        android:paddingLeft="10dp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:drawablePadding="5dp"/>

相关文章

网友评论

    本文标题:可以自由设置drawable大小的TextView

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