From e90e0a1082e535b355a94946299f8ae9fc8f04f4 Mon Sep 17 00:00:00 2001 From: tianxiangyu <96164429+FlyJingFish@users.noreply.github.com> Date: Thu, 10 Nov 2022 11:12:54 +0800 Subject: [PATCH] =?UTF-8?q?1=EF=BC=8C=E4=BF=AE=E5=A4=8Dbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 30 ++-- .../gradienttextviewlib/GradientTextView.java | 155 +++++++++++++++++- .../gradienttextviewlib/ViewUtils.java | 65 ++++++++ 3 files changed, 230 insertions(+), 20 deletions(-) create mode 100644 library/src/main/java/com/flyjingfish/gradienttextviewlib/ViewUtils.java diff --git a/README.md b/README.md index 8537e9e..e81f89c 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ ```gradle dependencies { - implementation 'com.github.FlyJingFish:GradientTextView:1.0.6' + implementation 'com.github.FlyJingFish:GradientTextView:1.0.7' } ``` ## 第三步,使用说明 @@ -83,20 +83,20 @@ ### 属性一览 -|attr|format| description | -|---|:---:|:-----------------------:| -|gradient_startColor|color| 前景字体渐变颜色开始颜色 | -|gradient_centerColor|color| 前景字体渐变颜色中心颜色 | -|gradient_endColor|color| 前景字体渐变颜色结束颜色 | -|gradient_angle|float| 前景字体渐变颜色开始角度 | -|gradient_rtl_angle|boolean| 前景字体渐变颜色开始角度是否支持镜像Rtl适配 | -|gradient_stroke_startColor|color| 字体粗边渐变颜色开始颜色 | -|gradient_stroke_centerColor|color| 字体粗边渐变颜色中心颜色 | -|gradient_stroke_endColor|color| 字体粗边渐变颜色结束颜色 | -|gradient_stroke_angle|float| 字体粗边渐变颜色开始角度 | -|gradient_stroke_rtl_angle|boolean| 字体粗边渐变颜色开始角度是否支持镜像Rtl适配 | -|gradient_stroke_strokeWidth|dimension| 字体粗边画笔宽度 | -|gradient_stroke_textColor|color| 字体粗边颜色(设置渐变色之后此属性无效) | +| attr | format | description | +|-----------------------------|:---------:|:-----------------------:| +| gradient_startColor | color | 前景字体渐变颜色开始颜色 | +| gradient_centerColor | color | 前景字体渐变颜色中心颜色 | +| gradient_endColor | color | 前景字体渐变颜色结束颜色 | +| gradient_angle | float | 前景字体渐变颜色开始角度 | +| gradient_rtl_angle | boolean | 前景字体渐变颜色开始角度是否支持镜像Rtl适配 | +| gradient_stroke_startColor | color | 字体粗边渐变颜色开始颜色 | +| gradient_stroke_centerColor | color | 字体粗边渐变颜色中心颜色 | +| gradient_stroke_endColor | color | 字体粗边渐变颜色结束颜色 | +| gradient_stroke_angle | float | 字体粗边渐变颜色开始角度 | +| gradient_stroke_rtl_angle | boolean | 字体粗边渐变颜色开始角度是否支持镜像Rtl适配 | +| gradient_stroke_strokeWidth | dimension | 字体粗边画笔宽度 | +| gradient_stroke_textColor | color | 字体粗边颜色(设置渐变色之后此属性无效) | ### 常见问题 diff --git a/library/src/main/java/com/flyjingfish/gradienttextviewlib/GradientTextView.java b/library/src/main/java/com/flyjingfish/gradienttextviewlib/GradientTextView.java index 0743cf9..d05c59a 100644 --- a/library/src/main/java/com/flyjingfish/gradienttextviewlib/GradientTextView.java +++ b/library/src/main/java/com/flyjingfish/gradienttextviewlib/GradientTextView.java @@ -8,12 +8,14 @@ import android.graphics.LinearGradient; import android.graphics.Paint; import android.graphics.Shader; +import android.graphics.drawable.Drawable; import android.text.TextPaint; import android.util.AttributeSet; import android.util.LayoutDirection; import android.view.ViewGroup; import android.widget.TextView; +import androidx.annotation.Nullable; import androidx.appcompat.widget.AppCompatTextView; import androidx.core.text.TextUtilsCompat; @@ -95,6 +97,10 @@ public GradientTextView(Context context, AttributeSet attrs, backGroundText.setTextColor(strokeTextColor); backGroundText.setText(getText()); backGroundText.setGravity(getGravity()); + + initCompoundDrawables(); + + backGroundText.setCompoundDrawablePadding(getCompoundDrawablePadding()); } @@ -155,8 +161,9 @@ protected void onDraw(Canvas canvas) { } protected float[] getAngleXY(float currentAngle){ - int height = getHeight(); - int width = getWidth(); + int[] paddings = getCompoundDrawablesPaddings(); + int height = getHeight() - paddings[3] - paddings[1]; + int width = getWidth() - paddings[2] - paddings[0]; float angle = currentAngle % 360; if (angle < 0) { @@ -177,8 +184,7 @@ protected float[] getAngleXY(float currentAngle){ y0 = height / 2f * percent + height / 2f; } else if (angle <= 180) { float percent = (angle - 135) / 45; - x0 = width / 2f + width / 2f * percent; - ; + x0 = width / 2f + width / 2f * (1-percent); y0 = height; } else if (angle <= 225) { float percent = (angle - 180) / 45; @@ -195,9 +201,9 @@ protected float[] getAngleXY(float currentAngle){ } else { float percent = (angle - 315) / 45; x0 = width / 2f * percent; - ; y0 = 0; } + x1 = width - x0; y1 = height - y0; @@ -307,4 +313,143 @@ public void setStrokeTextColor(int strokeTextColor) { gradientStrokeColor = false; invalidate(); } + + @Override + public void setText(CharSequence text, BufferType type) { + if (backGroundText != null){ + backGroundText.setText(text, type); + } + super.setText(text, type); + } + + @Override + public void setCompoundDrawables(@Nullable Drawable left, @Nullable Drawable top, @Nullable Drawable right, @Nullable Drawable bottom) { + super.setCompoundDrawables(left, top, right, bottom); + initCompoundDrawables(); + } + + @Override + public void setCompoundDrawablesRelative(@Nullable Drawable start, @Nullable Drawable top, @Nullable Drawable end, @Nullable Drawable bottom) { + super.setCompoundDrawablesRelative(start, top, end, bottom); + initCompoundDrawables(); + } + + @Override + public void setCompoundDrawablePadding(int pad) { + super.setCompoundDrawablePadding(pad); + if (backGroundText != null){ + backGroundText.setCompoundDrawablePadding(pad); + } + } + + private void initCompoundDrawables(){ + if (backGroundText == null){ + return; + } + Drawable[] drawablesRelative = getCompoundDrawablesRelative(); + + Drawable[] drawables = getCompoundDrawables(); + + Drawable drawableLeft; + Drawable drawableRight; + Drawable drawableTop = null; + Drawable drawableBottom = null; + if (isRtl){ + if (drawablesRelative[0] != null || drawablesRelative[2] != null){ + drawableLeft = drawablesRelative[2]; + drawableRight = drawablesRelative[0]; + }else { + drawableLeft = drawables[0]; + drawableRight = drawables[2]; + } + + }else { + if (drawablesRelative[0] != null || drawablesRelative[2] != null){ + drawableLeft = drawablesRelative[0]; + drawableRight = drawablesRelative[2]; + }else { + drawableLeft = drawables[0]; + drawableRight = drawables[2]; + } + + } + + if (drawablesRelative[1] != null){ + drawableTop = drawablesRelative[1]; + }else if (drawables[1] != null){ + drawableTop = drawables[1]; + } + + if (drawablesRelative[3] != null){ + drawableBottom = drawablesRelative[3]; + }else if (drawables[3] != null){ + drawableBottom = drawables[3]; + } + + backGroundText.setCompoundDrawables(drawableLeft,drawableTop,drawableRight,drawableBottom); + } + + private int[] getCompoundDrawablesPaddings(){ + Drawable[] drawablesRelative = getCompoundDrawablesRelative(); + + Drawable[] drawables = getCompoundDrawables(); + + Drawable drawableLeft; + Drawable drawableRight; + Drawable drawableTop = null; + Drawable drawableBottom = null; + if (isRtl){ + if (drawablesRelative[0] != null || drawablesRelative[2] != null){ + drawableLeft = drawablesRelative[2]; + drawableRight = drawablesRelative[0]; + }else { + drawableLeft = drawables[0]; + drawableRight = drawables[2]; + } + + }else { + if (drawablesRelative[0] != null || drawablesRelative[2] != null){ + drawableLeft = drawablesRelative[0]; + drawableRight = drawablesRelative[2]; + }else { + drawableLeft = drawables[0]; + drawableRight = drawables[2]; + } + + } + + if (drawablesRelative[1] != null){ + drawableTop = drawablesRelative[1]; + }else if (drawables[1] != null){ + drawableTop = drawables[1]; + } + + if (drawablesRelative[3] != null){ + drawableBottom = drawablesRelative[3]; + }else if (drawables[3] != null){ + drawableBottom = drawables[3]; + } + + int[] paddings = new int[4]; + paddings[0] = ViewUtils.getViewPaddingLeft(this); + paddings[1] = getPaddingTop(); + paddings[2] = ViewUtils.getViewPaddingRight(this); + paddings[3] = getPaddingBottom(); + int drawablePadding = getCompoundDrawablePadding(); + if (drawableLeft != null){ + paddings[0] = drawableLeft.getMinimumWidth()+paddings[0]+drawablePadding; + } + if (drawableTop != null){ + paddings[1] = drawableTop.getMinimumWidth()+paddings[1]+drawablePadding; + } + if (drawableRight != null){ + paddings[2] = drawableRight.getMinimumWidth()+paddings[2]+drawablePadding; + } + + if (drawableBottom != null){ + paddings[3] = drawableBottom.getMinimumWidth()+paddings[3]+drawablePadding; + } + + return paddings; + } } diff --git a/library/src/main/java/com/flyjingfish/gradienttextviewlib/ViewUtils.java b/library/src/main/java/com/flyjingfish/gradienttextviewlib/ViewUtils.java new file mode 100644 index 0000000..dc74214 --- /dev/null +++ b/library/src/main/java/com/flyjingfish/gradienttextviewlib/ViewUtils.java @@ -0,0 +1,65 @@ +package com.flyjingfish.gradienttextviewlib; + +import android.util.LayoutDirection; +import android.view.View; + +import androidx.core.text.TextUtilsCompat; + +import java.util.Locale; + +public class ViewUtils { + + public static int getViewPaddingLeft(View view){ + boolean isRtl = false; + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { + isRtl = TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == LayoutDirection.RTL; + } + int paddingStart = view.getPaddingStart(); + int paddingEnd = view.getPaddingEnd(); + int paddingLeft = view.getPaddingLeft(); + int paddingLeftMax; + + if (isRtl){ + if (paddingEnd != 0){ + paddingLeftMax = paddingEnd; + }else { + paddingLeftMax = paddingLeft; + } + }else { + if (paddingStart != 0){ + paddingLeftMax = paddingStart; + }else { + paddingLeftMax = paddingLeft; + } + + } + + return paddingLeftMax; + } + + public static int getViewPaddingRight(View view){ + boolean isRtl = false; + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { + isRtl = TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == LayoutDirection.RTL; + } + int paddingStart = view.getPaddingStart(); + int paddingEnd = view.getPaddingEnd(); + int paddingRight = view.getPaddingRight(); + int paddingRightMax; + if (isRtl){ + if (paddingStart != 0){ + paddingRightMax = paddingStart; + }else { + paddingRightMax = paddingRight; + } + }else { + if (paddingEnd != 0){ + paddingRightMax = paddingEnd; + }else { + paddingRightMax = paddingRight; + } + + } + return paddingRightMax; + } +} \ No newline at end of file