From ce9867645e316d88b46ee1a1fa833a601ad44c52 Mon Sep 17 00:00:00 2001 From: xiaxl Date: Thu, 21 Nov 2019 10:36:52 +0800 Subject: [PATCH 1/3] =?UTF-8?q?[bug=E4=BF=AE=E5=A4=8D]=20Android=209.0?= =?UTF-8?q?=E4=BB=A5=E4=B8=8A=E8=AE=BE=E5=A4=87=E5=85=BC=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../textdrawablelibrary/TextViewDrawable.java | 46 ++++++++++++++----- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/textdrawablelibrary/src/main/java/com/hedan/textdrawablelibrary/TextViewDrawable.java b/textdrawablelibrary/src/main/java/com/hedan/textdrawablelibrary/TextViewDrawable.java index e241e75..31afa10 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) { @@ -74,27 +74,51 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) { if (drawableBottom != null) { setDrawable(drawableBottom, 3, drawableBottomWidth, drawableBottomHeight); } - this.setCompoundDrawables(drawableLeft,drawableTop,drawableRight,drawableBottom); + this.setCompoundDrawables(drawableLeft, drawableTop, drawableRight, drawableBottom); } private void setDrawable(Drawable drawable, int tag, int drawableWidth, int drawableHeight) { + // 首先要有数据 + if (getLineCount() == 0) { + 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(); + // 行数、行高、行数*行高 + 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); From 5fff5ee4056601668dcf2f5f87e240f6f7d3c378 Mon Sep 17 00:00:00 2001 From: xiaxl Date: Thu, 21 Nov 2019 13:46:13 +0800 Subject: [PATCH 2/3] =?UTF-8?q?[=E5=8A=A8=E6=80=81=E8=AE=BE=E7=BD=AEDrawab?= =?UTF-8?q?le]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../textdrawablelibrary/TextViewDrawable.java | 39 ++++++++++++------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/textdrawablelibrary/src/main/java/com/hedan/textdrawablelibrary/TextViewDrawable.java b/textdrawablelibrary/src/main/java/com/hedan/textdrawablelibrary/TextViewDrawable.java index 31afa10..3dda0b2 100644 --- a/textdrawablelibrary/src/main/java/com/hedan/textdrawablelibrary/TextViewDrawable.java +++ b/textdrawablelibrary/src/main/java/com/hedan/textdrawablelibrary/TextViewDrawable.java @@ -51,42 +51,51 @@ 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); + } + } + // + super.setCompoundDrawables(left, top, right, bottom); + } + private void setDrawable(Drawable drawable, int tag, int drawableWidth, int drawableHeight) { // 首先要有数据 - if (getLineCount() == 0) { + if (getLineCount() == 0 || getLineCount() == 1) { return; } //获取图片实际长宽 int mDrawableWidth = drawableWidth == 0 ? drawable.getIntrinsicWidth() : drawableWidth; int mDrawableHeight = drawableHeight == 0 ? drawable.getIntrinsicHeight() : drawableHeight; // view 的高度 - int mViewHeight = getHeight(); + int mViewHeight = getHeight() - getPaddingBottom() - getPaddingTop(); // 行数、行高、行数*行高 int mLineCount = getLineCount(); int mLineHeight = getLineHeight(); From aea21a6dff41ab69208de97a46de594aef4ea0bf Mon Sep 17 00:00:00 2001 From: xiaxl Date: Thu, 21 Nov 2019 13:47:58 +0800 Subject: [PATCH 3/3] =?UTF-8?q?[=E6=8E=92=E9=99=A4FontPadding=E5=BD=B1?= =?UTF-8?q?=E5=93=8D]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index aa1faa9..e069a4c 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,16 @@ -####先看大众点评的购买须知 +#### 先看大众点评的购买须知 ![大众.png](http://upload-images.jianshu.io/upload_images/3523210-e7971f5aa1049148.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) -######如上图,需求在每条提示语句前加一个小圆点,我刚看到需求就想到用 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)位置进行了控制达到下面的效果: ![123.png](http://upload-images.jianshu.io/upload_images/3523210-ae55e5504b7aae6e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) -######也可以达到图标大小可控的效果 +###### 也可以达到图标大小可控的效果 ![000.png](http://upload-images.jianshu.io/upload_images/3523210-0ad048c82ce9bad8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) -#####下面看看使用方法 -######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~