Android textView drawableXX图片大小

TextView自带的drawableLeft属性竟然不能设置图片大小,简直不能忍,啥也不说了,直接上代码

    <!--drawableTextView-->
    <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" />
        <attr name="addTail" format="boolean"/>
    </declare-styleable>

继承自TextView,直接在布局文件使用即可,在可以设置图片大小的前提下,进行了扩展,使其可以使用在更多的场景
例如:
一个textview表现更多的效果

以上场景就可以使用在多行文本的表现形式,然后使用spannableString 或者spannableStringBuilder 实现更多更复杂的表现形式

/**
 * Drawable 监听回调接口
 *
 * Created by yinw on 2016-12-19.
 */

public class DrawableListener {


    public interface DrawableRightListener{

        void drawableRightListener(View view);

    }

    public interface DrawableLeftListener{

        void drawableLeftListener(View view);

    }


    public interface DrawableTopListener{

        void drawableTopListener(View view);

    }


    public interface DrawableBottomListener{

        void drawableBottomListener(View view);

    }



}
/**
 * Created by yinw on 2016-12-07.
 */

public class DrawableTextView extends TextView {

    private int leftDrawableWidth, leftDrawableHeight, rightDrawableWidth, rightDrawableHeight,
            topDrawableWidth, topDrawableHeight, bottomDrawableWidth, bottomDrawableHeight;


    private int leftWidth, rightWidth;//左右图片宽度

    private boolean addTail = false;//是否对换行符的长字符串进行...替换


    private final int DRAWABLE_LEFT = 0;
    private final int DRAWABLE_TOP = 1;
    private final int DRAWABLE_RIGHT = 2;
    private final int DRAWABLE_BOTTOM = 3;

    private DrawableListener.DrawableRightListener drawableRightListener;

    private DrawableListener.DrawableLeftListener drawableLeftListener;

    private DrawableListener.DrawableTopListener drawableTopListener;

    private DrawableListener.DrawableBottomListener drawableBottomListener;

    public DrawableTextView(Context context) {
        this(context, null);
    }

    public DrawableTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

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

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DrawableTextView, defStyleAttr, 0);

        leftDrawableHeight = typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_leftDrawableHeight,
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, -1, getResources().getDisplayMetrics()));
        leftDrawableWidth = typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_leftDrawableWidth,
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, -1, getResources().getDisplayMetrics()));

        rightDrawableHeight = typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_rightDrawableHeight,
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, -1, getResources().getDisplayMetrics()));
        rightDrawableWidth = typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_rightDrawableWidth,
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, -1, getResources().getDisplayMetrics()));

        topDrawableHeight = typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_topDrawableHeight,
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, -1, getResources().getDisplayMetrics()));
        topDrawableWidth = typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_topDrawableWidth,
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, -1, getResources().getDisplayMetrics()));

        bottomDrawableHeight = typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_bottomDrawableHeight,
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, -1, getResources().getDisplayMetrics()));
        bottomDrawableWidth = typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_bottomDrawableWidth,
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, -1, getResources().getDisplayMetrics()));

        addTail = typedArray.getBoolean(R.styleable.DrawableTextView_addTail, false);

        typedArray.recycle();

        Drawable[] drawables = getCompoundDrawables();

        for (int i = 0; i < drawables.length; i++) {

            setDrawableSize(drawables[i], i);

        }

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

    }


    //设置drawableRight 图片的点击监听
    public void setDrawableRightListener(DrawableListener.DrawableRightListener drawableRightListener) {
        this.drawableRightListener = drawableRightListener;
    }


    public void setDrawableLeftListener(DrawableListener.DrawableLeftListener drawableLeftListener) {
        this.drawableLeftListener = drawableLeftListener;
    }

    public void setDrawableTopListener(DrawableListener.DrawableTopListener drawableTopListener) {
        this.drawableTopListener = drawableTopListener;
    }

    public void setDrawableBottomListener(DrawableListener.DrawableBottomListener drawableBottomListener) {
        this.drawableBottomListener = drawableBottomListener;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {

            case MotionEvent.ACTION_UP:


                if (drawableRightListener != null) {

                    Drawable drawableRight = getCompoundDrawables()[DRAWABLE_RIGHT];
                    if (drawableRight != null && event.getRawX() >= (getRight() - drawableRight.getBounds().width())
                            && event.getRawX() < getRight()) {

                        drawableRightListener.drawableRightListener(this);

                        return true;

                    }

                }


                if (drawableLeftListener != null) {

                    Drawable drawableLeft = getCompoundDrawables()[DRAWABLE_LEFT];
                    if (drawableLeft != null && event.getRawX() <= (getLeft() + drawableLeft.getBounds().width())
                            && event.getRawX() > getLeft()) {

                        drawableLeftListener.drawableLeftListener(this);

                        return true;

                    }


                }


                if (drawableTopListener != null) {

                    Drawable drawableTop = getCompoundDrawables()[DRAWABLE_TOP];
                    if (drawableTop != null && event.getRawY() <= (getTop() + drawableTop.getBounds().height())
                            && event.getRawY() > getTop()) {

                        drawableTopListener.drawableTopListener(this);

                        return true;

                    }


                }

                if (drawableBottomListener != null) {

                    Drawable drawableBottom = getCompoundDrawables()[DRAWABLE_BOTTOM];
                    if (drawableBottom != null && event.getRawY() >= (getBottom() - drawableBottom.getBounds().height())
                            && event.getRawY() < getBottom()) {

                        drawableBottomListener.drawableBottomListener(this);

                        return true;

                    }


                }


                break;


        }


        return super.onTouchEvent(event);
    }

    //设置图片的高度和宽度
    private void setDrawableSize(Drawable drawable, int index) {

        if (drawable == null) {

            return;
        }
        //左上右下


        int width = 0, height = 0;

        switch (index) {

            case DRAWABLE_LEFT:

                width = leftDrawableWidth;
                height = leftDrawableHeight;

                break;

            case DRAWABLE_TOP:

                width = topDrawableWidth;
                height = topDrawableHeight;
                break;

            case DRAWABLE_RIGHT:

                width = rightDrawableWidth;
                height = rightDrawableHeight;

                break;

            case DRAWABLE_BOTTOM:

                width = bottomDrawableWidth;
                height = bottomDrawableHeight;

                break;


        }

        //如果没有设置图片的高度和宽度具使用默认的图片高度和宽度
        if (width < 0) {

            width = drawable.getIntrinsicWidth();

        }

        if (height < 0) {

            height = drawable.getIntrinsicHeight();
        }

        if (index == 0) {

            leftWidth = width;

        } else if (index == 2) {

            rightWidth = width;
        }

        drawable.setBounds(0, 0, width, height);


    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        //是否对包含有换行符的文字,以换行符分割的句子,超过textView最大宽度的时候以“...”结尾
        if (addTail) {
            String text = getText().toString();

            float textWidth = getWidth() - getPaddingRight() - getPaddingLeft();

            if (leftWidth != 0) {

                textWidth = textWidth - leftWidth - getCompoundDrawablePadding();

            }

            if (rightWidth != 0) {

                textWidth = textWidth - rightWidth - getCompoundDrawablePadding();
            }
            setText(changeText(text, textWidth));
        }

    }


    /**
     * 以换行符\n来分离文本,每行超过最大长度的文本以...来替换 可以少写很多的textView
     *
     * @param text      文本内容
     * @param textWidth 文本最大长度
     * @return
     */
    private String changeText(String text, float textWidth) {

        String[] contents = text.split("\\n");
        float contentWidth;
        String content;
        for (int j = 0; j < contents.length; j++) {

            content = contents[j];
            contentWidth = this.getPaint().measureText(content);

            if (contentWidth > textWidth) {


                String newContent;
                float newContentWidth;
                for (int i = content.length(); i >= 0; i--) {

                    newContent = content.substring(0, i);

                    newContentWidth = this.getPaint().measureText(newContent + "...");


                    if (newContentWidth <= textWidth) {

                        contents[j] = newContent.concat("...");

                        break;

                    }

                }

            }

        }

        StringBuilder stringBuilder = new StringBuilder();
        for (int k=0;k<contents.length;k++) {


            if(k<contents.length-1) {

                stringBuilder.append(contents[k] + "\n");

            }else{

                stringBuilder.append(contents[k]);
            }


        }

        return stringBuilder.toString();

    }


}

程序员内功修炼手册 不定期分享程序员基础知识,大前端知识!想跟博主一块成长的快快关注吧!

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值