Skip to content

Commit

Permalink
优化渐变色相关的 API 命名
Browse files Browse the repository at this point in the history
新增 ShapeRadioGroup 控件
修复背景阴影色和背景渐变色不能同时设置的问题
修复背景阴影色和背景边框色不能同时设置的问题
  • Loading branch information
getActivity committed Oct 24, 2021
1 parent 48d16c0 commit fefc314
Show file tree
Hide file tree
Showing 26 changed files with 480 additions and 109 deletions.
2 changes: 1 addition & 1 deletion HelpDoc.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
####
#### 常见疑问解答

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

Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

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

* 点击此处 [下载 Demo](ShapeView.apk) 进行演示或者测试
* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处可直接下载](ShapeView.apk)

![](picture/demo_code.png)

#### 集成步骤

Expand Down Expand Up @@ -36,7 +38,7 @@ android {
dependencies {
// Shape 框架:https://github.com/getActivity/ShapeView
implementation 'com.github.getActivity:ShapeView:6.0'
implementation 'com.github.getActivity:ShapeView:6.2'
}
```

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

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

shapeButton.setText("颜色已经改变啦");
Expand Down
Binary file modified ShapeView.apk
Binary file not shown.
2 changes: 1 addition & 1 deletion UseDemo.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
android:textSize="14sp"
app:shape="rectangle"
app:shape_radius="10dp"
app:shape_shadowColor="#AA5A8DDF"
app:shape_shadowColor="#5A8DDF"
app:shape_shadowSize="10dp"
app:shape_solidColor="#FFFFFF" />

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 60
versionName "6.0"
versionCode 62
versionName "6.2"
}

// 支持 Java JDK 8
Expand Down Expand Up @@ -58,7 +58,7 @@ dependencies {
implementation 'com.android.support:design:28.0.0'

// 标题栏框架:https://github.com/getActivity/TitleBar
implementation 'com.github.getActivity:TitleBar:9.0'
implementation 'com.github.getActivity:TitleBar:9.2'

// 内存泄漏检测:https://github.com/square/leakcanary
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.7'
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:theme="@style/AppTheme">

<activity
android:name=".MainActivity">
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
android:textSize="14sp"
app:shape="rectangle"
app:shape_radius="20dp"
app:shape_solidColor="@android:color/transparent"
app:shape_solidPressedColor="#5A8DDF"
app:shape_strokeColor="#5A8DDF"
app:shape_strokeWidth="1dp"
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
buildscript {
repositories {
// 阿里云云效仓库:https://maven.aliyun.com/mvn/guide
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'https://maven.aliyun.com/repository/public' }
maven { url 'https://maven.aliyun.com/repository/google' }
// 华为开源镜像:https://mirrors.huaweicloud.com/
maven { url 'https://repo.huaweicloud.com/repository/maven/' }
Expand All @@ -19,7 +19,7 @@ buildscript {

allprojects {
repositories {
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'https://maven.aliyun.com/repository/public' }
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://repo.huaweicloud.com/repository/maven/' }
maven { url 'https://jitpack.io' }
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 60
versionName "6.0"
versionCode 62
versionName "6.2"
}

// 支持 Java JDK 8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.support.annotation.Nullable;
import android.view.View;

import com.hjq.shape.drawable.ShapeDrawable;
Expand All @@ -19,6 +20,8 @@
*/
public final class ShapeDrawableBuilder {

private static final int NO_COLOR = Color.TRANSPARENT;

private final View mView;

private int mShape;
Expand All @@ -37,7 +40,7 @@ public final class ShapeDrawableBuilder {
private float mBottomLeftRadius;
private float mBottomRightRadius;

private int[] mGradientColor;
private int[] mGradientColors;
private boolean mUseLevel;
private int mAngle;
private int mGradientType;
Expand Down Expand Up @@ -72,21 +75,21 @@ public ShapeDrawableBuilder(View view, TypedArray typedArray, IShapeDrawableStyl
mShapeWidth = typedArray.getDimensionPixelSize(styleable.getShapeWidthStyleable(), -1);
mShapeHeight = typedArray.getDimensionPixelSize(styleable.getShapeHeightStyleable(), -1);

mSolidColor = typedArray.getColor(styleable.getSolidColorStyleable(), Color.TRANSPARENT);
mSolidColor = typedArray.getColor(styleable.getSolidColorStyleable(), NO_COLOR);
if (typedArray.hasValue(styleable.getSolidPressedColorStyleable())) {
mSolidPressedColor = typedArray.getColor(styleable.getSolidPressedColorStyleable(), Color.TRANSPARENT);
mSolidPressedColor = typedArray.getColor(styleable.getSolidPressedColorStyleable(), NO_COLOR);
}
if (styleable.getSolidCheckedColorStyleable() > 0 && typedArray.hasValue(styleable.getSolidCheckedColorStyleable())) {
mSolidCheckedColor = typedArray.getColor(styleable.getSolidCheckedColorStyleable(), Color.TRANSPARENT);
mSolidCheckedColor = typedArray.getColor(styleable.getSolidCheckedColorStyleable(), NO_COLOR);
}
if (typedArray.hasValue(styleable.getSolidDisabledColorStyleable())) {
mSolidDisabledColor = typedArray.getColor(styleable.getSolidDisabledColorStyleable(), Color.TRANSPARENT);
mSolidDisabledColor = typedArray.getColor(styleable.getSolidDisabledColorStyleable(), NO_COLOR);
}
if (typedArray.hasValue(styleable.getSolidFocusedColorStyleable())) {
mSolidFocusedColor = typedArray.getColor(styleable.getSolidFocusedColorStyleable(), Color.TRANSPARENT);
mSolidFocusedColor = typedArray.getColor(styleable.getSolidFocusedColorStyleable(), NO_COLOR);
}
if (typedArray.hasValue(styleable.getSolidSelectedColorStyleable())) {
mSolidSelectedColor = typedArray.getColor(styleable.getSolidSelectedColorStyleable(), Color.TRANSPARENT);
mSolidSelectedColor = typedArray.getColor(styleable.getSolidSelectedColorStyleable(), NO_COLOR);
}

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

if (typedArray.hasValue(styleable.getStartColorStyleable()) && typedArray.hasValue(styleable.getEndColorStyleable())) {
if (typedArray.hasValue(styleable.getCenterColorStyleable())) {
mGradientColor = new int[] {typedArray.getColor(styleable.getStartColorStyleable(), Color.TRANSPARENT),
typedArray.getColor(styleable.getCenterColorStyleable(), Color.TRANSPARENT),
typedArray.getColor(styleable.getEndColorStyleable(), Color.TRANSPARENT)};
mGradientColors = new int[] {typedArray.getColor(styleable.getStartColorStyleable(), NO_COLOR),
typedArray.getColor(styleable.getCenterColorStyleable(), NO_COLOR),
typedArray.getColor(styleable.getEndColorStyleable(), NO_COLOR)};
} else {
mGradientColor = new int[] {typedArray.getColor(styleable.getStartColorStyleable(), Color.TRANSPARENT),
typedArray.getColor(styleable.getEndColorStyleable(), Color.TRANSPARENT)};
mGradientColors = new int[] {typedArray.getColor(styleable.getStartColorStyleable(), NO_COLOR),
typedArray.getColor(styleable.getEndColorStyleable(), NO_COLOR)};
}
}

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

mStrokeColor = typedArray.getColor(styleable.getStrokeColorStyleable(), Color.TRANSPARENT);
mStrokeColor = typedArray.getColor(styleable.getStrokeColorStyleable(), NO_COLOR);
if (typedArray.hasValue(styleable.getStrokePressedColorStyleable())) {
mStrokePressedColor = typedArray.getColor(styleable.getStrokePressedColorStyleable(), Color.TRANSPARENT);
mStrokePressedColor = typedArray.getColor(styleable.getStrokePressedColorStyleable(), NO_COLOR);
}
if (styleable.getStrokeCheckedColorStyleable() > 0 && typedArray.hasValue(styleable.getStrokeCheckedColorStyleable())) {
mStrokeCheckedColor = typedArray.getColor(styleable.getStrokeCheckedColorStyleable(), Color.TRANSPARENT);
mStrokeCheckedColor = typedArray.getColor(styleable.getStrokeCheckedColorStyleable(), NO_COLOR);
}
if (typedArray.hasValue(styleable.getStrokeDisabledColorStyleable())) {
mStrokeDisabledColor = typedArray.getColor(styleable.getStrokeDisabledColorStyleable(), Color.TRANSPARENT);
mStrokeDisabledColor = typedArray.getColor(styleable.getStrokeDisabledColorStyleable(), NO_COLOR);
}
if (typedArray.hasValue(styleable.getStrokeFocusedColorStyleable())) {
mStrokeFocusedColor = typedArray.getColor(styleable.getStrokeFocusedColorStyleable(), Color.TRANSPARENT);
mStrokeFocusedColor = typedArray.getColor(styleable.getStrokeFocusedColorStyleable(), NO_COLOR);
}
if (typedArray.hasValue(styleable.getStrokeSelectedColorStyleable())) {
mStrokeSelectedColor = typedArray.getColor(styleable.getStrokeSelectedColorStyleable(), Color.TRANSPARENT);
mStrokeSelectedColor = typedArray.getColor(styleable.getStrokeSelectedColorStyleable(), NO_COLOR);
}

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

public ShapeDrawableBuilder setSolidColor(int color) {
mSolidColor = color;
clearGradientColor();
clearGradientColors();
return this;
}

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

@Nullable
public Integer getSolidPressedColor() {
return mSolidPressedColor;
}
Expand All @@ -196,6 +200,7 @@ public ShapeDrawableBuilder setSolidCheckedColor(Integer color) {
return this;
}

@Nullable
public Integer getSolidCheckedColor() {
return mSolidCheckedColor;
}
Expand All @@ -205,6 +210,7 @@ public ShapeDrawableBuilder setSolidDisabledColor(Integer color) {
return this;
}

@Nullable
public Integer getSolidDisabledColor() {
return mSolidDisabledColor;
}
Expand All @@ -214,6 +220,7 @@ public ShapeDrawableBuilder setSolidFocusedColor(Integer color) {
return this;
}

@Nullable
public Integer getSolidFocusedColor() {
return mSolidFocusedColor;
}
Expand All @@ -223,6 +230,7 @@ public ShapeDrawableBuilder setSolidSelectedColor(Integer color) {
return this;
}

@Nullable
public Integer getSolidSelectedColor() {
return mSolidSelectedColor;
}
Expand Down Expand Up @@ -255,22 +263,31 @@ public float getBottomRightRadius() {
return mBottomRightRadius;
}

public ShapeDrawableBuilder setGradientColor(int[] color) {
mGradientColor = color;
public ShapeDrawableBuilder setGradientColors(int startColor, int endColor) {
return setGradientColors(new int[]{startColor, endColor});
}

public ShapeDrawableBuilder setGradientColors(int startColor, int centerColor, int endColor) {
return setGradientColors(new int[]{startColor, centerColor, endColor});
}

public ShapeDrawableBuilder setGradientColors(int[] colors) {
mGradientColors = colors;
return this;
}

public int[] getGradientColor() {
return mGradientColor;
@Nullable
public int[] getGradientColors() {
return mGradientColors;
}

public boolean isGradientColor() {
return mGradientColor != null &&
mGradientColor.length > 0;
public boolean isGradientColors() {
return mGradientColors != null &&
mGradientColors.length > 0;
}

public void clearGradientColor() {
mGradientColor = null;
public void clearGradientColors() {
mGradientColors = null;
}

public ShapeDrawableBuilder setUseLevel(boolean useLevel) {
Expand Down Expand Up @@ -341,6 +358,7 @@ public ShapeDrawableBuilder setStrokePressedColor(Integer color) {
return this;
}

@Nullable
public Integer getStrokePressedColor() {
return mStrokePressedColor;
}
Expand All @@ -350,6 +368,7 @@ public ShapeDrawableBuilder setStrokeCheckedColor(Integer color) {
return this;
}

@Nullable
public Integer getStrokeCheckedColor() {
return mStrokeCheckedColor;
}
Expand All @@ -359,6 +378,7 @@ public ShapeDrawableBuilder setStrokeDisabledColor(Integer color) {
return this;
}

@Nullable
public Integer getStrokeDisabledColor() {
return mStrokeDisabledColor;
}
Expand All @@ -368,6 +388,7 @@ public ShapeDrawableBuilder setStrokeFocusedColor(Integer color) {
return this;
}

@Nullable
public Integer getStrokeFocusedColor() {
return mStrokeFocusedColor;
}
Expand All @@ -377,6 +398,7 @@ public ShapeDrawableBuilder setStrokeSelectedColor(Integer color) {
return this;
}

@Nullable
public Integer getStrokeSelectedColor() {
return mStrokeSelectedColor;
}
Expand Down Expand Up @@ -489,14 +511,14 @@ public int getShadowOffsetY() {
}

public Drawable buildBackgroundDrawable() {
if (!isGradientColor() && mSolidColor == Color.TRANSPARENT && mStrokeColor == Color.TRANSPARENT) {
if (!isGradientColors() && mSolidColor == NO_COLOR && mStrokeColor == NO_COLOR) {
return null;
}

ShapeDrawable defaultDrawable = createShapeDrawable(mSolidColor, mStrokeColor);
// 判断是否设置了渐变色
if (isGradientColor()) {
defaultDrawable.setGradientColors(mGradientColor);
if (isGradientColors()) {
defaultDrawable.setGradientColors(mGradientColors);
}

if (mSolidPressedColor != null && mStrokePressedColor != null &&
Expand Down Expand Up @@ -546,7 +568,7 @@ public ShapeDrawable createShapeDrawable(int solidColor, int strokeColor) {
.setSize(mShapeWidth, mShapeHeight)
.setRadius(mTopLeftRadius, mTopRightRadius, mBottomLeftRadius, mBottomRightRadius)
.setSolidColor(solidColor)
.setUseLevel(isUseLevel())
.setUseLevel(mUseLevel)
.setStroke(mStrokeWidth, strokeColor, mDashWidth, mDashGap);

drawable.setGradientAngle(mAngle)
Expand Down
Loading

0 comments on commit fefc314

Please sign in to comment.