Skip to content

Commit 64b44be

Browse files
committed
修正 ShapeDrawable 阴影区域大小计算规则有误差的问题
修复在 Android 8.1 及以下版本使用无圆角阴影会出现变黑的问题
1 parent a899bc1 commit 64b44be

6 files changed

Lines changed: 41 additions & 18 deletions

File tree

README.md

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

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

7-
* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处可直接下载](https://github.com/getActivity/ShapeView/releases/download/8.2/ShapeView.apk)
7+
* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处可直接下载](https://github.com/getActivity/ShapeView/releases/download/8.3/ShapeView.apk)
88

99
![](picture/demo_code.png)
1010

@@ -47,7 +47,7 @@ android {
4747
4848
dependencies {
4949
// Shape 框架:https://github.com/getActivity/ShapeView
50-
implementation 'com.github.getActivity:ShapeView:8.2'
50+
implementation 'com.github.getActivity:ShapeView:8.3'
5151
}
5252
```
5353

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 82
12-
versionName "8.2"
11+
versionCode 83
12+
versionName "8.3"
1313
}
1414

1515
// 支持 Java JDK 8
@@ -63,5 +63,5 @@ dependencies {
6363
implementation 'com.github.getActivity:TitleBar:9.5'
6464

6565
// 内存泄漏检测:https://github.com/square/leakcanary
66-
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.8.1'
66+
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.9.1'
6767
}

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@
211211
android:layout_width="match_parent"
212212
android:layout_height="wrap_content"
213213
android:layout_margin="10dp"
214-
android:clickable="true"
215214
android:gravity="center"
216215
android:paddingTop="30dp"
217216
android:paddingBottom="30dp"
@@ -224,6 +223,22 @@
224223
app:shape_shadowSize="10dp"
225224
app:shape_solidColor="#FFFFFF" />
226225

226+
<com.hjq.shape.view.ShapeTextView
227+
android:layout_width="match_parent"
228+
android:layout_height="wrap_content"
229+
android:layout_margin="10dp"
230+
android:gravity="center"
231+
android:paddingTop="30dp"
232+
android:paddingBottom="30dp"
233+
android:text="直角阴影效果"
234+
android:textColor="@android:color/black"
235+
android:textSize="14sp"
236+
app:shape="rectangle"
237+
app:shape_radius="1px"
238+
app:shape_shadowColor="#20000000"
239+
app:shape_shadowSize="10dp"
240+
app:shape_solidColor="#FFFFFF" />
241+
227242
<com.hjq.shape.view.ShapeTextView
228243
android:layout_width="match_parent"
229244
android:layout_height="wrap_content"
@@ -376,9 +391,9 @@
376391
android:layout_margin="10dp"
377392
android:gravity="center"
378393
android:padding="10dp"
379-
app:shape_textColor="#5A8DDF"
380394
android:text="文本边框颜色效果"
381395
android:textSize="18sp"
396+
app:shape_textColor="#5A8DDF"
382397
app:shape_textEndColor="#f08833"
383398
app:shape_textGradientOrientation="horizontal"
384399
app:shape_textStartColor="#fefa54"
@@ -932,8 +947,8 @@
932947
android:text="线在字的左边"
933948
android:textColor="@android:color/black"
934949
android:textSize="14sp"
935-
app:shape_lineGravity="start"
936950
app:shape="line"
951+
app:shape_lineGravity="start"
937952
app:shape_strokeColor="#ffff0000"
938953
app:shape_strokeWidth="2dp"/>
939954

@@ -946,8 +961,8 @@
946961
android:text="改变线的大小和颜色"
947962
android:textColor="@android:color/black"
948963
android:textSize="14sp"
949-
app:shape_lineGravity="bottom"
950964
app:shape="line"
965+
app:shape_lineGravity="bottom"
951966
app:shape_strokeColor="#ffff00ff"
952967
app:shape_strokeWidth="3dp" />
953968

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 82
9-
versionName "8.2"
8+
versionCode 83
9+
versionName "8.3"
1010
}
1111

1212
// 支持 Java JDK 8

library/src/main/java/com/hjq/shape/drawable/ShapeDrawable.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ public ShapeDrawable setGradientOrientation(ShapeGradientOrientation orientation
319319
public ShapeDrawable setShadowColor(int color) {
320320
mShapeState.setShadowColor(color);
321321
mPathIsDirty = true;
322+
mRectIsDirty = true;
322323
invalidateSelf();
323324
return this;
324325
}
@@ -329,6 +330,7 @@ public ShapeDrawable setShadowColor(int color) {
329330
public ShapeDrawable setShadowSize(int size) {
330331
mShapeState.setShadowSize(size);
331332
mPathIsDirty = true;
333+
mRectIsDirty = true;
332334
invalidateSelf();
333335
return this;
334336
}
@@ -339,6 +341,7 @@ public ShapeDrawable setShadowSize(int size) {
339341
public ShapeDrawable setShadowOffsetX(int offsetX) {
340342
mShapeState.setShadowOffsetX(offsetX);
341343
mPathIsDirty = true;
344+
mRectIsDirty = true;
342345
invalidateSelf();
343346
return this;
344347
}
@@ -349,6 +352,7 @@ public ShapeDrawable setShadowOffsetX(int offsetX) {
349352
public ShapeDrawable setShadowOffsetY(int offsetY) {
350353
mShapeState.setShadowOffsetY(offsetY);
351354
mPathIsDirty = true;
355+
mRectIsDirty = true;
352356
invalidateSelf();
353357
return this;
354358
}
@@ -516,7 +520,8 @@ of the fill (if any) without worrying about blending artifacts.
516520
}
517521

518522
mShadowPaint.setColor(shadowColor);
519-
mShadowPaint.setMaskFilter(new BlurMaskFilter(mShapeState.mShadowSize, BlurMaskFilter.Blur.NORMAL));
523+
// 这里解释一下为什么要阴影大小除以 1.2f,因为如果不这么做会导致阴影显示会超过 View 边界,从而导致出现阴影被截断的效果
524+
mShadowPaint.setMaskFilter(new BlurMaskFilter(mShapeState.mShadowSize / 1.2f, BlurMaskFilter.Blur.NORMAL));
520525

521526
} else {
522527
if (mShadowPaint != null) {
@@ -796,12 +801,10 @@ private boolean ensureValidRect() {
796801

797802
final ShapeState st = mShapeState;
798803

799-
float shadowScale = 1.2f;
800-
801-
float let = bounds.left + inset + mShapeState.mShadowSize * shadowScale;
802-
float top = bounds.top + inset + mShapeState.mShadowSize * shadowScale;
803-
float right = bounds.right - inset - mShapeState.mShadowSize * shadowScale;
804-
float bottom = bounds.bottom - inset - mShapeState.mShadowSize * shadowScale;
804+
float let = bounds.left + inset + mShapeState.mShadowSize;
805+
float top = bounds.top + inset + mShapeState.mShadowSize;
806+
float right = bounds.right - inset - mShapeState.mShadowSize;
807+
float bottom = bounds.bottom - inset - mShapeState.mShadowSize;
805808

806809
mRect.set(let, top, right, bottom);
807810

library/src/main/java/com/hjq/shape/drawable/ShapeState.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ private void computeOpacity() {
172172
return;
173173
}
174174

175+
if (mShadowSize > 0) {
176+
mOpaque = false;
177+
return;
178+
}
179+
175180
if (mStrokeWidth > 0 && !isOpaque(mStrokeColor)) {
176181
mOpaque = false;
177182
return;

0 commit comments

Comments
 (0)