Skip to content

Commit

Permalink
修正 ShapeDrawable 阴影区域大小计算规则有误差的问题
Browse files Browse the repository at this point in the history
修复在 Android 8.1 及以下版本使用无圆角阴影会出现变黑的问题
  • Loading branch information
getActivity committed Jul 24, 2022
1 parent a899bc1 commit 64b44be
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 18 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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

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

![](picture/demo_code.png)

Expand Down Expand Up @@ -47,7 +47,7 @@ android {
dependencies {
// Shape 框架:https://github.com/getActivity/ShapeView
implementation 'com.github.getActivity:ShapeView:8.2'
implementation 'com.github.getActivity:ShapeView:8.3'
}
```

Expand Down
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
minSdkVersion 16
// noinspection ExpiredTargetSdkVersion
targetSdkVersion 28
versionCode 82
versionName "8.2"
versionCode 83
versionName "8.3"
}

// 支持 Java JDK 8
Expand Down Expand Up @@ -63,5 +63,5 @@ dependencies {
implementation 'com.github.getActivity:TitleBar:9.5'

// 内存泄漏检测:https://github.com/square/leakcanary
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.8.1'
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.9.1'
}
23 changes: 19 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:clickable="true"
android:gravity="center"
android:paddingTop="30dp"
android:paddingBottom="30dp"
Expand All @@ -224,6 +223,22 @@
app:shape_shadowSize="10dp"
app:shape_solidColor="#FFFFFF" />

<com.hjq.shape.view.ShapeTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:paddingTop="30dp"
android:paddingBottom="30dp"
android:text="直角阴影效果"
android:textColor="@android:color/black"
android:textSize="14sp"
app:shape="rectangle"
app:shape_radius="1px"
app:shape_shadowColor="#20000000"
app:shape_shadowSize="10dp"
app:shape_solidColor="#FFFFFF" />

<com.hjq.shape.view.ShapeTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down Expand Up @@ -376,9 +391,9 @@
android:layout_margin="10dp"
android:gravity="center"
android:padding="10dp"
app:shape_textColor="#5A8DDF"
android:text="文本边框颜色效果"
android:textSize="18sp"
app:shape_textColor="#5A8DDF"
app:shape_textEndColor="#f08833"
app:shape_textGradientOrientation="horizontal"
app:shape_textStartColor="#fefa54"
Expand Down Expand Up @@ -932,8 +947,8 @@
android:text="线在字的左边"
android:textColor="@android:color/black"
android:textSize="14sp"
app:shape_lineGravity="start"
app:shape="line"
app:shape_lineGravity="start"
app:shape_strokeColor="#ffff0000"
app:shape_strokeWidth="2dp"/>

Expand All @@ -946,8 +961,8 @@
android:text="改变线的大小和颜色"
android:textColor="@android:color/black"
android:textSize="14sp"
app:shape_lineGravity="bottom"
app:shape="line"
app:shape_lineGravity="bottom"
app:shape_strokeColor="#ffff00ff"
app:shape_strokeWidth="3dp" />

Expand Down
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ android {

defaultConfig {
minSdkVersion 16
versionCode 82
versionName "8.2"
versionCode 83
versionName "8.3"
}

// 支持 Java JDK 8
Expand Down
17 changes: 10 additions & 7 deletions library/src/main/java/com/hjq/shape/drawable/ShapeDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ public ShapeDrawable setGradientOrientation(ShapeGradientOrientation orientation
public ShapeDrawable setShadowColor(int color) {
mShapeState.setShadowColor(color);
mPathIsDirty = true;
mRectIsDirty = true;
invalidateSelf();
return this;
}
Expand All @@ -329,6 +330,7 @@ public ShapeDrawable setShadowColor(int color) {
public ShapeDrawable setShadowSize(int size) {
mShapeState.setShadowSize(size);
mPathIsDirty = true;
mRectIsDirty = true;
invalidateSelf();
return this;
}
Expand All @@ -339,6 +341,7 @@ public ShapeDrawable setShadowSize(int size) {
public ShapeDrawable setShadowOffsetX(int offsetX) {
mShapeState.setShadowOffsetX(offsetX);
mPathIsDirty = true;
mRectIsDirty = true;
invalidateSelf();
return this;
}
Expand All @@ -349,6 +352,7 @@ public ShapeDrawable setShadowOffsetX(int offsetX) {
public ShapeDrawable setShadowOffsetY(int offsetY) {
mShapeState.setShadowOffsetY(offsetY);
mPathIsDirty = true;
mRectIsDirty = true;
invalidateSelf();
return this;
}
Expand Down Expand Up @@ -516,7 +520,8 @@ of the fill (if any) without worrying about blending artifacts.
}

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

} else {
if (mShadowPaint != null) {
Expand Down Expand Up @@ -796,12 +801,10 @@ private boolean ensureValidRect() {

final ShapeState st = mShapeState;

float shadowScale = 1.2f;

float let = bounds.left + inset + mShapeState.mShadowSize * shadowScale;
float top = bounds.top + inset + mShapeState.mShadowSize * shadowScale;
float right = bounds.right - inset - mShapeState.mShadowSize * shadowScale;
float bottom = bounds.bottom - inset - mShapeState.mShadowSize * shadowScale;
float let = bounds.left + inset + mShapeState.mShadowSize;
float top = bounds.top + inset + mShapeState.mShadowSize;
float right = bounds.right - inset - mShapeState.mShadowSize;
float bottom = bounds.bottom - inset - mShapeState.mShadowSize;

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

Expand Down
5 changes: 5 additions & 0 deletions library/src/main/java/com/hjq/shape/drawable/ShapeState.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ private void computeOpacity() {
return;
}

if (mShadowSize > 0) {
mOpaque = false;
return;
}

if (mStrokeWidth > 0 && !isOpaque(mStrokeColor)) {
mOpaque = false;
return;
Expand Down

0 comments on commit 64b44be

Please sign in to comment.