Skip to content

Commit 2391495

Browse files
committed
增加支持渐变属性
1 parent 72fbca2 commit 2391495

File tree

9 files changed

+141
-72
lines changed

9 files changed

+141
-72
lines changed

README.md

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* 链式调用
1414
* 支持给iconfont左右设置文字
1515
* 支持xml中直接设置常用shape的所有属性
16+
* 支持渐变背景色,边线,纯色背景色,圆角等
1617
* 支持分别设置iconfont,左文字,右文字的颜色(Selector也可以)、字号、样式(粗体、斜体)
1718
* 支持左边文字,中间文字,右边文字都设置iconfont
1819
* 支持文字和iconfont居中
@@ -39,7 +40,7 @@ dependencies {
3940
}
4041
```
4142
### 效果
42-
<img src="https://github.com/DrownCoder/EasyTextView/blob/master/easttextview_demo.jpg" width = "362" height = "642" alt="自由发挥想象空间" align=center />
43+
<img src="https://github.com/DrownCoder/EasyTextView/blob/master/EasyTextView.jpg" width = "362" height = "642" alt="自由发挥想象空间" align=center />
4344

4445
### 使用
4546
#### xml属性
@@ -108,7 +109,22 @@ dependencies {
108109
<enum name="bold" value="1" />
109110
<enum name="italic" value="2" />
110111
</attr>
111-
<attr name="autoMaxHeight" format="boolean"/>
112+
<attr name="autoMaxHeight" format="boolean" />
113+
//渐变色方向
114+
<attr name="gradientOrientation">
115+
<enum name="top_bottom" value="0" />
116+
<enum name="tp_bl" value="1" />
117+
<enum name="right_left" value="2" />
118+
<enum name="br_tl" value="3" />
119+
<enum name="bottom_top" value="4" />
120+
<enum name="bl_tr" value="5" />
121+
<enum name="left_right" value="6" />
122+
<enum name="tl_br" value="7" />
123+
</attr>
124+
//渐变色的起始色,中间色,末尾色
125+
<attr name="startSolid" format="reference|color" />
126+
<attr name="centerSolid" format="reference|color" />
127+
<attr name="endSolid" format="reference|color" />
112128
</declare-styleable>
113129
```
114130
#### java Api
@@ -154,12 +170,14 @@ public void addSpanRight(Object object, int start, int end, int flags);
154170
详细使用说明参考[Wiki](https://github.com/DrownCoder/EasyTextView/wiki)
155171

156172
### 版本更新
157-
v1.13
158-
>1.修改icon类型从String改为charsequence
159-
>2.进一步拓展JAVA的API
160-
>3.补充Error信息
173+
v1.14
174+
>新增xml中支持设置渐变色
175+
v1.13
176+
>1.修改icon类型从String改为charsequence
177+
>2.进一步拓展JAVA的API
178+
>3.补充Error信息
161179
162-
v1.12
180+
v1.12
163181
>1.修复center_vertical时文字重叠问题
164182
>2.新增boolean型autoMaxHeight属性,当为true时解决以下问题
165183
>-修复左右文字大小大于中间文字大小高度不准问题

WX20180614-214347@2x.png

-163 KB
Binary file not shown.

app/src/main/java/com/study/xuan/easytextview/MainActivity.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,15 @@
33
import android.graphics.Color;
44
import android.os.Bundle;
55
import android.support.v7.app.AppCompatActivity;
6-
import android.support.v7.widget.LinearLayoutManager;
76
import android.support.v7.widget.RecyclerView;
8-
import android.text.SpannableStringBuilder;
97
import android.text.Spanned;
108
import android.text.style.ForegroundColorSpan;
119
import android.text.style.StrikethroughSpan;
1210
import android.text.style.UnderlineSpan;
13-
import android.view.LayoutInflater;
1411
import android.view.View;
15-
import android.view.ViewGroup;
16-
import android.widget.TextView;
1712

1813
import com.study.xuan.library.widget.EasyTextView;
1914

20-
import java.util.ArrayList;
21-
import java.util.List;
22-
2315
public class MainActivity extends AppCompatActivity {
2416
private EasyTextView textView;
2517
private int[] color = new int[]{

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

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585

8686
<com.study.xuan.library.widget.EasyTextView
8787
android:layout_marginLeft="20dp"
88+
android:id="@+id/etv_address"
8889
android:layout_width="wrap_content"
8990
android:layout_height="wrap_content"
9091
app:textLeft="坐标:"
@@ -95,12 +96,11 @@
9596
android:text="&#xe63f;"
9697
app:textRight="北京"
9798
android:gravity="center"
98-
app:textPadding="4dp"
99+
android:includeFontPadding="false"
99100
app:textRightColor="@color/black"
100101
android:padding="4dp"
101102
app:totalRadius="4dp"
102-
app:soildBac="@color/white"
103-
/>
103+
app:soildBac="@color/white" />
104104
</LinearLayout>
105105

106106

@@ -110,14 +110,14 @@
110110
android:orientation="horizontal"
111111
android:background="@color/lightblack"
112112
android:layout_marginTop="10dp"
113-
android:gravity="center_vertical"
114-
>
113+
android:gravity="center_vertical">
114+
115115
<TextView
116116
android:layout_marginLeft="10dp"
117117
android:layout_width="wrap_content"
118118
android:layout_height="wrap_content"
119-
android:text="span样式:"
120-
/>
119+
android:text="span样式:" />
120+
121121
<com.study.xuan.library.widget.EasyTextView
122122
android:id="@+id/easyText"
123123
android:paddingTop="5dp"
@@ -390,33 +390,22 @@
390390
android:layout_marginLeft="10dp"
391391
android:gravity="center_vertical"
392392
android:padding="5dp"
393-
android:text="中间文字"
393+
android:text="渐变"
394394
app:textCenterStyle="bold"
395-
android:textColor="#494949"
395+
android:textColor="@color/white"
396396
app:textLeft="@string/icon_font"
397397
app:textRight="@string/icon_font"
398398
android:textSize="12dp"
399399
app:radiusBottomLeft="15dp"
400400
app:radiusBottomRight="2dp"
401401
app:radiusTopLeft="15dp"
402402
app:radiusTopRight="8dp"
403-
app:soildBac="@color/bohegreen"
403+
app:gradientOrientation="left_right"
404+
app:startSolid="@color/bohegreen"
405+
app:endSolid="@color/colorPrimary"
404406
app:strokeColor="@color/bohegreen"
405407
app:textPadding="5dp" />
406408

407409
</LinearLayout>
408410

409-
<TextView
410-
android:id="@+id/text"
411-
android:layout_width="wrap_content"
412-
android:layout_height="wrap_content"
413-
android:layout_marginTop="20dp" />
414-
415-
<!--<android.support.v7.widget.RecyclerView
416-
android:id="@+id/rcy"
417-
android:layout_width="match_parent"
418-
android:layout_height="match_parent"
419-
android:visibility="gone"
420-
/>-->
421-
422411
</LinearLayout>

device-2018-04-28-151248.png

-101 KB
Binary file not shown.

library/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ dependencies {
3131
})
3232
compile 'com.android.support:appcompat-v7:26.+'
3333
testCompile 'junit:junit:4.12'
34-
compile 'com.github.sdfdzx:SupperShape:v1.0.0'
34+
compile 'com.github.sdfdzx:SupperShape:v1.0.3'
3535
}

library/src/main/java/com/study/xuan/library/widget/EasyTextView.java

Lines changed: 74 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.content.res.ColorStateList;
55
import android.content.res.TypedArray;
66
import android.graphics.Typeface;
7+
import android.graphics.drawable.GradientDrawable;
78
import android.support.annotation.ColorInt;
89
import android.support.annotation.Nullable;
910
import android.support.annotation.StringRes;
@@ -82,6 +83,12 @@ public class EasyTextView extends TextView {
8283
private int iconIndex = 0;
8384
//是否开启计算文字边界,开启后会以最大文字大小为View高度,并且会增加部分文字高度,防止部分英文类型y,g由于基线的原因无法显示完全
8485
private boolean autoMaxHeight;
86+
//渐变色
87+
private ColorStateList startColor = null;
88+
private ColorStateList centerColor = null;
89+
private ColorStateList endColor = null;
90+
//渐变方向
91+
GradientDrawable.Orientation orientation;
8592

8693
public EasyTextView(Context context) {
8794
this(context, null);
@@ -146,8 +153,8 @@ private void initIconFont() {
146153

147154
if (!TextUtils.isEmpty(mTextRight)) {
148155
AbsoluteSizeSpan sizeSpan = new AbsoluteSizeSpan((int) mTextPadding);
149-
stringBuilder.setSpan(sizeSpan, iconIndex + centerSize, iconIndex + centerSize + 1, Spanned
150-
.SPAN_EXCLUSIVE_EXCLUSIVE);
156+
stringBuilder.setSpan(sizeSpan, iconIndex + centerSize, iconIndex +
157+
centerSize + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
151158
}
152159

153160
}
@@ -207,7 +214,8 @@ private void initIconFont() {
207214
for (SpanContainer container : rightContainer) {
208215
for (Object o : container.spans) {
209216
try {
210-
stringBuilder.setSpan(o, start + container.start, start + container.end, container.flag);
217+
stringBuilder.setSpan(o, start + container.start, start + container.end,
218+
container.flag);
211219
} catch (Exception e) {
212220
//please check invoke clearSpan() method first
213221
}
@@ -280,7 +288,8 @@ private void setLeftTextAttr(SpannableStringBuilder stringBuilder) {
280288
}
281289
}
282290

283-
private void initTextSize(SpannableStringBuilder stringBuilder, int start, int end, float textSize, int mCurColor) {
291+
private void initTextSize(SpannableStringBuilder stringBuilder, int start, int end, float
292+
textSize, int mCurColor) {
284293
if (textSize != 0) {
285294
CharacterStyle sizeSpan;
286295
final int gravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
@@ -308,7 +317,8 @@ private void initTextLeftColor(SpannableStringBuilder stringBuilder, int end) {
308317
}
309318
}
310319

311-
private void initTextStyle(int textStyle, SpannableStringBuilder stringBuilder, int start, int end) {
320+
private void initTextStyle(int textStyle, SpannableStringBuilder stringBuilder, int start,
321+
int end) {
312322
StyleSpan span;
313323
if (textStyle != Typeface.NORMAL) {
314324
span = new StyleSpan(textStyle);
@@ -327,14 +337,53 @@ private void initShape() {
327337
}
328338

329339
private void setShape() {
340+
ShapeBuilder shapeBuilder;
330341
if (mRadius != 0) {
331-
ShapeBuilder.create().Type(type).Radius(mRadius).Soild(mSoild).Stroke
332-
(mStrokeWidth, mStrokeColor).build(this);
342+
shapeBuilder = ShapeBuilder.create().Type(type).Radius(mRadius).Stroke
343+
(mStrokeWidth, mStrokeColor);
344+
} else {
345+
shapeBuilder = ShapeBuilder.create().Type(type).Radius(mRadiusTopLeft,
346+
mRadiusTopRight, mRadiusBottomLeft, mRadiusBottomRight).Stroke
347+
(mStrokeWidth, mStrokeColor);
348+
}
349+
if (orientation != null && startColor != null && endColor != null) {
350+
//渐变
351+
if (centerColor != null) {
352+
shapeBuilder.Gradient(orientation, getColor(startColor), getColor(centerColor),
353+
getColor(endColor));
354+
} else {
355+
shapeBuilder.GradientInit(orientation, getColor(startColor), getColor(endColor));
356+
}
333357
} else {
334-
ShapeBuilder.create().Type(type).Radius(mRadiusTopLeft,
335-
mRadiusTopRight, mRadiusBottomLeft, mRadiusBottomRight).Soild(mSoild).Stroke
336-
(mStrokeWidth, mStrokeColor).build(this);
358+
shapeBuilder.Solid(mSoild);
359+
}
360+
shapeBuilder.build(this);
361+
}
362+
363+
private int getColor(ColorStateList color) {
364+
return color.getColorForState(getDrawableState(), 0);
365+
}
366+
367+
private GradientDrawable.Orientation switchEnumToOrientation(int orientation) {
368+
switch (orientation) {
369+
case 0:
370+
return GradientDrawable.Orientation.TOP_BOTTOM;
371+
case 1:
372+
return GradientDrawable.Orientation.TR_BL;
373+
case 2:
374+
return GradientDrawable.Orientation.RIGHT_LEFT;
375+
case 3:
376+
return GradientDrawable.Orientation.BR_TL;
377+
case 4:
378+
return GradientDrawable.Orientation.BOTTOM_TOP;
379+
case 5:
380+
return GradientDrawable.Orientation.BL_TR;
381+
case 6:
382+
return GradientDrawable.Orientation.LEFT_RIGHT;
383+
case 7:
384+
return GradientDrawable.Orientation.TL_BR;
337385
}
386+
return GradientDrawable.Orientation.LEFT_RIGHT;
338387
}
339388

340389
private void clearText() {
@@ -377,8 +426,6 @@ private void initAttr(Context context, AttributeSet attrs) {
377426
mTextRight = textValue.string;
378427
}
379428
}
380-
//mTextLeft = array.getString(R.styleable.EasyTextView_textLeft);
381-
//mTextRight = array.getString(R.styleable.EasyTextView_textRight);
382429
mIconColor = array.getColorStateList(R.styleable.EasyTextView_iconColor);
383430
mLeftColor = array.getColorStateList(R.styleable.EasyTextView_textLeftColor);
384431
mRightColor = array.getColorStateList(R.styleable.EasyTextView_textRightColor);
@@ -388,6 +435,11 @@ private void initAttr(Context context, AttributeSet attrs) {
388435
mTextRightStyle = array.getInt(R.styleable.EasyTextView_textRightStyle, Typeface.NORMAL);
389436
mTextCenterStyle = array.getInt(R.styleable.EasyTextView_textCenterStyle, Typeface.NORMAL);
390437
autoMaxHeight = array.getBoolean(R.styleable.EasyTextView_autoMaxHeight, false);
438+
orientation = switchEnumToOrientation(array.getInt(R.styleable
439+
.EasyTextView_gradientOrientation, 0));
440+
startColor = array.getColorStateList(R.styleable.EasyTextView_startSolid);
441+
centerColor = array.getColorStateList(R.styleable.EasyTextView_centerSolid);
442+
endColor = array.getColorStateList(R.styleable.EasyTextView_endSolid);
391443
array.recycle();
392444
}
393445

@@ -859,14 +911,19 @@ public EasyTextView build() {
859911
}
860912

861913
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
862-
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
863-
if(this.autoMaxHeight) {
914+
try {
915+
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
916+
} catch (Exception e) {
917+
918+
}
919+
if (this.autoMaxHeight) {
864920
int lead = 0;
865-
if(this.getPaint() != null) {
921+
if (this.getPaint() != null) {
866922
lead = this.getPaint().getFontMetricsInt().leading * 3;
867923
}
868-
869-
this.setMeasuredDimension(this.getMeasuredWidth(), (int)(Math.max((float)this.getMeasuredHeight(), Math.max(this.mLeftSize, this.mRightSize)) + (float)lead));
924+
this.setMeasuredDimension(this.getMeasuredWidth(), (int) (Math.max((float) this
925+
.getMeasuredHeight(), Math.max(this.mLeftSize, this.mRightSize)) + (float)
926+
lead));
870927
}
871928

872929
}

library/src/main/res/values/attrs.xml

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,22 @@
77
<enum name="oval" value="1" />
88
<enum name="LINE" value="2" />
99
</attr>
10-
<attr name="totalRadius" format="dimension"/>
11-
<attr name="radiusTopLeft" format="dimension"/>
12-
<attr name="radiusBottomLeft" format="dimension"/>
13-
<attr name="radiusTopRight" format="dimension"/>
14-
<attr name="radiusBottomRight" format="dimension"/>
15-
<attr name="strokeColor" format="color"/>
10+
<attr name="totalRadius" format="dimension" />
11+
<attr name="radiusTopLeft" format="dimension" />
12+
<attr name="radiusBottomLeft" format="dimension" />
13+
<attr name="radiusTopRight" format="dimension" />
14+
<attr name="radiusBottomRight" format="dimension" />
15+
<attr name="strokeColor" format="color" />
1616
<attr name="strokeWidth" format="dimension" />
17-
<attr name="soildBac" format="color"/>
18-
<attr name="textPadding" format="dimension"/>
19-
<attr name="textLeft" format="reference|string"/>
20-
<attr name="textRight" format="reference|string"/>
17+
<attr name="soildBac" format="color" />
18+
<attr name="textPadding" format="dimension" />
19+
<attr name="textLeft" format="string" />
20+
<attr name="textRight" format="string" />
21+
<attr name="iconColor" format="reference|color" />
22+
<attr name="textLeftColor" format="reference|color" />
23+
<attr name="textRightColor" format="reference|color" />
24+
<attr name="textLeftSize" format="dimension" />
25+
<attr name="textRightSize" format="dimension" />
2126
<attr name="textLeftStyle">
2227
<enum name="bold" value="1" />
2328
<enum name="italic" value="2" />
@@ -30,11 +35,19 @@
3035
<enum name="bold" value="1" />
3136
<enum name="italic" value="2" />
3237
</attr>
33-
<attr name="iconColor" format="reference|color"/>
34-
<attr name="textLeftColor" format="reference|color"/>
35-
<attr name="textRightColor" format="reference|color"/>
36-
<attr name="textLeftSize" format="dimension"/>
37-
<attr name="textRightSize" format="dimension"/>
38-
<attr name="autoMaxHeight" format="boolean"/>
38+
<attr name="autoMaxHeight" format="boolean" />
39+
<attr name="gradientOrientation">
40+
<enum name="top_bottom" value="0" />
41+
<enum name="tp_bl" value="1" />
42+
<enum name="right_left" value="2" />
43+
<enum name="br_tl" value="3" />
44+
<enum name="bottom_top" value="4" />
45+
<enum name="bl_tr" value="5" />
46+
<enum name="left_right" value="6" />
47+
<enum name="tl_br" value="7" />
48+
</attr>
49+
<attr name="startSolid" format="reference|color" />
50+
<attr name="centerSolid" format="reference|color" />
51+
<attr name="endSolid" format="reference|color" />
3952
</declare-styleable>
4053
</resources>

0 commit comments

Comments
 (0)