Skip to content

Commit

Permalink
update 1.7.5 增加文字颜色渐变
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoqi01 committed Aug 17, 2022
1 parent c64b686 commit 633f81b
Show file tree
Hide file tree
Showing 14 changed files with 282 additions and 16 deletions.
18 changes: 18 additions & 0 deletions BackgroundLibrary.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1490,4 +1490,22 @@
<option name="XML" value="true" />
</context>
</template>
<template name="bl_text_gradient_startColor" value="app:bl_text_gradient_startColor=&quot;$value$&quot;" description="文字渐变起始颜色" toReformat="true" toShortenFQNames="true">
<variable name="value" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="XML" value="true" />
</context>
</template>
<template name="bl_text_gradient_endColor" value="app:bl_text_gradient_endColor=&quot;$value$&quot;" description="文字渐变结束颜色" toReformat="true" toShortenFQNames="true">
<variable name="value" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="XML" value="true" />
</context>
</template>
<template name="bl_text_gradient_orientation" value="app:bl_text_gradient_orientation=&quot;$value$&quot;" description="文字渐变方向" toReformat="true" toShortenFQNames="true">
<variable name="value" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="XML" value="true" />
</context>
</template>
</templateSet>
8 changes: 8 additions & 0 deletions androidx/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
tools:context=".MainActivity"
tools:ignore="MissingPrefix,HardcodedText">

<TextView
android:layout_width="wrap_content"
android:text="dasdasdads"
app:bl_text_gradient_orientation="horizontal"
app:bl_text_gradient_startColor="@color/colorAccent"
app:bl_text_gradient_endColor="@android:color/holo_green_dark"
android:layout_height="wrap_content"/>

<com.noober.background.view.BLTextView
android:layout_width="match_parent"
android:layout_height="50dp"
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ dependencies {
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
// implementation 'com.noober.background:core:1.6.5'
// implementation 'com.github.JavaNoober:BackgroundLibrary:1.6.6'
// implementation project(':library')
implementation 'com.github.JavaNoober.BackgroundLibrary:library:1.7.3'
implementation project(':library')
// implementation 'com.github.JavaNoober.BackgroundLibrary:library:1.7.3'

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
9 changes: 9 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
tools:context=".MainActivity"
tools:ignore="MissingPrefix">

<TextView
android:layout_width="wrap_content"
android:text="dasdasdads"
app:bl_text_gradient_orientation="horizontal"
app:bl_text_gradient_startColor="@color/colorAccent"
app:bl_text_gradient_endColor="@android:color/holo_green_dark"
android:layout_height="wrap_content"/>


<Button
android:id="@+id/btnConsole"
android:layout_width="match_parent"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.widget.TextView;

import com.noober.background.drawable.DrawableFactory;
import com.noober.background.drawable.TextViewFactory;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -84,6 +85,7 @@ private static View setViewBackground(String name, Context context, AttributeSet
TypedArray animTa = context.obtainStyledAttributes(attrs, R.styleable.bl_anim);
TypedArray multiSelTa = context.obtainStyledAttributes(attrs, R.styleable.background_multi_selector);
TypedArray multiTextTa = context.obtainStyledAttributes(attrs, R.styleable.background_multi_selector_text);
TypedArray textViewTa = context.obtainStyledAttributes(attrs, R.styleable.bl_text);
TypedArray selectorPre21Ta = null;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
selectorPre21Ta = context.obtainStyledAttributes(attrs, R.styleable.background_selector_pre_21);
Expand All @@ -92,7 +94,7 @@ private static View setViewBackground(String name, Context context, AttributeSet
try {
if (typedArray.getIndexCount() == 0 && selectorTa.getIndexCount() == 0 && pressTa.getIndexCount() == 0
&& textTa.getIndexCount() == 0 && buttonTa.getIndexCount() == 0 && animTa.getIndexCount() == 0
&& multiSelTa.getIndexCount() == 0 && multiTextTa.getIndexCount() == 0) {
&& multiSelTa.getIndexCount() == 0 && multiTextTa.getIndexCount() == 0 && textViewTa.getIndexCount() == 0) {
return view;
}
if (view == null) {
Expand Down Expand Up @@ -151,8 +153,11 @@ private static View setViewBackground(String name, Context context, AttributeSet
((TextView) view).setTextColor(DrawableFactory.getTextSelectorColor(textTa));
} else if (view instanceof TextView && multiTextTa.getIndexCount() > 0) {
((TextView) view).setTextColor(DrawableFactory.getMultiTextColorSelectorColorCreator(context, multiTextTa));
} else if (view instanceof TextView && textViewTa.getIndexCount() > 0) {
TextViewFactory.setTextGradientColor(context, attrs, (TextView) view);
}


if (typedArray.getBoolean(R.styleable.background_bl_ripple_enable, false) &&
typedArray.hasValue(R.styleable.background_bl_ripple_color)) {
int color = typedArray.getColor(R.styleable.background_bl_ripple_color, 0);
Expand Down Expand Up @@ -209,6 +214,7 @@ public void onClick(View view) {
animTa.recycle();
multiSelTa.recycle();
multiTextTa.recycle();
textViewTa.recycle();
if (selectorPre21Ta != null) {
selectorPre21Ta.recycle();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.noober.background.drawable;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

/**
* Author: xiaoqi
* Date: 2022/8/17 3:13 下午
* Description:
*/
public interface ITextViewOperator {
void invoke(Context context, AttributeSet attrs, TextView textView);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.noober.background.drawable;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

/**
* Author: xiaoqi
* Date: 2022/8/17 3:11 下午
* Description:
*/
public class TextViewFactory {

public static void setTextGradientColor(Context context, AttributeSet attrs, final TextView textView){
new TextViewGradientColor().invoke(context, attrs, textView);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.noober.background.drawable;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.LinearGradient;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.widget.TextView;

import com.noober.background.R;

/**
* Author: xiaoqi
* Date: 2022/8/17 3:14 下午
* Description:
*/
public class TextViewGradientColor implements ITextViewOperator{
private int endColor = -1;
private int startColor = -1;
private int orientation = 0;

@Override
public void invoke(Context context, AttributeSet attrs, final TextView textView) {
TypedArray textTa = context.obtainStyledAttributes(attrs, R.styleable.bl_text);
try {
if(textTa.getIndexCount() == 0){
return;
}
for (int i = 0; i < textTa.getIndexCount(); i++) {
int attr = textTa.getIndex(i);
if(attr == R.styleable.bl_text_bl_text_gradient_endColor){
endColor = textTa.getColor(attr, -1);
}else if(attr == R.styleable.bl_text_bl_text_gradient_startColor){
startColor = textTa.getColor(attr, -1);
}else if(attr == R.styleable.bl_text_bl_text_gradient_orientation){
orientation = textTa.getInt(attr, 0);
}
}

if(endColor == -1 && startColor != -1){
textView.setTextColor(startColor);
}else if(startColor == -1 && endColor != -1){
textView.setTextColor(endColor);
}else if(endColor != -1 && startColor != -1){
if(orientation == 0){
textView.post(new Runnable() {
@Override
public void run() {
textView.getPaint().setShader(new LinearGradient(0f, 0f, 0f, textView.getPaint().descent() - textView.getPaint().ascent(),
startColor, endColor, Shader.TileMode.REPEAT));
textView.invalidate();
}
});

}else {
textView.post(new Runnable() {
@Override
public void run() {
textView.getPaint().setShader(new LinearGradient(0, 0f, textView.getMeasuredWidth(), 0f, startColor, endColor, Shader.TileMode.REPEAT));
textView.invalidate();
}
});
}
}
}catch (Exception e){

}finally {
textTa.recycle();
}
}
}
8 changes: 8 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -452,4 +452,12 @@
<attr name="bl_frame_drawable_item14" format="reference"/>
</declare-styleable>

<declare-styleable name="bl_text">
<attr name="bl_text_gradient_startColor" format="color|reference" />
<attr name="bl_text_gradient_endColor" format="color|reference" />
<attr name="bl_text_gradient_orientation" format="enum">
<enum name="vertical" value="0" />
<enum name="horizontal" value="1" />
</attr>
</declare-styleable>
</resources>
31 changes: 18 additions & 13 deletions libraryx/src/main/java/com/noober/background/BackgroundFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.widget.TextView;

import com.noober.background.drawable.DrawableFactory;
import com.noober.background.drawable.TextViewFactory;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -85,16 +86,16 @@ private static View setViewBackground(String name, Context context, AttributeSet
TypedArray animTa = context.obtainStyledAttributes(attrs, R.styleable.bl_anim);
TypedArray multiSelTa = context.obtainStyledAttributes(attrs, R.styleable.background_multi_selector);
TypedArray multiTextTa = context.obtainStyledAttributes(attrs, R.styleable.background_multi_selector_text);

TypedArray textViewTa = context.obtainStyledAttributes(attrs, R.styleable.bl_text);
TypedArray selectorPre21Ta = null;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
selectorPre21Ta = context.obtainStyledAttributes(attrs, R.styleable.background_selector_pre_21);
}

try {
if (typedArray.getIndexCount() == 0 && selectorTa.getIndexCount() == 0 && pressTa.getIndexCount() == 0
&& textTa.getIndexCount() == 0 && buttonTa.getIndexCount() == 0 && animTa.getIndexCount() == 0
&& multiSelTa.getIndexCount() == 0 && multiTextTa.getIndexCount() == 0) {
&& textTa.getIndexCount() == 0 && buttonTa.getIndexCount() == 0 && animTa.getIndexCount() == 0
&& multiSelTa.getIndexCount() == 0 && multiTextTa.getIndexCount() == 0 && textViewTa.getIndexCount() == 0) {
return view;
}
if (view == null) {
Expand Down Expand Up @@ -153,10 +154,13 @@ private static View setViewBackground(String name, Context context, AttributeSet
((TextView) view).setTextColor(DrawableFactory.getTextSelectorColor(textTa));
} else if (view instanceof TextView && multiTextTa.getIndexCount() > 0) {
((TextView) view).setTextColor(DrawableFactory.getMultiTextColorSelectorColorCreator(context, multiTextTa));
} else if (view instanceof TextView && textViewTa.getIndexCount() > 0) {
TextViewFactory.setTextGradientColor(context, attrs, (TextView) view);
}


if (typedArray.getBoolean(R.styleable.background_bl_ripple_enable, false) &&
typedArray.hasValue(R.styleable.background_bl_ripple_color)) {
typedArray.hasValue(R.styleable.background_bl_ripple_color)) {
int color = typedArray.getColor(R.styleable.background_bl_ripple_color, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Drawable contentDrawable = (stateListDrawable == null ? drawable : stateListDrawable);
Expand All @@ -180,7 +184,7 @@ private static View setViewBackground(String name, Context context, AttributeSet
final Context currentContext = view.getContext();
final Class parentClass = currentContext.getClass();
final Method method = getMethod(parentClass, methodName);
if (method != null) {
if(method != null){
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Expand Down Expand Up @@ -211,13 +215,13 @@ public void onClick(View view) {
animTa.recycle();
multiSelTa.recycle();
multiTextTa.recycle();
textViewTa.recycle();
if (selectorPre21Ta != null) {
selectorPre21Ta.recycle();
}
}
}


private static Method getMethod(Class clazz, String methodName) {
Method method = null;
HashMap<String, Method> methodHashMap = methodMap.get(clazz.getCanonicalName());
Expand Down Expand Up @@ -376,7 +380,7 @@ private static View createView(Context context, String name, String prefix) thro
try {
if (constructor == null) {
Class<? extends View> clazz = context.getClassLoader().loadClass(
prefix != null ? (prefix + name) : name).asSubclass(View.class);
prefix != null ? (prefix + name) : name).asSubclass(View.class);

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


private static boolean hasGradientState(TypedArray typedArray) {
return typedArray.hasValue(R.styleable.background_bl_checkable_gradient_startColor) ||
typedArray.hasValue(R.styleable.background_bl_checked_gradient_startColor) ||
typedArray.hasValue(R.styleable.background_bl_enabled_gradient_startColor) ||
typedArray.hasValue(R.styleable.background_bl_selected_gradient_startColor) ||
typedArray.hasValue(R.styleable.background_bl_pressed_gradient_startColor) ||
typedArray.hasValue(R.styleable.background_bl_focused_gradient_startColor);
typedArray.hasValue(R.styleable.background_bl_checked_gradient_startColor) ||
typedArray.hasValue(R.styleable.background_bl_enabled_gradient_startColor) ||
typedArray.hasValue(R.styleable.background_bl_selected_gradient_startColor) ||
typedArray.hasValue(R.styleable.background_bl_pressed_gradient_startColor) ||
typedArray.hasValue(R.styleable.background_bl_focused_gradient_startColor);
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.noober.background.drawable;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

/**
* Author: xiaoqi
* Date: 2022/8/17 3:13 下午
* Description:
*/
public interface ITextViewOperator {
void invoke(Context context, AttributeSet attrs, TextView textView);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.noober.background.drawable;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

/**
* Author: xiaoqi
* Date: 2022/8/17 3:11 下午
* Description:
*/
public class TextViewFactory {

public static void setTextGradientColor(Context context, AttributeSet attrs, final TextView textView){
new TextViewGradientColor().invoke(context, attrs, textView);
}
}
Loading

0 comments on commit 633f81b

Please sign in to comment.