Skip to content

Commit 633f81b

Browse files
author
xiaoqi01
committed
update 1.7.5 增加文字颜色渐变
1 parent c64b686 commit 633f81b

File tree

14 files changed

+282
-16
lines changed

14 files changed

+282
-16
lines changed

BackgroundLibrary.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,4 +1490,22 @@
14901490
<option name="XML" value="true" />
14911491
</context>
14921492
</template>
1493+
<template name="bl_text_gradient_startColor" value="app:bl_text_gradient_startColor=&quot;$value$&quot;" description="文字渐变起始颜色" toReformat="true" toShortenFQNames="true">
1494+
<variable name="value" expression="" defaultValue="" alwaysStopAt="true" />
1495+
<context>
1496+
<option name="XML" value="true" />
1497+
</context>
1498+
</template>
1499+
<template name="bl_text_gradient_endColor" value="app:bl_text_gradient_endColor=&quot;$value$&quot;" description="文字渐变结束颜色" toReformat="true" toShortenFQNames="true">
1500+
<variable name="value" expression="" defaultValue="" alwaysStopAt="true" />
1501+
<context>
1502+
<option name="XML" value="true" />
1503+
</context>
1504+
</template>
1505+
<template name="bl_text_gradient_orientation" value="app:bl_text_gradient_orientation=&quot;$value$&quot;" description="文字渐变方向" toReformat="true" toShortenFQNames="true">
1506+
<variable name="value" expression="" defaultValue="" alwaysStopAt="true" />
1507+
<context>
1508+
<option name="XML" value="true" />
1509+
</context>
1510+
</template>
14931511
</templateSet>

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313
tools:context=".MainActivity"
1414
tools:ignore="MissingPrefix,HardcodedText">
1515

16+
<TextView
17+
android:layout_width="wrap_content"
18+
android:text="dasdasdads"
19+
app:bl_text_gradient_orientation="horizontal"
20+
app:bl_text_gradient_startColor="@color/colorAccent"
21+
app:bl_text_gradient_endColor="@android:color/holo_green_dark"
22+
android:layout_height="wrap_content"/>
23+
1624
<com.noober.background.view.BLTextView
1725
android:layout_width="match_parent"
1826
android:layout_height="50dp"

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ dependencies {
4040
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
4141
// implementation 'com.noober.background:core:1.6.5'
4242
// implementation 'com.github.JavaNoober:BackgroundLibrary:1.6.6'
43-
// implementation project(':library')
44-
implementation 'com.github.JavaNoober.BackgroundLibrary:library:1.7.3'
43+
implementation project(':library')
44+
// implementation 'com.github.JavaNoober.BackgroundLibrary:library:1.7.3'
4545

4646
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
4747
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@
1313
tools:context=".MainActivity"
1414
tools:ignore="MissingPrefix">
1515

16+
<TextView
17+
android:layout_width="wrap_content"
18+
android:text="dasdasdads"
19+
app:bl_text_gradient_orientation="horizontal"
20+
app:bl_text_gradient_startColor="@color/colorAccent"
21+
app:bl_text_gradient_endColor="@android:color/holo_green_dark"
22+
android:layout_height="wrap_content"/>
23+
24+
1625
<Button
1726
android:id="@+id/btnConsole"
1827
android:layout_width="match_parent"

library/src/main/java/com/noober/background/BackgroundFactory.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import android.widget.TextView;
2323

2424
import com.noober.background.drawable.DrawableFactory;
25+
import com.noober.background.drawable.TextViewFactory;
2526

2627
import java.lang.reflect.Constructor;
2728
import java.lang.reflect.InvocationTargetException;
@@ -84,6 +85,7 @@ private static View setViewBackground(String name, Context context, AttributeSet
8485
TypedArray animTa = context.obtainStyledAttributes(attrs, R.styleable.bl_anim);
8586
TypedArray multiSelTa = context.obtainStyledAttributes(attrs, R.styleable.background_multi_selector);
8687
TypedArray multiTextTa = context.obtainStyledAttributes(attrs, R.styleable.background_multi_selector_text);
88+
TypedArray textViewTa = context.obtainStyledAttributes(attrs, R.styleable.bl_text);
8789
TypedArray selectorPre21Ta = null;
8890
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
8991
selectorPre21Ta = context.obtainStyledAttributes(attrs, R.styleable.background_selector_pre_21);
@@ -92,7 +94,7 @@ private static View setViewBackground(String name, Context context, AttributeSet
9294
try {
9395
if (typedArray.getIndexCount() == 0 && selectorTa.getIndexCount() == 0 && pressTa.getIndexCount() == 0
9496
&& textTa.getIndexCount() == 0 && buttonTa.getIndexCount() == 0 && animTa.getIndexCount() == 0
95-
&& multiSelTa.getIndexCount() == 0 && multiTextTa.getIndexCount() == 0) {
97+
&& multiSelTa.getIndexCount() == 0 && multiTextTa.getIndexCount() == 0 && textViewTa.getIndexCount() == 0) {
9698
return view;
9799
}
98100
if (view == null) {
@@ -151,8 +153,11 @@ private static View setViewBackground(String name, Context context, AttributeSet
151153
((TextView) view).setTextColor(DrawableFactory.getTextSelectorColor(textTa));
152154
} else if (view instanceof TextView && multiTextTa.getIndexCount() > 0) {
153155
((TextView) view).setTextColor(DrawableFactory.getMultiTextColorSelectorColorCreator(context, multiTextTa));
156+
} else if (view instanceof TextView && textViewTa.getIndexCount() > 0) {
157+
TextViewFactory.setTextGradientColor(context, attrs, (TextView) view);
154158
}
155159

160+
156161
if (typedArray.getBoolean(R.styleable.background_bl_ripple_enable, false) &&
157162
typedArray.hasValue(R.styleable.background_bl_ripple_color)) {
158163
int color = typedArray.getColor(R.styleable.background_bl_ripple_color, 0);
@@ -209,6 +214,7 @@ public void onClick(View view) {
209214
animTa.recycle();
210215
multiSelTa.recycle();
211216
multiTextTa.recycle();
217+
textViewTa.recycle();
212218
if (selectorPre21Ta != null) {
213219
selectorPre21Ta.recycle();
214220
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.noober.background.drawable;
2+
3+
import android.content.Context;
4+
import android.util.AttributeSet;
5+
import android.widget.TextView;
6+
7+
/**
8+
* Author: xiaoqi
9+
* Date: 2022/8/17 3:13 下午
10+
* Description:
11+
*/
12+
public interface ITextViewOperator {
13+
void invoke(Context context, AttributeSet attrs, TextView textView);
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.noober.background.drawable;
2+
3+
import android.content.Context;
4+
import android.util.AttributeSet;
5+
import android.widget.TextView;
6+
7+
/**
8+
* Author: xiaoqi
9+
* Date: 2022/8/17 3:11 下午
10+
* Description:
11+
*/
12+
public class TextViewFactory {
13+
14+
public static void setTextGradientColor(Context context, AttributeSet attrs, final TextView textView){
15+
new TextViewGradientColor().invoke(context, attrs, textView);
16+
}
17+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.noober.background.drawable;
2+
3+
import android.content.Context;
4+
import android.content.res.TypedArray;
5+
import android.graphics.LinearGradient;
6+
import android.graphics.Shader;
7+
import android.util.AttributeSet;
8+
import android.widget.TextView;
9+
10+
import com.noober.background.R;
11+
12+
/**
13+
* Author: xiaoqi
14+
* Date: 2022/8/17 3:14 下午
15+
* Description:
16+
*/
17+
public class TextViewGradientColor implements ITextViewOperator{
18+
private int endColor = -1;
19+
private int startColor = -1;
20+
private int orientation = 0;
21+
22+
@Override
23+
public void invoke(Context context, AttributeSet attrs, final TextView textView) {
24+
TypedArray textTa = context.obtainStyledAttributes(attrs, R.styleable.bl_text);
25+
try {
26+
if(textTa.getIndexCount() == 0){
27+
return;
28+
}
29+
for (int i = 0; i < textTa.getIndexCount(); i++) {
30+
int attr = textTa.getIndex(i);
31+
if(attr == R.styleable.bl_text_bl_text_gradient_endColor){
32+
endColor = textTa.getColor(attr, -1);
33+
}else if(attr == R.styleable.bl_text_bl_text_gradient_startColor){
34+
startColor = textTa.getColor(attr, -1);
35+
}else if(attr == R.styleable.bl_text_bl_text_gradient_orientation){
36+
orientation = textTa.getInt(attr, 0);
37+
}
38+
}
39+
40+
if(endColor == -1 && startColor != -1){
41+
textView.setTextColor(startColor);
42+
}else if(startColor == -1 && endColor != -1){
43+
textView.setTextColor(endColor);
44+
}else if(endColor != -1 && startColor != -1){
45+
if(orientation == 0){
46+
textView.post(new Runnable() {
47+
@Override
48+
public void run() {
49+
textView.getPaint().setShader(new LinearGradient(0f, 0f, 0f, textView.getPaint().descent() - textView.getPaint().ascent(),
50+
startColor, endColor, Shader.TileMode.REPEAT));
51+
textView.invalidate();
52+
}
53+
});
54+
55+
}else {
56+
textView.post(new Runnable() {
57+
@Override
58+
public void run() {
59+
textView.getPaint().setShader(new LinearGradient(0, 0f, textView.getMeasuredWidth(), 0f, startColor, endColor, Shader.TileMode.REPEAT));
60+
textView.invalidate();
61+
}
62+
});
63+
}
64+
}
65+
}catch (Exception e){
66+
67+
}finally {
68+
textTa.recycle();
69+
}
70+
}
71+
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,4 +452,12 @@
452452
<attr name="bl_frame_drawable_item14" format="reference"/>
453453
</declare-styleable>
454454

455+
<declare-styleable name="bl_text">
456+
<attr name="bl_text_gradient_startColor" format="color|reference" />
457+
<attr name="bl_text_gradient_endColor" format="color|reference" />
458+
<attr name="bl_text_gradient_orientation" format="enum">
459+
<enum name="vertical" value="0" />
460+
<enum name="horizontal" value="1" />
461+
</attr>
462+
</declare-styleable>
455463
</resources>

libraryx/src/main/java/com/noober/background/BackgroundFactory.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import android.widget.TextView;
2121

2222
import com.noober.background.drawable.DrawableFactory;
23+
import com.noober.background.drawable.TextViewFactory;
2324

2425
import java.lang.reflect.Constructor;
2526
import java.lang.reflect.InvocationTargetException;
@@ -85,16 +86,16 @@ private static View setViewBackground(String name, Context context, AttributeSet
8586
TypedArray animTa = context.obtainStyledAttributes(attrs, R.styleable.bl_anim);
8687
TypedArray multiSelTa = context.obtainStyledAttributes(attrs, R.styleable.background_multi_selector);
8788
TypedArray multiTextTa = context.obtainStyledAttributes(attrs, R.styleable.background_multi_selector_text);
88-
89+
TypedArray textViewTa = context.obtainStyledAttributes(attrs, R.styleable.bl_text);
8990
TypedArray selectorPre21Ta = null;
9091
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
9192
selectorPre21Ta = context.obtainStyledAttributes(attrs, R.styleable.background_selector_pre_21);
9293
}
9394

9495
try {
9596
if (typedArray.getIndexCount() == 0 && selectorTa.getIndexCount() == 0 && pressTa.getIndexCount() == 0
96-
&& textTa.getIndexCount() == 0 && buttonTa.getIndexCount() == 0 && animTa.getIndexCount() == 0
97-
&& multiSelTa.getIndexCount() == 0 && multiTextTa.getIndexCount() == 0) {
97+
&& textTa.getIndexCount() == 0 && buttonTa.getIndexCount() == 0 && animTa.getIndexCount() == 0
98+
&& multiSelTa.getIndexCount() == 0 && multiTextTa.getIndexCount() == 0 && textViewTa.getIndexCount() == 0) {
9899
return view;
99100
}
100101
if (view == null) {
@@ -153,10 +154,13 @@ private static View setViewBackground(String name, Context context, AttributeSet
153154
((TextView) view).setTextColor(DrawableFactory.getTextSelectorColor(textTa));
154155
} else if (view instanceof TextView && multiTextTa.getIndexCount() > 0) {
155156
((TextView) view).setTextColor(DrawableFactory.getMultiTextColorSelectorColorCreator(context, multiTextTa));
157+
} else if (view instanceof TextView && textViewTa.getIndexCount() > 0) {
158+
TextViewFactory.setTextGradientColor(context, attrs, (TextView) view);
156159
}
157160

161+
158162
if (typedArray.getBoolean(R.styleable.background_bl_ripple_enable, false) &&
159-
typedArray.hasValue(R.styleable.background_bl_ripple_color)) {
163+
typedArray.hasValue(R.styleable.background_bl_ripple_color)) {
160164
int color = typedArray.getColor(R.styleable.background_bl_ripple_color, 0);
161165
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
162166
Drawable contentDrawable = (stateListDrawable == null ? drawable : stateListDrawable);
@@ -180,7 +184,7 @@ private static View setViewBackground(String name, Context context, AttributeSet
180184
final Context currentContext = view.getContext();
181185
final Class parentClass = currentContext.getClass();
182186
final Method method = getMethod(parentClass, methodName);
183-
if (method != null) {
187+
if(method != null){
184188
view.setOnClickListener(new View.OnClickListener() {
185189
@Override
186190
public void onClick(View view) {
@@ -211,13 +215,13 @@ public void onClick(View view) {
211215
animTa.recycle();
212216
multiSelTa.recycle();
213217
multiTextTa.recycle();
218+
textViewTa.recycle();
214219
if (selectorPre21Ta != null) {
215220
selectorPre21Ta.recycle();
216221
}
217222
}
218223
}
219224

220-
221225
private static Method getMethod(Class clazz, String methodName) {
222226
Method method = null;
223227
HashMap<String, Method> methodHashMap = methodMap.get(clazz.getCanonicalName());
@@ -376,7 +380,7 @@ private static View createView(Context context, String name, String prefix) thro
376380
try {
377381
if (constructor == null) {
378382
Class<? extends View> clazz = context.getClassLoader().loadClass(
379-
prefix != null ? (prefix + name) : name).asSubclass(View.class);
383+
prefix != null ? (prefix + name) : name).asSubclass(View.class);
380384

381385
constructor = clazz.getConstructor(sConstructorSignature);
382386
sConstructorMap.put(name, constructor);
@@ -394,13 +398,14 @@ public View onCreateView(View parent, String name, Context context, AttributeSet
394398
return onCreateView(name, context, attrs);
395399
}
396400

397-
398401
private static boolean hasGradientState(TypedArray typedArray) {
399402
return typedArray.hasValue(R.styleable.background_bl_checkable_gradient_startColor) ||
400-
typedArray.hasValue(R.styleable.background_bl_checked_gradient_startColor) ||
401-
typedArray.hasValue(R.styleable.background_bl_enabled_gradient_startColor) ||
402-
typedArray.hasValue(R.styleable.background_bl_selected_gradient_startColor) ||
403-
typedArray.hasValue(R.styleable.background_bl_pressed_gradient_startColor) ||
404-
typedArray.hasValue(R.styleable.background_bl_focused_gradient_startColor);
403+
typedArray.hasValue(R.styleable.background_bl_checked_gradient_startColor) ||
404+
typedArray.hasValue(R.styleable.background_bl_enabled_gradient_startColor) ||
405+
typedArray.hasValue(R.styleable.background_bl_selected_gradient_startColor) ||
406+
typedArray.hasValue(R.styleable.background_bl_pressed_gradient_startColor) ||
407+
typedArray.hasValue(R.styleable.background_bl_focused_gradient_startColor);
405408
}
406409
}
410+
411+

0 commit comments

Comments
 (0)