感覺,注釋比源碼精彩!直接拿到項目里試試吧,方便、高效、減少布局層級!
1.源碼以及注釋
/**
*
* TextView的setCompoundDrawables()可以在其四周設置圖片,但是有個眾所周知的問題,即無法設置drawable大小。
* 這就導致在實際的使用中有很大的局限性,必須用代碼去控制,就略顯麻煩了。
*
* 這個自定義控件雖然簡單,也非常不起眼,但是用處還真不少:
* 1.解決了主要矛盾,無法在布局里設置TextView圖片大小問題,使用更加簡單。
* 除了設置圖片大小,其它的TextView可以的事情這個一樣也都可以
*
* 2.圖片加文字的簡單組合非常常見,原本為了適配圖片大小不得不用一個xxxLayout+ImageView+TextView
* 才能搞定的事,現(xiàn)在用一個控件即可搞定。
* 在方便、高效使用的同時,也有效的減少了布局層。===》千萬不要瞧不上這點蒼蠅肉
*
* 原理真的簡單到只有兩句話:
* 1.通過Drawable的setBound()設置顯示區(qū)域,也就是圖片大小
* 2.通過TextView的setCompoundDrawables()設置要顯示的圖片
*
* 高能預警:
* 在網上看到有一個版本,在控件的onMeasure()設置Drawable.setBound(), 在onDraw()里設置 setCompoundDrawables()。
* 看setCompoundDrawables()源碼可以知道,這個方法最終會調用invalide()和requestLayout(),會導致嚴重的后果就是,
* onMeasure()和onDraw()會無限循環(huán)的互調下去,有點浪費
*/
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"/>