Android textView drawableXX图片大小
来源:互联网 发布:阿里云代金券转让 编辑:程序博客网 时间:2024/06/07 20:00
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,直接在布局文件使用即可,在可以设置图片大小的前提下,进行了扩展,使其可以使用在更多的场景
例如:
以上场景就可以使用在多行文本的表现形式,然后使用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 0
- Android textView drawableXX图片大小
- Android 调整 TextView,CheckBox,RadioButton中图片大小
- Android 自定义view-如何设置TextView drawableLeft 图片大小?
- textview设置图片大小
- textView 设置图片大小
- textview动态设置 DrableLeft 以及改变图片大小
- drawleft,textview、EditText中图片大小设置
- 自定义TextView实现设置drawable图片大小
- 控制TextView左侧图片大小的方法
- iOS textview加载html并改变图片大小
- android动态调整图片大小
- Android缩放图片大小小结
- android BitMap 设置图片大小
- Android 设置图片大小
- android图片大小计算方式
- android图片大小按比例缩放
- Android:图片大小缩放drawable
- Android图片大小处理
- spring mvc Restful架构
- LINK1123:failure during conversion to COFF:file invalid or corrupt
- InputService
- ubuntu 命令
- Extjs MVC模式下传递参数 动态插入Item
- Android textView drawableXX图片大小
- hadoop 端口
- Linux网络编程面试--滑动窗口的实现机制
- Disable Keyboard Shortcuts and Combination Keys with C# (3): Disable Ctrl + Alt + Del
- 在UiAutomator2.0测试脚本中操作联系人数据
- js 双引号中引入变量
- 我的求职之路(一)
- Linux 克隆虚拟机引起的“Device eth0 does not seem to be present, delaying initialization”
- ExtJS之Ext.form.field.Number数字输入框