TextWithImageDrawable
Introduction: 一个可以同时包含图片和文字的 drawable,使用方式类似 TextView 和它的 drawableLeft 之类的方法,功能比较全面,可以代替 TextView 或是解决一些非得需要在 ImageView 中同时显示文字和图片的问题
Tags:
An android drawable object which contains text and image
可以同时包含文字和图像的 Drawable,也可以单独只包含文字或是图像
使用步骤
在 app 的构建文件 build.gradle 中的 dependencies 中加入下面的代码
compile 'wu.seal:textwithimagedrawable:1.0.4'
然后就可以直接在代码中使用就可以了
Api 的简单介绍
/**
* 实例化一个同时包含文字和图片的 drawable
*/
TextWithImageDrawable textWithImageDrawable = new TextWithImageDrawable(thisActivity);
/**
* 设置 drawable 里的文字
*/
textWithImageDrawable.setText(text);
/**
* 设置 drawable 里的图像资源
*/
textWithImageDrawable.setImageRes(leftMenuIconResId);
/**
* 设置 drawable 里的图像资源
*/
textWithImageDrawable.setImageBitmap(mBitmap);
/**
* 设置 drawable 里的 drawable
*/
textWithImageDrawable.setDrawable(mDrawable);
/**
* 设置 drawable 中文字的大小,注意此处的单位是 sp
*/
textWithImageDrawable.setTextSize(16);
/**
* 设置文字的颜色
*/
textWithImageDrawable.setTextColor(getResources().getColor(R.color.text_color_white));
/**
* 设置文字和图像之间的间隔,单位是 px
*/
textWithImageDrawable.setImagePadding(DensityUtils.dip2px(5));
/**
* 设置此 drawable 的左边填充大小,单位 px
*/
textWithImageDrawable.setPaddingLeft(DensityUtils.dip2px(8));
/**
* 设置此 drawable 上方填充大小,单位 px
*/
textWithImageDrawable.setPaddingTop(DensityUtils.dip2px(6));
/**
* 设置此 drawable 的最大文字限制长度
*/
textWithImageDrawable.setMaxTextLength(3);
/**
* 设置图像和文字的相对位置,此处设置的是图像在文字右边显示
*/
textWithImageDrawable.setImagePosition(TextWithImageDrawable.Position.RIGHT);
使用示例:
ImageView left, right, top, bottom;
String mText = "text";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* 图像资源显示在文字左边的显示效果
*/
left = (ImageView) findViewById(R.id.iv_left);
/**
* 图像资源显示在文字右边的显示效果
*/
right = (ImageView) findViewById(R.id.iv_right);
/**
* 图像资源显示在文字上边的显示效果
*/
top = (ImageView) findViewById(R.id.iv_top);
/**
* 图像资源显示在文字下边的显示效果
*/
bottom = (ImageView) findViewById(R.id.iv_bottom);
/**
* 图像和文字之间的距离
*/
final int drawablePadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics());
TextWithImageDrawable drawableLeft = new TextWithImageDrawable(this);
initDrawable(drawablePadding, drawableLeft, mText, TextWithImageDrawable.Position.LEFT);
left.setImageDrawable(drawableLeft);
TextWithImageDrawable drawableRight = new TextWithImageDrawable(this);
initDrawable(drawablePadding, drawableRight, mText, TextWithImageDrawable.Position.RIGHT);
right.setImageDrawable(drawableRight);
TextWithImageDrawable drawableTop = new TextWithImageDrawable(this);
initDrawable(drawablePadding, drawableTop, mText, TextWithImageDrawable.Position.TOP);
top.setImageDrawable(drawableTop);
TextWithImageDrawable drawableBottom = new TextWithImageDrawable(this);
initDrawable(drawablePadding, drawableBottom, mText, TextWithImageDrawable.Position.BOTTOM);
bottom.setImageDrawable(drawableBottom);
}
private void initDrawable(int drawablePadding, TextWithImageDrawable drawable, String mText, TextWithImageDrawable.Position position) {
drawable.setText(mText);
drawable.setImagePosition(position);
drawable.setImagePadding(drawablePadding);
drawable.setImageRes(R.mipmap.ic_launcher);
}
BaseCombinedDrawable
An android drawable object which contains two drawables
一个组合 drawable,能对两个 drawable 进行拼凑组合成一个新的 drawable,两个 drawable 的位置可以灵活组合,基本能满足所有的 drawable 的定制,各种图文混排,你懂的
Api 介绍
/**
* 设置 drawable two 左上角相对于 drawable one 左上角 的相对偏移位置
* 偏移以 drawable one 的左上角为起始点
* drawable one 会优先放在最前面进行绘制(如果两个 drawable 的相对偏移值为 0 则效果如同 FrameLayout)
*
* @param relatedX x 轴的相对偏移
* @param relatedY y 轴的相对偏移值
*/
public void setRelatedPosition(int relatedX, int relatedY)
/**
* 设置组合后的新的 drawable 的四个 Padding 值
*
* @param paddingLeft 左边填充距离
* @param paddingTop 上边填充距离
* @param paddingRight 右边填充距离
* @param paddingBottom 下边填充距离
*/
public void setPadding(int paddingLeft, int paddingTop, int paddingRight, int paddingBottom)
代码示例
BaseCombinedDrawable baseCombinedDrawable = new BaseCombinedDrawable(drawableLeft, drawableRight);
baseCombinedDrawable.setRelatedPosition(drawableLeft.getIntrinsicWidth() + drawablePadding, 0);
combine.setImageDrawable(baseCombinedDrawable);