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

                                                                                    

继承自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

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

更多相关文章

  1. android 拍照和从相册选择组件
  2. Android之TextView设置String文本颜色
  3. android 下载图片及时显示
  4. 第一个Android(安卓)程序的源代码: TxtReader文本阅读器
  5. Android相册及小小秒表震动(17)
  6. Android入门:查看服务器图片应用
  7. 用android来实现图片的绘制以及旋转缩放案例分析
  8. android学习笔记(三)基础UI组件1——按钮,文本框,CheckBox,Radiobutto
  9. Android(安卓)8.1 关机充电动画(二)Uboot模式

随机推荐

  1. android通过chmod命令实现文件权限修改
  2. android:shape的使用
  3. ADT下载地址(含各版本),最新ADT-23.0.6
  4. Android适配器及其控件
  5. android library projects cannot be lau
  6. Android的网络与通信
  7. Android中的lcd_density设置
  8. Android(安卓)display架构分析-SW架构分
  9. Android(安卓)应用软件开发(六)窗口布局
  10. android Tabhost部件(详细)