Skip to content

Commit fefc314

Browse files
committed
优化渐变色相关的 API 命名
新增 ShapeRadioGroup 控件 修复背景阴影色和背景渐变色不能同时设置的问题 修复背景阴影色和背景边框色不能同时设置的问题
1 parent 48d16c0 commit fefc314

26 files changed

+480
-109
lines changed

HelpDoc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
####
1+
#### 常见疑问解答
22

33
* 框架上线有很多人说框架的侵入性很强?这点我必须承认,我也有看到网上也有用 `LayoutInflater.Factory` 来实现,这样入侵性很低,但是有一个致命的缺点,无法在布局中预览,这样你是不是突然就感觉不香了?入侵强当然有缺点也有优点,我不能光看它坏的一面,那样看待问题就太片面了,不过它的缺点并不是致命的,就好比你使用了一个自定义 View 叫 `XxxTextView`,这种情况下你肯定就没办法再使用 **ShapeTextView** 了,那么这种情况我们该这么办?解决方式大致分为两种:
44

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
* 博客介绍:[震惊,没想到 Shape 也可以这么写](https://www.jianshu.com/p/1288d8873440)
66

7-
* 点击此处 [下载 Demo](ShapeView.apk) 进行演示或者测试
7+
* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处可直接下载](ShapeView.apk)
8+
9+
![](picture/demo_code.png)
810

911
#### 集成步骤
1012

@@ -36,7 +38,7 @@ android {
3638
3739
dependencies {
3840
// Shape 框架:https://github.com/getActivity/ShapeView
39-
implementation 'com.github.getActivity:ShapeView:6.0'
41+
implementation 'com.github.getActivity:ShapeView:6.2'
4042
}
4143
```
4244

@@ -64,10 +66,12 @@ shapeButton.setOnClickListener(new View.OnClickListener() {
6466
shapeButton.getShapeDrawableBuilder()
6567
.setSolidColor(0xFF000000)
6668
.setStrokeColor(0xFF5A8DDF)
69+
// 最后需要调用一下 into 方法才能生效
6770
.intoBackground();
6871

6972
shapeButton.getTextColorBuilder()
7073
.setTextColor(0xFFFFFFFF)
74+
// 最后需要调用一下 into 方法才能生效
7175
.intoTextColor();
7276

7377
shapeButton.setText("颜色已经改变啦");

ShapeView.apk

2.05 KB
Binary file not shown.

UseDemo.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
android:textSize="14sp"
117117
app:shape="rectangle"
118118
app:shape_radius="10dp"
119-
app:shape_shadowColor="#AA5A8DDF"
119+
app:shape_shadowColor="#5A8DDF"
120120
app:shape_shadowSize="10dp"
121121
app:shape_solidColor="#FFFFFF" />
122122

app/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ android {
88
minSdkVersion 16
99
// noinspection ExpiredTargetSdkVersion
1010
targetSdkVersion 28
11-
versionCode 60
12-
versionName "6.0"
11+
versionCode 62
12+
versionName "6.2"
1313
}
1414

1515
// 支持 Java JDK 8
@@ -58,7 +58,7 @@ dependencies {
5858
implementation 'com.android.support:design:28.0.0'
5959

6060
// 标题栏框架:https://github.com/getActivity/TitleBar
61-
implementation 'com.github.getActivity:TitleBar:9.0'
61+
implementation 'com.github.getActivity:TitleBar:9.2'
6262

6363
// 内存泄漏检测:https://github.com/square/leakcanary
6464
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.7'

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
android:label="@string/app_name"
88
android:roundIcon="@mipmap/ic_launcher_round"
99
android:theme="@style/AppTheme">
10+
1011
<activity
11-
android:name=".MainActivity">
12+
android:name=".MainActivity"
13+
android:exported="true">
1214
<intent-filter>
1315
<action android:name="android.intent.action.MAIN" />
1416
<action android:name="android.intent.action.VIEW" />

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
android:textSize="14sp"
9393
app:shape="rectangle"
9494
app:shape_radius="20dp"
95+
app:shape_solidColor="@android:color/transparent"
9596
app:shape_solidPressedColor="#5A8DDF"
9697
app:shape_strokeColor="#5A8DDF"
9798
app:shape_strokeWidth="1dp"

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
buildscript {
22
repositories {
33
// 阿里云云效仓库:https://maven.aliyun.com/mvn/guide
4-
maven { url 'https://maven.aliyun.com/repository/jcenter' }
4+
maven { url 'https://maven.aliyun.com/repository/public' }
55
maven { url 'https://maven.aliyun.com/repository/google' }
66
// 华为开源镜像:https://mirrors.huaweicloud.com/
77
maven { url 'https://repo.huaweicloud.com/repository/maven/' }
@@ -19,7 +19,7 @@ buildscript {
1919

2020
allprojects {
2121
repositories {
22-
maven { url 'https://maven.aliyun.com/repository/jcenter' }
22+
maven { url 'https://maven.aliyun.com/repository/public' }
2323
maven { url 'https://maven.aliyun.com/repository/google' }
2424
maven { url 'https://repo.huaweicloud.com/repository/maven/' }
2525
maven { url 'https://jitpack.io' }

library/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ android {
55

66
defaultConfig {
77
minSdkVersion 16
8-
versionCode 60
9-
versionName "6.0"
8+
versionCode 62
9+
versionName "6.2"
1010
}
1111

1212
// 支持 Java JDK 8

library/src/main/java/com/hjq/shape/builder/ShapeDrawableBuilder.java

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.graphics.Color;
55
import android.graphics.drawable.Drawable;
66
import android.graphics.drawable.StateListDrawable;
7+
import android.support.annotation.Nullable;
78
import android.view.View;
89

910
import com.hjq.shape.drawable.ShapeDrawable;
@@ -19,6 +20,8 @@
1920
*/
2021
public final class ShapeDrawableBuilder {
2122

23+
private static final int NO_COLOR = Color.TRANSPARENT;
24+
2225
private final View mView;
2326

2427
private int mShape;
@@ -37,7 +40,7 @@ public final class ShapeDrawableBuilder {
3740
private float mBottomLeftRadius;
3841
private float mBottomRightRadius;
3942

40-
private int[] mGradientColor;
43+
private int[] mGradientColors;
4144
private boolean mUseLevel;
4245
private int mAngle;
4346
private int mGradientType;
@@ -72,21 +75,21 @@ public ShapeDrawableBuilder(View view, TypedArray typedArray, IShapeDrawableStyl
7275
mShapeWidth = typedArray.getDimensionPixelSize(styleable.getShapeWidthStyleable(), -1);
7376
mShapeHeight = typedArray.getDimensionPixelSize(styleable.getShapeHeightStyleable(), -1);
7477

75-
mSolidColor = typedArray.getColor(styleable.getSolidColorStyleable(), Color.TRANSPARENT);
78+
mSolidColor = typedArray.getColor(styleable.getSolidColorStyleable(), NO_COLOR);
7679
if (typedArray.hasValue(styleable.getSolidPressedColorStyleable())) {
77-
mSolidPressedColor = typedArray.getColor(styleable.getSolidPressedColorStyleable(), Color.TRANSPARENT);
80+
mSolidPressedColor = typedArray.getColor(styleable.getSolidPressedColorStyleable(), NO_COLOR);
7881
}
7982
if (styleable.getSolidCheckedColorStyleable() > 0 && typedArray.hasValue(styleable.getSolidCheckedColorStyleable())) {
80-
mSolidCheckedColor = typedArray.getColor(styleable.getSolidCheckedColorStyleable(), Color.TRANSPARENT);
83+
mSolidCheckedColor = typedArray.getColor(styleable.getSolidCheckedColorStyleable(), NO_COLOR);
8184
}
8285
if (typedArray.hasValue(styleable.getSolidDisabledColorStyleable())) {
83-
mSolidDisabledColor = typedArray.getColor(styleable.getSolidDisabledColorStyleable(), Color.TRANSPARENT);
86+
mSolidDisabledColor = typedArray.getColor(styleable.getSolidDisabledColorStyleable(), NO_COLOR);
8487
}
8588
if (typedArray.hasValue(styleable.getSolidFocusedColorStyleable())) {
86-
mSolidFocusedColor = typedArray.getColor(styleable.getSolidFocusedColorStyleable(), Color.TRANSPARENT);
89+
mSolidFocusedColor = typedArray.getColor(styleable.getSolidFocusedColorStyleable(), NO_COLOR);
8790
}
8891
if (typedArray.hasValue(styleable.getSolidSelectedColorStyleable())) {
89-
mSolidSelectedColor = typedArray.getColor(styleable.getSolidSelectedColorStyleable(), Color.TRANSPARENT);
92+
mSolidSelectedColor = typedArray.getColor(styleable.getSolidSelectedColorStyleable(), NO_COLOR);
9093
}
9194

9295
int radius = typedArray.getDimensionPixelSize(styleable.getRadiusStyleable(), 0);
@@ -97,12 +100,12 @@ public ShapeDrawableBuilder(View view, TypedArray typedArray, IShapeDrawableStyl
97100

98101
if (typedArray.hasValue(styleable.getStartColorStyleable()) && typedArray.hasValue(styleable.getEndColorStyleable())) {
99102
if (typedArray.hasValue(styleable.getCenterColorStyleable())) {
100-
mGradientColor = new int[] {typedArray.getColor(styleable.getStartColorStyleable(), Color.TRANSPARENT),
101-
typedArray.getColor(styleable.getCenterColorStyleable(), Color.TRANSPARENT),
102-
typedArray.getColor(styleable.getEndColorStyleable(), Color.TRANSPARENT)};
103+
mGradientColors = new int[] {typedArray.getColor(styleable.getStartColorStyleable(), NO_COLOR),
104+
typedArray.getColor(styleable.getCenterColorStyleable(), NO_COLOR),
105+
typedArray.getColor(styleable.getEndColorStyleable(), NO_COLOR)};
103106
} else {
104-
mGradientColor = new int[] {typedArray.getColor(styleable.getStartColorStyleable(), Color.TRANSPARENT),
105-
typedArray.getColor(styleable.getEndColorStyleable(), Color.TRANSPARENT)};
107+
mGradientColors = new int[] {typedArray.getColor(styleable.getStartColorStyleable(), NO_COLOR),
108+
typedArray.getColor(styleable.getEndColorStyleable(), NO_COLOR)};
106109
}
107110
}
108111

@@ -113,21 +116,21 @@ public ShapeDrawableBuilder(View view, TypedArray typedArray, IShapeDrawableStyl
113116
mCenterY = typedArray.getFloat(styleable.getCenterYStyleable(), 0.5f);
114117
mGradientRadius = typedArray.getDimensionPixelSize(styleable.getGradientRadiusStyleable(), radius);
115118

116-
mStrokeColor = typedArray.getColor(styleable.getStrokeColorStyleable(), Color.TRANSPARENT);
119+
mStrokeColor = typedArray.getColor(styleable.getStrokeColorStyleable(), NO_COLOR);
117120
if (typedArray.hasValue(styleable.getStrokePressedColorStyleable())) {
118-
mStrokePressedColor = typedArray.getColor(styleable.getStrokePressedColorStyleable(), Color.TRANSPARENT);
121+
mStrokePressedColor = typedArray.getColor(styleable.getStrokePressedColorStyleable(), NO_COLOR);
119122
}
120123
if (styleable.getStrokeCheckedColorStyleable() > 0 && typedArray.hasValue(styleable.getStrokeCheckedColorStyleable())) {
121-
mStrokeCheckedColor = typedArray.getColor(styleable.getStrokeCheckedColorStyleable(), Color.TRANSPARENT);
124+
mStrokeCheckedColor = typedArray.getColor(styleable.getStrokeCheckedColorStyleable(), NO_COLOR);
122125
}
123126
if (typedArray.hasValue(styleable.getStrokeDisabledColorStyleable())) {
124-
mStrokeDisabledColor = typedArray.getColor(styleable.getStrokeDisabledColorStyleable(), Color.TRANSPARENT);
127+
mStrokeDisabledColor = typedArray.getColor(styleable.getStrokeDisabledColorStyleable(), NO_COLOR);
125128
}
126129
if (typedArray.hasValue(styleable.getStrokeFocusedColorStyleable())) {
127-
mStrokeFocusedColor = typedArray.getColor(styleable.getStrokeFocusedColorStyleable(), Color.TRANSPARENT);
130+
mStrokeFocusedColor = typedArray.getColor(styleable.getStrokeFocusedColorStyleable(), NO_COLOR);
128131
}
129132
if (typedArray.hasValue(styleable.getStrokeSelectedColorStyleable())) {
130-
mStrokeSelectedColor = typedArray.getColor(styleable.getStrokeSelectedColorStyleable(), Color.TRANSPARENT);
133+
mStrokeSelectedColor = typedArray.getColor(styleable.getStrokeSelectedColorStyleable(), NO_COLOR);
131134
}
132135

133136
mStrokeWidth = typedArray.getDimensionPixelSize(styleable.getStrokeWidthStyleable(), 0);
@@ -174,7 +177,7 @@ public int getShapeHeight() {
174177

175178
public ShapeDrawableBuilder setSolidColor(int color) {
176179
mSolidColor = color;
177-
clearGradientColor();
180+
clearGradientColors();
178181
return this;
179182
}
180183

@@ -187,6 +190,7 @@ public ShapeDrawableBuilder setSolidPressedColor(Integer color) {
187190
return this;
188191
}
189192

193+
@Nullable
190194
public Integer getSolidPressedColor() {
191195
return mSolidPressedColor;
192196
}
@@ -196,6 +200,7 @@ public ShapeDrawableBuilder setSolidCheckedColor(Integer color) {
196200
return this;
197201
}
198202

203+
@Nullable
199204
public Integer getSolidCheckedColor() {
200205
return mSolidCheckedColor;
201206
}
@@ -205,6 +210,7 @@ public ShapeDrawableBuilder setSolidDisabledColor(Integer color) {
205210
return this;
206211
}
207212

213+
@Nullable
208214
public Integer getSolidDisabledColor() {
209215
return mSolidDisabledColor;
210216
}
@@ -214,6 +220,7 @@ public ShapeDrawableBuilder setSolidFocusedColor(Integer color) {
214220
return this;
215221
}
216222

223+
@Nullable
217224
public Integer getSolidFocusedColor() {
218225
return mSolidFocusedColor;
219226
}
@@ -223,6 +230,7 @@ public ShapeDrawableBuilder setSolidSelectedColor(Integer color) {
223230
return this;
224231
}
225232

233+
@Nullable
226234
public Integer getSolidSelectedColor() {
227235
return mSolidSelectedColor;
228236
}
@@ -255,22 +263,31 @@ public float getBottomRightRadius() {
255263
return mBottomRightRadius;
256264
}
257265

258-
public ShapeDrawableBuilder setGradientColor(int[] color) {
259-
mGradientColor = color;
266+
public ShapeDrawableBuilder setGradientColors(int startColor, int endColor) {
267+
return setGradientColors(new int[]{startColor, endColor});
268+
}
269+
270+
public ShapeDrawableBuilder setGradientColors(int startColor, int centerColor, int endColor) {
271+
return setGradientColors(new int[]{startColor, centerColor, endColor});
272+
}
273+
274+
public ShapeDrawableBuilder setGradientColors(int[] colors) {
275+
mGradientColors = colors;
260276
return this;
261277
}
262278

263-
public int[] getGradientColor() {
264-
return mGradientColor;
279+
@Nullable
280+
public int[] getGradientColors() {
281+
return mGradientColors;
265282
}
266283

267-
public boolean isGradientColor() {
268-
return mGradientColor != null &&
269-
mGradientColor.length > 0;
284+
public boolean isGradientColors() {
285+
return mGradientColors != null &&
286+
mGradientColors.length > 0;
270287
}
271288

272-
public void clearGradientColor() {
273-
mGradientColor = null;
289+
public void clearGradientColors() {
290+
mGradientColors = null;
274291
}
275292

276293
public ShapeDrawableBuilder setUseLevel(boolean useLevel) {
@@ -341,6 +358,7 @@ public ShapeDrawableBuilder setStrokePressedColor(Integer color) {
341358
return this;
342359
}
343360

361+
@Nullable
344362
public Integer getStrokePressedColor() {
345363
return mStrokePressedColor;
346364
}
@@ -350,6 +368,7 @@ public ShapeDrawableBuilder setStrokeCheckedColor(Integer color) {
350368
return this;
351369
}
352370

371+
@Nullable
353372
public Integer getStrokeCheckedColor() {
354373
return mStrokeCheckedColor;
355374
}
@@ -359,6 +378,7 @@ public ShapeDrawableBuilder setStrokeDisabledColor(Integer color) {
359378
return this;
360379
}
361380

381+
@Nullable
362382
public Integer getStrokeDisabledColor() {
363383
return mStrokeDisabledColor;
364384
}
@@ -368,6 +388,7 @@ public ShapeDrawableBuilder setStrokeFocusedColor(Integer color) {
368388
return this;
369389
}
370390

391+
@Nullable
371392
public Integer getStrokeFocusedColor() {
372393
return mStrokeFocusedColor;
373394
}
@@ -377,6 +398,7 @@ public ShapeDrawableBuilder setStrokeSelectedColor(Integer color) {
377398
return this;
378399
}
379400

401+
@Nullable
380402
public Integer getStrokeSelectedColor() {
381403
return mStrokeSelectedColor;
382404
}
@@ -489,14 +511,14 @@ public int getShadowOffsetY() {
489511
}
490512

491513
public Drawable buildBackgroundDrawable() {
492-
if (!isGradientColor() && mSolidColor == Color.TRANSPARENT && mStrokeColor == Color.TRANSPARENT) {
514+
if (!isGradientColors() && mSolidColor == NO_COLOR && mStrokeColor == NO_COLOR) {
493515
return null;
494516
}
495517

496518
ShapeDrawable defaultDrawable = createShapeDrawable(mSolidColor, mStrokeColor);
497519
// 判断是否设置了渐变色
498-
if (isGradientColor()) {
499-
defaultDrawable.setGradientColors(mGradientColor);
520+
if (isGradientColors()) {
521+
defaultDrawable.setGradientColors(mGradientColors);
500522
}
501523

502524
if (mSolidPressedColor != null && mStrokePressedColor != null &&
@@ -546,7 +568,7 @@ public ShapeDrawable createShapeDrawable(int solidColor, int strokeColor) {
546568
.setSize(mShapeWidth, mShapeHeight)
547569
.setRadius(mTopLeftRadius, mTopRightRadius, mBottomLeftRadius, mBottomRightRadius)
548570
.setSolidColor(solidColor)
549-
.setUseLevel(isUseLevel())
571+
.setUseLevel(mUseLevel)
550572
.setStroke(mStrokeWidth, strokeColor, mDashWidth, mDashGap);
551573

552574
drawable.setGradientAngle(mAngle)

0 commit comments

Comments
 (0)