From 64b44be82e62222df1c3463bf57450b5b9b8a304 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BD=AE=E5=AD=90=E5=93=A5?= Date: Sun, 24 Jul 2022 17:33:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=20ShapeDrawable=20=E9=98=B4?= =?UTF-8?q?=E5=BD=B1=E5=8C=BA=E5=9F=9F=E5=A4=A7=E5=B0=8F=E8=AE=A1=E7=AE=97?= =?UTF-8?q?=E8=A7=84=E5=88=99=E6=9C=89=E8=AF=AF=E5=B7=AE=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20=E4=BF=AE=E5=A4=8D=E5=9C=A8=20Android=208.1=20?= =?UTF-8?q?=E5=8F=8A=E4=BB=A5=E4=B8=8B=E7=89=88=E6=9C=AC=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=97=A0=E5=9C=86=E8=A7=92=E9=98=B4=E5=BD=B1=E4=BC=9A=E5=87=BA?= =?UTF-8?q?=E7=8E=B0=E5=8F=98=E9=BB=91=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- app/build.gradle | 6 ++--- app/src/main/res/layout/activity_main.xml | 23 +++++++++++++++---- library/build.gradle | 4 ++-- .../com/hjq/shape/drawable/ShapeDrawable.java | 17 ++++++++------ .../com/hjq/shape/drawable/ShapeState.java | 5 ++++ 6 files changed, 41 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 16eb64d..d086b29 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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' } ``` diff --git a/app/build.gradle b/app/build.gradle index 4437127..9ccaf77 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,8 +8,8 @@ android { minSdkVersion 16 // noinspection ExpiredTargetSdkVersion targetSdkVersion 28 - versionCode 82 - versionName "8.2" + versionCode 83 + versionName "8.3" } // 支持 Java JDK 8 @@ -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' } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 6121a05..54f1621 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -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" @@ -224,6 +223,22 @@ app:shape_shadowSize="10dp" app:shape_solidColor="#FFFFFF" /> + + @@ -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" /> diff --git a/library/build.gradle b/library/build.gradle index 242e6e6..ae93603 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -5,8 +5,8 @@ android { defaultConfig { minSdkVersion 16 - versionCode 82 - versionName "8.2" + versionCode 83 + versionName "8.3" } // 支持 Java JDK 8 diff --git a/library/src/main/java/com/hjq/shape/drawable/ShapeDrawable.java b/library/src/main/java/com/hjq/shape/drawable/ShapeDrawable.java index aa9c970..5c3d7ac 100644 --- a/library/src/main/java/com/hjq/shape/drawable/ShapeDrawable.java +++ b/library/src/main/java/com/hjq/shape/drawable/ShapeDrawable.java @@ -319,6 +319,7 @@ public ShapeDrawable setGradientOrientation(ShapeGradientOrientation orientation public ShapeDrawable setShadowColor(int color) { mShapeState.setShadowColor(color); mPathIsDirty = true; + mRectIsDirty = true; invalidateSelf(); return this; } @@ -329,6 +330,7 @@ public ShapeDrawable setShadowColor(int color) { public ShapeDrawable setShadowSize(int size) { mShapeState.setShadowSize(size); mPathIsDirty = true; + mRectIsDirty = true; invalidateSelf(); return this; } @@ -339,6 +341,7 @@ public ShapeDrawable setShadowSize(int size) { public ShapeDrawable setShadowOffsetX(int offsetX) { mShapeState.setShadowOffsetX(offsetX); mPathIsDirty = true; + mRectIsDirty = true; invalidateSelf(); return this; } @@ -349,6 +352,7 @@ public ShapeDrawable setShadowOffsetX(int offsetX) { public ShapeDrawable setShadowOffsetY(int offsetY) { mShapeState.setShadowOffsetY(offsetY); mPathIsDirty = true; + mRectIsDirty = true; invalidateSelf(); return this; } @@ -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) { @@ -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); diff --git a/library/src/main/java/com/hjq/shape/drawable/ShapeState.java b/library/src/main/java/com/hjq/shape/drawable/ShapeState.java index 593074c..1d5fb51 100644 --- a/library/src/main/java/com/hjq/shape/drawable/ShapeState.java +++ b/library/src/main/java/com/hjq/shape/drawable/ShapeState.java @@ -172,6 +172,11 @@ private void computeOpacity() { return; } + if (mShadowSize > 0) { + mOpaque = false; + return; + } + if (mStrokeWidth > 0 && !isOpaque(mStrokeColor)) { mOpaque = false; return;