Skip to content

Commit

Permalink
1,修复bug
Browse files Browse the repository at this point in the history
  • Loading branch information
FlyJingFish committed Nov 10, 2022
1 parent eb42887 commit e90e0a1
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 20 deletions.
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

```gradle
dependencies {
implementation 'com.github.FlyJingFish:GradientTextView:1.0.6'
implementation 'com.github.FlyJingFish:GradientTextView:1.0.7'
}
```
## 第三步,使用说明
Expand Down Expand Up @@ -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 | 字体粗边颜色(设置渐变色之后此属性无效) |

### 常见问题

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -95,6 +97,10 @@ public GradientTextView(Context context, AttributeSet attrs,
backGroundText.setTextColor(strokeTextColor);
backGroundText.setText(getText());
backGroundText.setGravity(getGravity());

initCompoundDrawables();

backGroundText.setCompoundDrawablePadding(getCompoundDrawablePadding());
}


Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit e90e0a1

Please sign in to comment.