Skip to content

Commit 0e99344

Browse files
author
xiaoqi
committed
update to 1.2.4 增加文字不同状态颜色的处理
1 parent 0e4dce1 commit 0e99344

File tree

13 files changed

+300
-114
lines changed

13 files changed

+300
-114
lines changed

.idea/caches/build_file_checksums.ser

0 Bytes
Binary file not shown.

README.md

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ A framework for directly generating shape through Tags, no need to write shape.x
1010
依赖方式:
1111

1212
implementation "com.android.support:appcompat-v7:$supportVersion"
13-
implementation 'com.noober.background:core:1.2.3'
13+
implementation 'com.noober.background:core:1.2.4'
1414

1515
版本更新:
1616

1717
1.0.5 解决android 8以下Fragment中view显示问题,解决水波纹不能与press事件共存问题,优化水波纹显示
1818
1.2.0 增加对selector的支持
1919
1.2.2 修复部分属性不生效的bug
2020
1.2.3 checkbox radiobutton使用的bug
21+
1.2.4 新增了text不同状态变色的支持
2122

2223

2324
## 示例效果
@@ -84,7 +85,19 @@ A framework for directly generating shape through Tags, no need to write shape.x
8485
|unFocused_drawable|color、reference|
8586
|unFocused_hovered|color、reference|
8687
|unFocused_activated|color、reference|
87-
88+
|关于text颜色的设置||
89+
|checkable_textColor|color|
90+
|checked_textColor|color|
91+
|enabled_textColor|color|
92+
|selected_textColor|color|
93+
|pressed_textColor|color|
94+
|focused_textColor|color|
95+
|unCheckable_textColor|color|
96+
|unChecked_textColor|color|
97+
|unEnabled_textColor|color|
98+
|unSelected_textColor|color|
99+
|unPressed_textColor|color|
100+
|unFocused_textColor|color|
88101
### 其他属性
89102
| 名称 | 类型 |备注|
90103
|---|---|---|
@@ -184,6 +197,21 @@ A framework for directly generating shape through Tags, no need to write shape.x
184197
app:unPressed_drawable="#7CFC00" />
185198
186199
使用其实基本和selector shape一样。
200+
201+
3.点击文字变色
202+
![](https://user-gold-cdn.xitu.io/2018/9/19/165f131f7e85b1e7?w=289&h=61&f=gif&s=8828)
203+
204+
<Button
205+
android:layout_width="300dp"
206+
android:layout_height="50dp"
207+
android:layout_marginTop="5dp"
208+
android:gravity="center"
209+
android:padding="0dp"
210+
android:text="点击文字变色"
211+
app:pressed_textColor="#919DAF"
212+
app:unPressed_textColor="@android:color/holo_red_dark"/>
213+
214+
187215
## 简单的性能测试
188216
我用原生的方法写了例子里面的shape和selector,做了一个比较简单的测试:
189217

@@ -227,4 +255,10 @@ A framework for directly generating shape through Tags, no need to write shape.x
227255

228256
View item = LayoutInflater.from(context).inflate(xxx)
229257

230-
7、自定义View中调用了inflate,同listView一样处理即可
258+
7、自定义View中调用了inflate,同listView一样处理即可
259+
8、关于水波纹,如果光设置下面两个属性是无效的,必须还要有个填充颜色的属性,原因如下:
260+
261+
app:ripple_color="#71C671"
262+
app:ripple_enable="true"
263+
264+
水波纹需要设置一个默认背景颜色,也就是填充颜色。比如app:solid_color 或者app:unPressed_drawable 或者app:unFocussed_drawable等都行,这些都是默认等背景颜色。而app:ripple_color是波纹的颜色。如果没有一个背景色,这个波纹颜色是无法显示的

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ dependencies {
3131
testImplementation 'junit:junit:4.12'
3232
androidTestImplementation 'com.android.support.test:runner:1.0.2'
3333
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
34-
// implementation 'com.noober.background:core:1.2.2'
35-
implementation project(':library')
34+
implementation 'com.noober.background:core:1.2.4'
35+
// implementation project(':library')
3636
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.noober.backgroudlibrary;
2+
3+
import android.content.Context;
4+
import android.support.annotation.Nullable;
5+
import android.util.AttributeSet;
6+
import android.view.View;
7+
8+
/**
9+
* Created by xiaoqi on 2018/9/19
10+
*/
11+
public class TextView extends View {
12+
public TextView(Context context) {
13+
super(context);
14+
}
15+
16+
public TextView(Context context, @Nullable AttributeSet attrs) {
17+
super(context, attrs);
18+
}
19+
20+
public TextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
21+
super(context, attrs, defStyleAttr);
22+
}
23+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<selector xmlns:android="http://schemas.android.com/apk/res/android">
3+
4+
<item android:color="#919DAF" android:state_pressed="false" />
5+
<item android:color="@android:color/holo_red_dark" android:state_pressed="true" />
6+
7+
</selector>

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,16 @@
174174
app:stroke_width="2dp"
175175
app:unPressed_drawable="#7CFC00" />
176176

177+
<Button
178+
android:layout_width="300dp"
179+
android:layout_height="50dp"
180+
android:layout_marginTop="5dp"
181+
android:gravity="center"
182+
android:padding="0dp"
183+
android:text="点击文字变色"
184+
app:pressed_textColor="#919DAF"
185+
app:unPressed_textColor="@android:color/holo_red_dark"/>
186+
177187
<FrameLayout
178188
android:id="@+id/fl_content"
179189
android:layout_width="match_parent"

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ ext {
3535
userOrg = 'noober'
3636
groupId = 'com.noober.background'
3737
uploadName = 'LibraryForBackground'
38-
publishVersion = '1.2.3'
38+
publishVersion = '1.2.4'
3939
desc = "A framework for directly generating shape through Tags, no need to write shape.xml again(通过标签直接生成shape,无需再写shape.xml)"
4040
website = 'https://github.com/JavaNoober/BackgroundLibrary'
4141
// gradlew clean build bintrayUpload -PbintrayUser=xiaoqiandroid -PbintrayKey=xxxxxxxxxxxxxxxx -PdryRun=false

images/textChange.gif

8.62 KB
Loading

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import android.view.View;
1717
import android.widget.CheckBox;
1818
import android.widget.RadioButton;
19+
import android.widget.TextView;
1920

2021
import com.noober.background.drawable.DrawableFactory;
2122

@@ -45,9 +46,10 @@ public View onCreateView(String name, Context context, AttributeSet attrs) {
4546
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.background);
4647
TypedArray pressTa = context.obtainStyledAttributes(attrs, R.styleable.background_press);
4748
TypedArray selectorTa = context.obtainStyledAttributes(attrs, R.styleable.background_selector);
48-
49+
TypedArray textTa = context.obtainStyledAttributes(attrs, R.styleable.text_selector);
4950
try {
50-
if (typedArray.getIndexCount() == 0 && selectorTa.getIndexCount() == 0 && pressTa.getIndexCount() == 0) {
51+
if (typedArray.getIndexCount() == 0 && selectorTa.getIndexCount() == 0
52+
&& pressTa.getIndexCount() == 0 && textTa.getIndexCount() == 0) {
5153
return null;
5254
}
5355
if (view == null) {
@@ -60,13 +62,13 @@ public View onCreateView(String name, Context context, AttributeSet attrs) {
6062
GradientDrawable drawable = null;
6163
StateListDrawable stateListDrawable = null;
6264
if (selectorTa.getIndexCount() > 0) {
63-
stateListDrawable = DrawableFactory.getSelectorDrawable(typedArray, selectorTa);
65+
stateListDrawable = DrawableFactory.getTextSelectorColor(typedArray, selectorTa);
6466
view.setClickable(true);
65-
if(view instanceof RadioButton){
66-
((RadioButton)view).setButtonDrawable(stateListDrawable);
67-
}else if(view instanceof CheckBox){
68-
((CheckBox)view).setButtonDrawable(stateListDrawable);
69-
}else {
67+
if (view instanceof RadioButton) {
68+
((RadioButton) view).setButtonDrawable(stateListDrawable);
69+
} else if (view instanceof CheckBox) {
70+
((CheckBox) view).setButtonDrawable(stateListDrawable);
71+
} else {
7072
view.setBackground(stateListDrawable);
7173
}
7274
} else if (pressTa.getIndexCount() > 0) {
@@ -79,6 +81,10 @@ public View onCreateView(String name, Context context, AttributeSet attrs) {
7981
view.setBackground(drawable);
8082
}
8183

84+
if (view instanceof TextView && textTa.getIndexCount() > 0) {
85+
((TextView) view).setTextColor(DrawableFactory.getTextSelectorColor(textTa));
86+
}
87+
8288
if (typedArray.getBoolean(R.styleable.background_ripple_enable, false) &&
8389
typedArray.hasValue(R.styleable.background_ripple_color)) {
8490
int color = typedArray.getColor(R.styleable.background_ripple_color, 0);
@@ -104,6 +110,7 @@ public View onCreateView(String name, Context context, AttributeSet attrs) {
104110
typedArray.recycle();
105111
pressTa.recycle();
106112
selectorTa.recycle();
113+
textTa.recycle();
107114
}
108115

109116
return view;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.noober.background.drawable;
2+
3+
import android.content.res.ColorStateList;
4+
import android.content.res.TypedArray;
5+
import android.support.annotation.AttrRes;
6+
7+
import com.noober.background.R;
8+
import com.noober.background.utils.TypeValueHelper;
9+
10+
public class ColorStateCreator {
11+
12+
private TypedArray textTa;
13+
14+
public ColorStateCreator(TypedArray textTa) {
15+
this.textTa = textTa;
16+
}
17+
18+
private int[][] states = new int[][]{};
19+
private int[] colors = new int[]{};
20+
private int index;
21+
22+
public ColorStateList create() {
23+
states = new int[textTa.getIndexCount()][];
24+
colors = new int[textTa.getIndexCount()];
25+
for (int i = 0; i < textTa.getIndexCount(); i++) {
26+
int attr = TypeValueHelper.sAppearanceTextValues.get(textTa.getIndex(i), -1);
27+
if (attr == -1) {
28+
continue;
29+
}
30+
int typeIndex = textTa.getIndex(i);
31+
32+
if (attr == R.styleable.text_selector_checkable_textColor) {
33+
setStateColor(textTa, typeIndex, android.R.attr.state_checkable);
34+
} else if (attr == R.styleable.text_selector_unCheckable_textColor) {
35+
setStateColor(textTa, typeIndex, -android.R.attr.state_checkable);
36+
} else if (attr == R.styleable.text_selector_checked_textColor) {
37+
setStateColor(textTa, typeIndex, android.R.attr.state_checked);
38+
} else if (attr == R.styleable.text_selector_unChecked_textColor) {
39+
setStateColor(textTa, typeIndex, -android.R.attr.state_checked);
40+
} else if (attr == R.styleable.text_selector_enabled_textColor) {
41+
setStateColor(textTa, typeIndex, android.R.attr.state_enabled);
42+
} else if (attr == R.styleable.text_selector_unEnabled_textColor) {
43+
setStateColor(textTa, typeIndex, -android.R.attr.state_enabled);
44+
} else if (attr == R.styleable.text_selector_selected_textColor) {
45+
setStateColor(textTa, typeIndex, android.R.attr.state_selected);
46+
} else if (attr == R.styleable.text_selector_unSelected_textColor) {
47+
setStateColor(textTa, typeIndex, -android.R.attr.state_selected);
48+
} else if (attr == R.styleable.text_selector_pressed_textColor) {
49+
setStateColor(textTa, typeIndex, android.R.attr.state_pressed);
50+
} else if (attr == R.styleable.text_selector_unPressed_textColor) {
51+
setStateColor(textTa, typeIndex, -android.R.attr.state_pressed);
52+
} else if (attr == R.styleable.text_selector_focused_textColor) {
53+
setStateColor(textTa, typeIndex, android.R.attr.state_focused);
54+
} else if (attr == R.styleable.text_selector_unFocused_textColor) {
55+
setStateColor(textTa, typeIndex, -android.R.attr.state_focused);
56+
}
57+
}
58+
return new ColorStateList(states, colors);
59+
}
60+
61+
private void setStateColor(TypedArray selectorTa, int typeIndex, @AttrRes int functionId) {
62+
states[index] = new int[]{functionId};
63+
colors[index] = selectorTa.getColor(typeIndex, 0);
64+
index++;
65+
}
66+
}

library/src/main/java/com/noober/background/drawable/DrawableFactory.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.noober.background.drawable;
22

3+
import android.content.res.ColorStateList;
34
import android.content.res.TypedArray;
45
import android.graphics.drawable.GradientDrawable;
56
import android.graphics.drawable.StateListDrawable;
@@ -17,10 +18,17 @@ public static GradientDrawable getDrawable(TypedArray typedArray) throws XmlPull
1718
}
1819

1920
//获取selector属性的drawable
20-
public static StateListDrawable getSelectorDrawable(TypedArray typedArray, TypedArray selectorTa) throws Exception {
21+
public static StateListDrawable getTextSelectorColor(TypedArray typedArray, TypedArray selectorTa) throws Exception {
2122
return (StateListDrawable) new SelectorDrawableCreator(typedArray, selectorTa).create();
2223
}
2324

25+
26+
//获取selector属性关于text的color
27+
public static ColorStateList getTextSelectorColor(TypedArray textTa) {
28+
return new ColorStateCreator(textTa).create();
29+
}
30+
31+
//适配早期版本的属性
2432
public static StateListDrawable getPressDrawable(GradientDrawable drawable, TypedArray typedArray, TypedArray pressTa)
2533
throws Exception {
2634
return (StateListDrawable) new PressDrawableCreator(drawable, typedArray, pressTa).create();

library/src/main/java/com/noober/background/utils/TypeValueHelper.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class TypeValueHelper {
99
public static final SparseIntArray sAppearanceValues = new SparseIntArray();
1010
public static final SparseIntArray sAppearancePressValues = new SparseIntArray();
1111
public static final SparseIntArray sAppearanceSelectorValues = new SparseIntArray();
12+
public static final SparseIntArray sAppearanceTextValues = new SparseIntArray();
1213
static {
1314
sAppearanceValues.put(R.styleable.background_shape, R.styleable.background_shape);
1415
sAppearanceValues.put(R.styleable.background_solid_color, R.styleable.background_solid_color);
@@ -58,5 +59,19 @@ public class TypeValueHelper {
5859
sAppearanceSelectorValues.put(R.styleable.background_selector_unFocused_drawable, R.styleable.background_selector_unFocused_drawable);
5960
sAppearanceSelectorValues.put(R.styleable.background_selector_unFocused_hovered, R.styleable.background_selector_unFocused_hovered);
6061
sAppearanceSelectorValues.put(R.styleable.background_selector_unFocused_activated, R.styleable.background_selector_unFocused_activated);
62+
63+
64+
sAppearanceTextValues.put(R.styleable.text_selector_checkable_textColor, R.styleable.text_selector_checkable_textColor);
65+
sAppearanceTextValues.put(R.styleable.text_selector_checked_textColor, R.styleable.text_selector_checked_textColor);
66+
sAppearanceTextValues.put(R.styleable.text_selector_enabled_textColor, R.styleable.text_selector_enabled_textColor);
67+
sAppearanceTextValues.put(R.styleable.text_selector_selected_textColor, R.styleable.text_selector_selected_textColor);
68+
sAppearanceTextValues.put(R.styleable.text_selector_pressed_textColor, R.styleable.text_selector_pressed_textColor);
69+
sAppearanceTextValues.put(R.styleable.text_selector_focused_textColor, R.styleable.text_selector_focused_textColor);
70+
sAppearanceTextValues.put(R.styleable.text_selector_unCheckable_textColor, R.styleable.text_selector_unCheckable_textColor);
71+
sAppearanceTextValues.put(R.styleable.text_selector_unChecked_textColor, R.styleable.text_selector_unChecked_textColor);
72+
sAppearanceTextValues.put(R.styleable.text_selector_unEnabled_textColor, R.styleable.text_selector_unEnabled_textColor);
73+
sAppearanceTextValues.put(R.styleable.text_selector_unSelected_textColor, R.styleable.text_selector_unSelected_textColor);
74+
sAppearanceTextValues.put(R.styleable.text_selector_unPressed_textColor, R.styleable.text_selector_unPressed_textColor);
75+
sAppearanceTextValues.put(R.styleable.text_selector_unFocused_textColor, R.styleable.text_selector_unFocused_textColor);
6176
}
6277
}

0 commit comments

Comments
 (0)