Skip to content

Commit db944ac

Browse files
committed
1,优化支持ColorStateList
1 parent c233604 commit db944ac

File tree

8 files changed

+134
-26
lines changed

8 files changed

+134
-26
lines changed

app/src/main/java/com/flyjingfish/gradienttextview/MainActivity.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@
22

33
import androidx.appcompat.app.AppCompatActivity;
44

5+
import android.graphics.Color;
56
import android.os.Bundle;
67

8+
import com.flyjingfish.gradienttextview.databinding.ActivityMainBinding;
9+
710
public class MainActivity extends AppCompatActivity {
811

912
@Override
1013
protected void onCreate(Bundle savedInstanceState) {
1114
super.onCreate(savedInstanceState);
12-
setContentView(R.layout.activity_main);
15+
ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
16+
setContentView(binding.getRoot());
17+
binding.gradientTextView1.setOnClickListener(v -> {
18+
binding.gradientTextView1.setTextColor(Color.BLACK);
19+
});
20+
binding.gradientTextView4.setOnClickListener(v -> {});
1321
}
1422
}

app/src/main/res/color/press.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<selector xmlns:android="http://schemas.android.com/apk/res/android">
3+
<item android:color="@color/purple_200" android:state_pressed="false"/>
4+
<item android:color="@color/purple_700" android:state_pressed="true"/>
5+
</selector>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<selector xmlns:android="http://schemas.android.com/apk/res/android">
3+
<item android:color="@color/teal_200" android:state_pressed="false"/>
4+
<item android:color="@color/purple_700" android:state_pressed="true"/>
5+
</selector>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<selector xmlns:android="http://schemas.android.com/apk/res/android">
3+
<item android:color="#80eeeeee" android:state_pressed="false"/>
4+
<item android:color="#eeeeee" android:state_pressed="true"/>
5+
</selector>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<selector xmlns:android="http://schemas.android.com/apk/res/android">
3+
<item android:color="@color/purple_500" android:state_pressed="false"/>
4+
<item android:color="@color/purple_700" android:state_pressed="true"/>
5+
</selector>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<selector xmlns:android="http://schemas.android.com/apk/res/android">
3+
<item android:color="#80000000" android:state_pressed="false"/>
4+
<item android:color="#000000" android:state_pressed="true"/>
5+
</selector>

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
tools:context=".MainActivity">
99

1010
<com.flyjingfish.gradienttextviewlib.GradientTextView
11+
android:id="@+id/gradientTextView1"
1112
android:layout_width="wrap_content"
1213
android:layout_height="wrap_content"
1314
android:text="Hello World!"
@@ -45,18 +46,19 @@
4546
app:gradient_stroke_textColor="@color/purple_700" />
4647

4748
<com.flyjingfish.gradienttextviewlib.GradientTextView
49+
android:id="@+id/gradientTextView4"
4850
android:layout_width="wrap_content"
4951
android:layout_height="wrap_content"
5052
android:layout_margin="10dp"
5153
android:text="Hello World!"
5254
android:textColor="@color/white"
5355
android:textSize="30sp"
5456
app:gradient_angle="270"
55-
app:gradient_endColor="@color/white"
56-
app:gradient_startColor="@color/teal_200"
57+
app:gradient_endColor="@color/press_end"
58+
app:gradient_startColor="@color/press_start"
5759
app:gradient_stroke_angle="0"
58-
app:gradient_stroke_endColor="@color/white"
59-
app:gradient_stroke_startColor="@color/purple_700"
60+
app:gradient_stroke_startColor="@color/press_start2"
61+
app:gradient_stroke_endColor="@color/press_end2"
6062
app:gradient_stroke_strokeWidth="6dp" />
6163

6264
</LinearLayout>

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

Lines changed: 94 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@
99
import android.graphics.LinearGradient;
1010
import android.graphics.Paint;
1111
import android.graphics.Shader;
12-
import android.graphics.drawable.Drawable;
1312
import android.text.Layout;
1413
import android.text.SpannableString;
1514
import android.text.TextPaint;
1615
import android.text.style.LeadingMarginSpan;
1716
import android.util.AttributeSet;
1817
import android.util.LayoutDirection;
19-
import android.util.Log;
2018

2119
import androidx.annotation.ColorInt;
20+
import androidx.annotation.Nullable;
2221
import androidx.core.text.TextUtilsCompat;
2322

2423
import com.flyjingfish.perfecttextviewlib.PerfectTextView;
2524

2625
import java.util.ArrayList;
26+
import java.util.Arrays;
2727
import java.util.List;
2828
import java.util.Locale;
2929

@@ -148,7 +148,7 @@ protected void drawableStateChanged() {
148148
updateColors();
149149
}
150150

151-
private void updateColors(){
151+
private boolean updateColors(){
152152
boolean inval = false;
153153
final int[] drawableState = getDrawableState();
154154
int color = strokeTextColor.getColorForState(drawableState, 0);
@@ -214,28 +214,32 @@ private void updateColors(){
214214
if (inval){
215215
invalidate();
216216
}
217+
return inval;
217218
}
219+
220+
@SuppressLint("DrawAllocation")
218221
@Override
219222
protected void onDraw(Canvas canvas) {
220223
TextPaint textPaint = getPaint();
221224
Paint.Style oldStyle = textPaint.getStyle();
222225
textPaint.setStrokeWidth(strokeWidth);
223226
textPaint.setStyle(Paint.Style.FILL_AND_STROKE);
224227
textPaint.setStrokeJoin(strokeJoin);
228+
LinearGradient linearGradient;
225229
if (gradientStrokeColor){
226230
float currentAngle = strokeAngle;
227231
if (strokeRtlAngle && isRtl){
228232
currentAngle = - strokeAngle;
229233
}
230234
float[] xy = getAngleXY(currentAngle);
231235

232-
@SuppressLint("DrawAllocation") LinearGradient linearGradient = new LinearGradient(xy[0], xy[1], xy[2], xy[3], gradientStrokeColors, gradientStrokePositions, Shader.TileMode.CLAMP);
233-
textPaint.setShader(linearGradient);
236+
linearGradient = new LinearGradient(xy[0], xy[1], xy[2], xy[3], gradientStrokeColors, gradientStrokePositions, Shader.TileMode.CLAMP);
234237
}else {
235-
@SuppressLint("DrawAllocation") LinearGradient linearGradient = new LinearGradient(0, 0,getWidth(),getHeight(), new int[]{strokeTextColor,strokeTextColor}, null, Shader.TileMode.CLAMP);
236-
textPaint.setShader(linearGradient);
238+
linearGradient = new LinearGradient(0, 0,getWidth(),getHeight(), new int[]{curStrokeTextColor,curStrokeTextColor}, null, Shader.TileMode.CLAMP);
237239
}
240+
textPaint.setShader(linearGradient);
238241
super.onDraw(canvas);
242+
239243
textPaint.setStrokeWidth(0);
240244
textPaint.setStyle(oldStyle);
241245
if (gradientColor){
@@ -245,11 +249,11 @@ protected void onDraw(Canvas canvas) {
245249
}
246250
float[] xy = getAngleXY(currentAngle);
247251

248-
@SuppressLint("DrawAllocation") LinearGradient linearGradient = new LinearGradient(xy[0], xy[1], xy[2], xy[3], gradientColors, gradientPositions, Shader.TileMode.CLAMP);
249-
getPaint().setShader(linearGradient);
252+
linearGradient = new LinearGradient(xy[0], xy[1], xy[2], xy[3], gradientColors, gradientPositions, Shader.TileMode.CLAMP);
250253
}else {
251-
getPaint().setShader(null);
254+
linearGradient = null;
252255
}
256+
textPaint.setShader(linearGradient);
253257
super.onDraw(canvas);
254258

255259
}
@@ -318,13 +322,41 @@ public int[] getGradientStrokeColors() {
318322
return gradientStrokeColors;
319323
}
320324

321-
public void setGradientStrokeColors(int[] gradientStrokeColors) {
322-
this.gradientStrokeColors = gradientStrokeColors;
323-
gradientStrokeColor = gradientStrokeColors != null;
324-
if (gradientStrokePositions != null && gradientStrokeColors != null && gradientStrokeColors.length != gradientStrokePositions.length){
325-
this.gradientStrokePositions = null;
325+
public List<ColorStateList> getGradientStrokeColorStates() {
326+
return gradientStrokeColorStates;
327+
}
328+
329+
public void setGradientStrokeColors(@Nullable @ColorInt int[] gradientStrokeColors) {
330+
ColorStateList[] colorStateLists;
331+
if (gradientStrokeColors != null){
332+
colorStateLists = new ColorStateList[gradientStrokeColors.length];
333+
for (int i = 0; i < gradientStrokeColors.length; i++) {
334+
colorStateLists[i] = ColorStateList.valueOf(gradientStrokeColors[i]);
335+
}
336+
}else {
337+
colorStateLists = null;
338+
}
339+
setGradientStrokeColors(colorStateLists);
340+
}
341+
342+
public void setGradientStrokeColors(@Nullable ColorStateList[] colorStateLists) {
343+
gradientStrokeColorStates.clear();
344+
if (colorStateLists != null){
345+
gradientStrokeColorStates.addAll(Arrays.asList(colorStateLists));
346+
if (gradientStrokeColorStates.size() == 1){
347+
gradientStrokeColorStates.add(ColorStateList.valueOf(Color.TRANSPARENT));
348+
}
349+
gradientStrokeColor = gradientStrokeColorStates.size() > 0;
350+
if (gradientStrokePositions != null && gradientStrokeColorStates.size() != gradientStrokePositions.length){
351+
this.gradientStrokePositions = null;
352+
}
353+
updateColors();
354+
}else {
355+
gradientStrokeColor = false;
356+
if (!updateColors()){
357+
invalidate();
358+
}
326359
}
327-
invalidate();
328360
}
329361

330362
public float[] getGradientStrokePositions() {
@@ -341,12 +373,40 @@ public int[] getGradientColors() {
341373
return gradientColors;
342374
}
343375

344-
public void setGradientColors(int[] gradientColors) {
345-
this.gradientColors = gradientColors;
346-
if (gradientPositions != null && gradientColors != null && gradientColors.length != gradientPositions.length){
347-
this.gradientPositions = null;
376+
public List<ColorStateList> getGradientColorStates() {
377+
return gradientColorStates;
378+
}
379+
public void setGradientColors(@Nullable @ColorInt int[] gradientColors) {
380+
ColorStateList[] colorStateLists;
381+
if (gradientColors != null){
382+
colorStateLists = new ColorStateList[gradientColors.length];
383+
for (int i = 0; i < gradientColors.length; i++) {
384+
colorStateLists[i] = ColorStateList.valueOf(gradientColors[i]);
385+
}
386+
}else {
387+
colorStateLists = null;
388+
}
389+
setGradientColors(colorStateLists);
390+
}
391+
392+
public void setGradientColors(@Nullable ColorStateList[] colorStateLists) {
393+
gradientColorStates.clear();
394+
if (colorStateLists != null){
395+
gradientColorStates.addAll(Arrays.asList(colorStateLists));
396+
if (gradientColorStates.size() == 1){
397+
gradientColorStates.add(ColorStateList.valueOf(Color.TRANSPARENT));
398+
}
399+
gradientStrokeColor = gradientColorStates.size() > 0;
400+
if (gradientPositions != null && gradientColorStates.size() != gradientPositions.length){
401+
this.gradientPositions = null;
402+
}
403+
updateColors();
404+
}else {
405+
gradientStrokeColor = false;
406+
if (!updateColors()){
407+
invalidate();
408+
}
348409
}
349-
invalidate();
350410
}
351411

352412
public float[] getGradientPositions() {
@@ -415,6 +475,18 @@ public void setStrokeTextColors(ColorStateList strokeTextColor) {
415475
updateColors();
416476
}
417477

478+
@Override
479+
public void setTextColor(int color) {
480+
gradientColor = false;
481+
super.setTextColor(color);
482+
}
483+
484+
@Override
485+
public void setTextColor(ColorStateList colors) {
486+
gradientColor = false;
487+
super.setTextColor(colors);
488+
}
489+
418490
@Override
419491
public void setText(CharSequence text, BufferType type) {
420492
if (strokeWidth > 0){
@@ -428,5 +500,6 @@ public void setText(CharSequence text, BufferType type) {
428500
*/
429501
public void setStrokeJoin(Paint.Join join){
430502
strokeJoin = join;
503+
invalidate();
431504
}
432505
}

0 commit comments

Comments
 (0)