diff --git a/README.md b/README.md
index aa1faa9..e069a4c 100644
--- a/README.md
+++ b/README.md
@@ -1,16 +1,16 @@
-####先看大众点评的购买须知
+#### 先看大众点评的购买须知

-######如上图,需求在每条提示语句前加一个小圆点,我刚看到需求就想到用 android:drawableLeft 来做,可做完发现:当TextView内容为单行的时候是没有问题的,多行的时候,添加的这个drawableLeft就随着TextView高度而垂直居中了。
-######一般解决办法嵌套布局解决,将(Textview)文字 与(ImageView)小圆点分离,这肯定是不符合布局优化。
-######前面写过一篇文章[Android 优化布局(解决TextView drawableLeft/top/right布局中大小不可控的方法)](http://www.jianshu.com/p/d3027acf475a)主要针对TextView drawable属性的大小进行了控制,这次在原来的基础上通过设置drawable. setBounds(int left, int top, int right, int bottom)位置进行了控制达到下面的效果:
+###### 如上图,需求在每条提示语句前加一个小圆点,我刚看到需求就想到用 android:drawableLeft 来做,可做完发现:当TextView内容为单行的时候是没有问题的,多行的时候,添加的这个drawableLeft就随着TextView高度而垂直居中了。
+###### 一般解决办法嵌套布局解决,将(Textview)文字 与(ImageView)小圆点分离,这肯定是不符合布局优化。
+###### 前面写过一篇文章[Android 优化布局(解决TextView drawableLeft/top/right布局中大小不可控的方法)](http://www.jianshu.com/p/d3027acf475a)主要针对TextView drawable属性的大小进行了控制,这次在原来的基础上通过设置drawable. setBounds(int left, int top, int right, int bottom)位置进行了控制达到下面的效果:

-######也可以达到图标大小可控的效果
+###### 也可以达到图标大小可控的效果

-#####下面看看使用方法
-######step1:Add it in your root build.gradle at the end of repositories:
+##### 下面看看使用方法
+###### step1:Add it in your root build.gradle at the end of repositories:
````
allprojects {
repositories {
@@ -19,13 +19,13 @@ allprojects {
}
}
````
-######step2:Add the dependency
+###### step2:Add the dependency
````
dependencies {
compile 'com.github.ithedan:TextViewDrawable:v1.0'
}
````
-#####图标与第一行文字对齐 xml (app:isAliganCenter="false"默认是true)
+##### 图标与第一行文字对齐 xml (app:isAliganCenter="false"默认是true)
````
````
-#####图标与第一行文字对齐 activity
+##### 图标与第一行文字对齐 activity
````
TextViewDrawable tv4=(TextViewDrawable)findViewById(R.id.text4);
tv4.setText("提供免费WIFI\n停车位收费标准:详情咨询商家");
````
-#####图标大小 xml
+##### 图标大小 xml
````
````
-####如有什么问题,敬请提出,十分感谢!希望越来越好,谢谢!如果喜欢,还请点击start,喜欢支持一下了,谢谢O(∩_∩)O~
+#### 如有什么问题,敬请提出,十分感谢!希望越来越好,谢谢!如果喜欢,还请点击start,喜欢支持一下了,谢谢O(∩_∩)O~
diff --git a/textdrawablelibrary/src/main/java/com/hedan/textdrawablelibrary/TextViewDrawable.java b/textdrawablelibrary/src/main/java/com/hedan/textdrawablelibrary/TextViewDrawable.java
index e241e75..3dda0b2 100644
--- a/textdrawablelibrary/src/main/java/com/hedan/textdrawablelibrary/TextViewDrawable.java
+++ b/textdrawablelibrary/src/main/java/com/hedan/textdrawablelibrary/TextViewDrawable.java
@@ -16,8 +16,8 @@ public class TextViewDrawable extends AppCompatTextView {
private int drawableLeftWidth, drawableTopWidth, drawableRightWidth, drawableBottomWidth;
private int drawableLeftHeight, drawableTopHeight, drawableRightHeight, drawableBottomHeight;
- private boolean isAliganCenter=true;
- private boolean isDwMath_content=false;
+ private boolean isAliganCenter = true;
+ private boolean isDwMath_content = false;
private int mWidth, mHeight;
public TextViewDrawable(Context context) {
@@ -51,50 +51,83 @@ private void initView(Context context, AttributeSet attrs, int defStyleAttr) {
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
-
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mWidth = w;
mHeight = h;
+ //
Drawable[] drawables = getCompoundDrawables();
Drawable drawableLeft = drawables[0];
Drawable drawableTop = drawables[1];
Drawable drawableRight = drawables[2];
Drawable drawableBottom = drawables[3];
- if (drawableLeft != null) {
- setDrawable(drawableLeft, 0, drawableLeftWidth, drawableLeftHeight);
- }
- if (drawableTop != null) {
- setDrawable(drawableTop, 1, drawableTopWidth, drawableTopHeight);
- }
- if (drawableRight != null) {
- setDrawable(drawableRight, 2, drawableRightWidth, drawableRightHeight);
- }
- if (drawableBottom != null) {
- setDrawable(drawableBottom, 3, drawableBottomWidth, drawableBottomHeight);
+ this.setCompoundDrawables(drawableLeft, drawableTop, drawableRight, drawableBottom);
+ }
+
+ @Override
+ public void setCompoundDrawables(@Nullable Drawable left, @Nullable Drawable top, @Nullable Drawable right, @Nullable Drawable bottom) {
+ //
+ if (mWidth != 0 && mHeight != 0) {
+ if (left != null) {
+ setDrawable(left, 0, drawableLeftWidth, drawableLeftHeight);
+ }
+ if (top != null) {
+ setDrawable(top, 1, drawableTopWidth, drawableTopHeight);
+ }
+ if (right != null) {
+ setDrawable(right, 2, drawableRightWidth, drawableRightHeight);
+ }
+ if (bottom != null) {
+ setDrawable(bottom, 3, drawableBottomWidth, drawableBottomHeight);
+ }
}
- this.setCompoundDrawables(drawableLeft,drawableTop,drawableRight,drawableBottom);
+ //
+ super.setCompoundDrawables(left, top, right, bottom);
}
private void setDrawable(Drawable drawable, int tag, int drawableWidth, int drawableHeight) {
+ // 首先要有数据
+ if (getLineCount() == 0 || getLineCount() == 1) {
+ return;
+ }
//获取图片实际长宽
- int width = drawableWidth == 0 ? drawable.getIntrinsicWidth() : drawableWidth;
- int height = drawableHeight == 0 ? drawable.getIntrinsicHeight() : drawableHeight;
+ int mDrawableWidth = drawableWidth == 0 ? drawable.getIntrinsicWidth() : drawableWidth;
+ int mDrawableHeight = drawableHeight == 0 ? drawable.getIntrinsicHeight() : drawableHeight;
+ // view 的高度
+ int mViewHeight = getHeight() - getPaddingBottom() - getPaddingTop();
+ // 行数、行高、行数*行高
+ int mLineCount = getLineCount();
+ int mLineHeight = getLineHeight();
+ int mTextHeight = mLineCount * mLineHeight;
+ // 每行的offset
+ int mLineOffset = 0;
+ if (mViewHeight > mTextHeight) {
+ mLineOffset = (mViewHeight - mTextHeight) / mLineCount;
+ }
+ // 最终行高
+ int mFinalLineHeight = mLineHeight + mLineOffset;
+ //
int left = 0, top = 0, right = 0, bottom = 0;
switch (tag) {
case 0:
case 2:
+ // 中心点向上移动的 行 (mLineCount - 1) / 2.0f
+ // 1行 (1 - 1) / 2.0f) = 0 不移动
+ // 2行 (2 - 1) / 2.0f) = 0.5 移动0.5行
+ // 3行 (3 - 1) / 2.0f) = 1 移动1行
+ int moveTop = -(int) (((mLineCount - 1) / 2.0f) * mFinalLineHeight);
+ //
left = 0;
- top = isAliganCenter ? 0 : -getLineCount() * getLineHeight() / 2 + getLineHeight() / 2;
- right = width;
- bottom = top + height;
+ top = isAliganCenter ? 0 : moveTop;
+ right = mDrawableWidth;
+ bottom = top + mDrawableHeight;
break;
case 1:
- left =isAliganCenter ? 0: -mWidth/2+width/2;
+ left = isAliganCenter ? 0 : -mWidth / 2 + mDrawableWidth / 2;
top = 0;
- right = left+width;
- bottom =top+height;
+ right = left + mDrawableWidth;
+ bottom = top + mDrawableHeight;
break;
}
drawable.setBounds(left, top, right, bottom);