Skip to content

Commit e12b51f

Browse files
committed
1,修改裁剪问题
1 parent fae905f commit e12b51f

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
```gradle
3939
dependencies {
40-
implementation 'com.github.FlyJingFish:GradientTextView:1.0.9'
40+
implementation 'com.github.FlyJingFish:GradientTextView:1.1.0'
4141
}
4242
```
4343
## 第三步,使用说明
@@ -108,9 +108,6 @@
108108
| gradient_stroke_textColor | color | 字体粗边颜色(设置渐变色之后此属性无效) |
109109
| gradient_stroke_join | enum | 字体粗边样式 round/bevel/miter 具体效果自行尝试(不建议使用miter,因为目前发现部分英文字母出现尖头) |
110110

111-
### 常见问题
112-
113-
1,如果使用粗边可能会存在左右两侧被切除一部分,可在字符串两侧添加空格解决问题
114111

115112

116113
# 最后推荐我写的另一个库,轻松实现在应用内点击小图查看大图的动画放大效果

app/src/main/res/layout/activity_main.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
android:layout_width="wrap_content"
3434
android:layout_height="wrap_content"
3535
android:layout_margin="10dp"
36-
android:text=" Hello World! "
36+
android:text="Hello World!"
3737
android:textColor="@color/white"
3838
android:textSize="30sp"
3939
app:gradient_angle="0"
@@ -47,7 +47,7 @@
4747
android:layout_width="wrap_content"
4848
android:layout_height="wrap_content"
4949
android:layout_margin="10dp"
50-
android:text=" Hello World! "
50+
android:text="Hello World!"
5151
android:textColor="@color/white"
5252
android:textSize="30sp"
5353
android:textStyle="italic|bold"

library/src/main/java/com/flyjingfish/gradienttextviewlib/GradientTextView.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
import android.graphics.Shader;
1010
import android.graphics.drawable.Drawable;
1111
import android.text.Layout;
12+
import android.text.SpannableString;
1213
import android.text.TextPaint;
14+
import android.text.style.LeadingMarginSpan;
1315
import android.util.AttributeSet;
1416
import android.util.LayoutDirection;
1517
import android.view.ViewGroup;
@@ -107,9 +109,15 @@ public GradientTextView(Context context, AttributeSet attrs,
107109

108110
initCompoundDrawables();
109111

110-
112+
CharSequence text = getText();
113+
setText(text);
111114
}
112115

116+
static SpannableString createIndentedText(CharSequence text, int marginFirstLine, int marginNextLines) {
117+
SpannableString result = new SpannableString(text);
118+
result.setSpan(new LeadingMarginSpan.Standard(marginFirstLine, marginNextLines), 0, text.length(), 0);
119+
return result;
120+
}
113121

114122
@Override
115123
public void setLayoutParams(ViewGroup.LayoutParams params) {
@@ -119,12 +127,25 @@ public void setLayoutParams(ViewGroup.LayoutParams params) {
119127

120128
@Override
121129
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
130+
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
122131
CharSequence tt = backGroundText.getText();
123132
if (tt == null || !tt.equals(this.getText())) {
124133
backGroundText.setText(getText());
125134
}
126135
backGroundText.measure(widthMeasureSpec, heightMeasureSpec);
127136
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
137+
if (widthMode == MeasureSpec.AT_MOST && strokeWidth > 0){
138+
int measureWidth = getMeasuredWidth();
139+
int width = MeasureSpec.getSize(widthMeasureSpec);
140+
if (measureWidth < width){
141+
int measureHeight = getMeasuredHeight();
142+
// int height = MeasureSpec.getSize(heightMeasureSpec);
143+
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
144+
backGroundText.measure(widthMeasureSpec, heightMeasureSpec);
145+
int newWidth = MeasureSpec.makeMeasureSpec(measureWidth+Math.min(strokeWidth/2,width-measureWidth), widthMode);
146+
setMeasuredDimension(newWidth,MeasureSpec.makeMeasureSpec(measureHeight, heightMode));
147+
}
148+
}
128149
}
129150

130151
@Override
@@ -323,10 +344,11 @@ public void setStrokeTextColor(int strokeTextColor) {
323344

324345
@Override
325346
public void setText(CharSequence text, BufferType type) {
347+
SpannableString spannableString = createIndentedText(text, strokeWidth/2, strokeWidth/2);
326348
if (backGroundText != null){
327-
backGroundText.setText(text, type);
349+
backGroundText.setText(spannableString, type);
328350
}
329-
super.setText(text, type);
351+
super.setText(spannableString, type);
330352
}
331353

332354
@Override

0 commit comments

Comments
 (0)