Skip to content

Commit

Permalink
优化框架方法命名及重复代码
Browse files Browse the repository at this point in the history
新增支持设置背景边框渐变色
新增支持设置文本边框渐变色
加入对 ShapeDrawable 对象的复用机制
修复设置阴影偏移量导致阴影出现瑕疵的问题
修复在无设置填充色导致状态选择器没有生效的问题
  • Loading branch information
getActivity committed May 4, 2022
1 parent 43e6b3c commit 784e712
Show file tree
Hide file tree
Showing 47 changed files with 1,626 additions and 369 deletions.
6 changes: 4 additions & 2 deletions .github/ISSUE_TEMPLATE/issue_template_bug.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ assignees: getActivity

* 是否必现:填是/否

* 手机信息:例如某米 9 / Android 10
* 出现问题的手机信息:请填写出现问题的品牌和机型

* 出现问题的安卓版本:请填写出现问题的 Android 版本

## 请回答

Expand All @@ -34,7 +36,7 @@ assignees: getActivity

## 其他

* 提供报错堆栈(如果有报错的话必填)
* 提供报错堆栈(如果有报错的话必填,注意不要拿被混淆过的代码堆栈上来

* 提供截图或视频(根据需要提供,此项不强制)

Expand Down
16 changes: 14 additions & 2 deletions HelpDoc.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#### 常见疑问解答
## 常见疑问解答

#### 框架入侵性太大怎么办?

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

Expand All @@ -8,7 +10,17 @@

* 另外有一个需要注意的点,如果你自己单独使用 **GradientDrawable** 还是 **ShapeDrawable** 在 Java 代码动态设置的话,如果涉及到虚线或者阴影的话,经过验证在有些手机上面是无法生效的,必须要先关闭硬件加速才能生效,当然 **ShapeDrawable** 有对外开放 **intoBackground** 方法,这个方法会帮你判断是否需要关闭硬件加速。

* 另外还有人要框架做裁剪子 View 的功能?在此我表示拒绝,因为我对框架定位很明确,只是为了帮助大家少写 xml,你现在让我加一个裁剪的功能进去,这样合适吗?不,这样不合适,我个人建议裁剪子 View 可以考虑使用 Google 支持库提供的 **CardView** 来实现,有必要时可以搭配 **ShapeDrawable** 来食用。
* 现在目前关于 Shape 的框架都无法十全十美,看个人怎么抉择了,无关好与坏,在享受框架优点的同时,也要学会忍受框架的缺点。

#### 能否加入 Layout 裁剪子 View 功能?

* 在此我表示拒绝,因为我对框架定位很明确,只是为了帮助大家少写 xml,你现在让我加一个裁剪的功能进去,这样合适吗?不,这样不合适,我个人建议裁剪子 View 可以考虑使用 Google 支持库提供的 **CardView** 来实现,有必要时可以搭配 **ShapeDrawable** 来使用。

#### 能否加入 ImageView 圆角裁剪功能?

* 对于 ImageView 的 src 圆角裁剪,第一个这个属性属于 View 的内容,而框架是针对设置 View 的背景,第二个就算要做裁剪,框架的 Shape 圆角属性是设置给 View 的背景 Drawable 对象,这个时候来了 ImageView 的 src,这两个要怎么区分开?(圆角到底要应用于背景还是 src?如果背景的圆角大,而 src 圆角小 这种情况怎么做?)有人可能会说了,多加几个属性不就 OK 了?可以是可以,但是框架会复杂化,可能会导致用的人大多区分不开,并且我觉得没有太大必要,圆角的功能统一交给 Glide 来实现就可以了,框架再做一次就功能重复了。

#### 最后分享一下我的观点

* 最后我来跟大家分享一下我的观点,我认为做好一个框架并不意味着什么功能都要做,并不是我实现不了,而是有没有必要那么做,如果那样做到最后框架很可能会变成一个杂货铺,连作者都会分不清楚这个框架到底是干嘛的,比如我做[标题栏框架](https://github.com/getActivity/TitleBar)的时候,有很多人让我做沉浸式状态栏的功能,我全部给拒绝了,并建议他们单独集成沉浸式框架来实现,首先我要声明一点,我做的是标题栏框架,沉浸式状态栏是沉浸式框架应该有的功能,我要是破格做了沉浸式状态栏的功能,后面就会有人找我做沉浸式底部导航栏的功能,再后面就会有让我做一个状态栏字体变色的功能..............,如果这些我都做到了,那么请问你是否还会使用这样的框架,将标题栏和沉浸式相互捆绑的框架?到底应该叫标题栏框架还是沉浸式框架?后面如果有人说我只想用标题栏的功能不想用沉浸式的功能该怎么办?

Expand Down
116 changes: 76 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,33 @@

![](picture/demo_code.png)

![](picture/dynamic_figure.jpg)

#### 集成步骤

* 在项目根目录下的 `build.gradle` 文件中加入
* 如果你的项目 Gradle 配置是在 `7.0 以下`,需要在 `build.gradle` 文件中加入

```groovy
buildscript {
allprojects {
repositories {
// JitPack 远程仓库:https://jitpack.io
maven { url 'https://jitpack.io' }
}
}
allprojects {
```

* 如果你的 Gradle 配置是 `7.0 及以上`,则需要在 `settings.gradle` 文件中加入

```groovy
dependencyResolutionManagement {
repositories {
// JitPack 远程仓库:https://jitpack.io
maven { url 'https://jitpack.io' }
}
}
```

* 在项目 app 模块下的 `build.gradle` 文件中加入
* 配置完远程仓库后,在项目 app 模块下的 `build.gradle` 文件中加入远程依赖

```groovy
android {
Expand All @@ -38,15 +47,15 @@ android {
dependencies {
// Shape 框架:https://github.com/getActivity/ShapeView
implementation 'com.github.getActivity:ShapeView:6.2'
implementation 'com.github.getActivity:ShapeView:8.0'
}
```

#### AndroidX

* 如果项目是基于 **AndroidX** 包,请在项目 `gradle.properties` 文件中加入

```groovy
```text
# 表示将第三方库迁移到 AndroidX
android.enableJetifier = true
```
Expand Down Expand Up @@ -101,17 +110,17 @@ shapeButton.setOnClickListener(new View.OnClickListener() {
<attr name="shape_height" format="dimension" />

<!-- 填充色(默认状态) -->
<attr name="shape_solidColor" format="color|reference" />
<attr name="shape_solidColor" format="color" />
<!-- 填充色(按下状态) -->
<attr name="shape_solidPressedColor" format="color|reference" />
<attr name="shape_solidPressedColor" format="color" />
<!-- 填充色(选中状态) -->
<attr name="shape_solidCheckedColor" format="color|reference" />
<attr name="shape_solidCheckedColor" format="color" />
<!-- 填充色(禁用状态) -->
<attr name="shape_solidDisabledColor" format="color|reference" />
<attr name="shape_solidDisabledColor" format="color" />
<!-- 填充色(焦点状态) -->
<attr name="shape_solidFocusedColor" format="color|reference" />
<attr name="shape_solidFocusedColor" format="color" />
<!-- 填充色(选择状态) -->
<attr name="shape_solidSelectedColor" format="color|reference" />
<attr name="shape_solidSelectedColor" format="color" />

<!-- 圆角大小 -->
<attr name="shape_radius" format="dimension" />
Expand All @@ -130,6 +139,14 @@ shapeButton.setOnClickListener(new View.OnClickListener() {
<attr name="shape_centerColor" format="color" />
<!-- 渐变色结束颜色 -->
<attr name="shape_endColor" format="color" />

<!-- 边框渐变色起始颜色 -->
<attr name="shape_strokeStartColor" format="color" />
<!-- 边框渐变色中间颜色(可不设置) -->
<attr name="shape_strokeCenterColor" format="color" />
<!-- 边框渐变色结束颜色 -->
<attr name="shape_strokeEndColor" format="color" />

<!-- 是否将用于缩放渐变 -->
<attr name="shape_useLevel" format="boolean" />
<!-- 渐变角度(仅用于线性渐变。必须是 0-315 范围内的值,并且是 45 的倍数) -->
Expand All @@ -151,17 +168,17 @@ shapeButton.setOnClickListener(new View.OnClickListener() {
<attr name="shape_gradientRadius" format="float|fraction|dimension" />

<!-- 边框色(默认状态) -->
<attr name="shape_strokeColor" format="color|reference" />
<attr name="shape_strokeColor" format="color" />
<!-- 边框色(按下状态) -->
<attr name="shape_strokePressedColor" format="color|reference" />
<attr name="shape_strokePressedColor" format="color" />
<!-- 边框色(选中状态) -->
<attr name="shape_strokeCheckedColor" format="color|reference" />
<attr name="shape_strokeCheckedColor" format="color" />
<!-- 边框色(禁用状态) -->
<attr name="shape_strokeDisabledColor" format="color|reference" />
<attr name="shape_strokeDisabledColor" format="color" />
<!-- 边框色(焦点状态) -->
<attr name="shape_strokeFocusedColor" format="color|reference" />
<attr name="shape_strokeFocusedColor" format="color" />
<!-- 边框色(选择状态) -->
<attr name="shape_strokeSelectedColor" format="color|reference" />
<attr name="shape_strokeSelectedColor" format="color" />

<!-- 边框宽度 -->
<attr name="shape_strokeWidth" format="dimension" />
Expand All @@ -172,11 +189,11 @@ shapeButton.setOnClickListener(new View.OnClickListener() {

<!-- 内环的半径(仅在 shape="ring" 生效) -->
<attr name="shape_innerRadius" format="dimension" />
<!-- 内环的半径比率(仅在 shape="ring" 生效),计算公式:整个圆环 / innerRadiusRatio = innerRadius -->
<!-- 内环的半径比率(仅在 shape="ring" 生效) -->
<attr name="shape_innerRadiusRatio" format="float" />
<!-- 外环的厚度(仅在 shape="ring" 生效) -->
<attr name="shape_thickness" format="dimension" />
<!-- 外环的厚度比率(仅在 shape="ring" 生效),计算公式:整个圆环 / thicknessRatio = thickness -->
<!-- 外环的厚度比率(仅在 shape="ring" 生效) -->
<attr name="shape_thicknessRatio" format="float" />

<!-- 阴影大小 -->
Expand All @@ -189,17 +206,17 @@ shapeButton.setOnClickListener(new View.OnClickListener() {
<attr name="shape_shadowOffsetY" format="dimension" />

<!-- 文本色(默认状态) -->
<attr name="shape_textColor" format="color|reference" />
<attr name="shape_textColor" format="color" />
<!-- 文本色(按下状态) -->
<attr name="shape_textPressedColor" format="color|reference" />
<attr name="shape_textPressedColor" format="color" />
<!-- 文本色(选中状态) -->
<attr name="shape_textCheckedColor" format="color|reference" />
<attr name="shape_textCheckedColor" format="color" />
<!-- 文本色(禁用状态) -->
<attr name="shape_textDisabledColor" format="color|reference" />
<attr name="shape_textDisabledColor" format="color" />
<!-- 文本色(焦点状态) -->
<attr name="shape_textFocusedColor" format="color|reference" />
<attr name="shape_textFocusedColor" format="color" />
<!-- 文本色(选择状态) -->
<attr name="shape_textSelectedColor" format="color|reference" />
<attr name="shape_textSelectedColor" format="color" />

<!-- 文本渐变色起始颜色 -->
<attr name="shape_textStartColor" format="color" />
Expand All @@ -215,6 +232,11 @@ shapeButton.setOnClickListener(new View.OnClickListener() {
<enum name="vertical" value="1" />
</attr>

<!-- 文本边框颜色 -->
<attr name="shape_textStrokeColor" format="color" />
<!-- 文本边框大小 -->
<attr name="shape_textStrokeSize" format="dimension" />

<!-- CheckBox 或者 RadioButton 图标(默认状态) -->
<attr name="shape_buttonDrawable" format="reference" />
<!-- CheckBox 或者 RadioButton 图标(按下状态) -->
Expand All @@ -235,51 +257,65 @@ shapeButton.setOnClickListener(new View.OnClickListener() {

* View 的子类:ShapeView、ShapeTextView、ShapeButton、ShapeImageView、ShapeRadioButton、ShapeCheckBox、ShapeEditText

* ViewGroup 的子类:ShapeLinearLayout、ShapeFrameLayout、ShapeRelativeLayout、ShapeConstraintLayout、ShapeRecyclerView
* ViewGroup 的子类:ShapeLinearLayout、ShapeFrameLayout、ShapeRelativeLayout、ShapeConstraintLayout、ShapeRecyclerView、ShapeRadioGroup

#### [常见疑问解答](HelpDoc.md)

#### [使用案例文档](UseDemo.md)

#### 框架亮点

* 功能强大:支持设置阴影、边框渐变色、文本渐变色

* 更加便捷:无需新增 Xml 文件,直接定义控件属性即可

* 即时生效:在布局中可实时预览效果,即见所得
* 即时生效:在布局中可实时预览效果,即见所得,无需运行

* 无学习成本:控件属性和原生 Shape 命名保持一致,无需额外学习

* 覆盖范围广:几乎涵盖所有常见的 View 控件,并且控件名称无任何记忆成本

* 支持状态选择器:不仅支持设置背景色的状态选择器,还支持设置文本颜色的状态选择器

* 功能覆盖全面:不仅支持设置背景阴影色,还支持设置文本渐变色

#### 作者的其他开源项目

* 安卓技术中台:[AndroidProject](https://github.com/getActivity/AndroidProject)
* 安卓技术中台:[AndroidProject](https://github.com/getActivity/AndroidProject) ![](https://img.shields.io/github/stars/getActivity/AndroidProject.svg) ![](https://img.shields.io/github/forks/getActivity/AndroidProject.svg)

* 安卓技术中台 Kt 版:[AndroidProject-Kotlin](https://github.com/getActivity/AndroidProject-Kotlin) ![](https://img.shields.io/github/stars/getActivity/AndroidProject-Kotlin.svg) ![](https://img.shields.io/github/forks/getActivity/AndroidProject-Kotlin.svg)

* 权限框架:[XXPermissions](https://github.com/getActivity/XXPermissions) ![](https://img.shields.io/github/stars/getActivity/XXPermissions.svg) ![](https://img.shields.io/github/forks/getActivity/XXPermissions.svg)

* 吐司框架:[ToastUtils](https://github.com/getActivity/ToastUtils) ![](https://img.shields.io/github/stars/getActivity/ToastUtils.svg) ![](https://img.shields.io/github/forks/getActivity/ToastUtils.svg)

* 网络框架:[EasyHttp](https://github.com/getActivity/EasyHttp) ![](https://img.shields.io/github/stars/getActivity/EasyHttp.svg) ![](https://img.shields.io/github/forks/getActivity/EasyHttp.svg)

* 标题栏框架:[TitleBar](https://github.com/getActivity/TitleBar) ![](https://img.shields.io/github/stars/getActivity/TitleBar.svg) ![](https://img.shields.io/github/forks/getActivity/TitleBar.svg)

* 悬浮窗框架:[XToast](https://github.com/getActivity/XToast) ![](https://img.shields.io/github/stars/getActivity/XToast.svg) ![](https://img.shields.io/github/forks/getActivity/XToast.svg)

* 语种切换框架:[MultiLanguages](https://github.com/getActivity/MultiLanguages) ![](https://img.shields.io/github/stars/getActivity/MultiLanguages.svg) ![](https://img.shields.io/github/forks/getActivity/MultiLanguages.svg)

* 权限框架:[XXPermissions](https://github.com/getActivity/XXPermissions)
* Gson 解析容错:[GsonFactory](https://github.com/getActivity/GsonFactory) ![](https://img.shields.io/github/stars/getActivity/GsonFactory.svg) ![](https://img.shields.io/github/forks/getActivity/GsonFactory.svg)

* 吐司框架:[ToastUtils](https://github.com/getActivity/ToastUtils)
* 日志查看框架:[Logcat](https://github.com/getActivity/Logcat) ![](https://img.shields.io/github/stars/getActivity/Logcat.svg) ![](https://img.shields.io/github/forks/getActivity/Logcat.svg)

* 网络框架:[EasyHttp](https://github.com/getActivity/EasyHttp)
* Android 版本适配:[AndroidVersionAdapter](https://github.com/getActivity/AndroidVersionAdapter) ![](https://img.shields.io/github/stars/getActivity/AndroidVersionAdapter.svg) ![](https://img.shields.io/github/forks/getActivity/AndroidVersionAdapter.svg)

* 标题栏框架:[TitleBar](https://github.com/getActivity/TitleBar)
* Android 代码规范:[AndroidCodeStandard](https://github.com/getActivity/AndroidCodeStandard) ![](https://img.shields.io/github/stars/getActivity/AndroidCodeStandard.svg) ![](https://img.shields.io/github/forks/getActivity/AndroidCodeStandard.svg)

* 国际化框架:[MultiLanguages](https://github.com/getActivity/MultiLanguages)
* Android 开源排行榜:[AndroidGithubBoss](https://github.com/getActivity/AndroidGithubBoss) ![](https://img.shields.io/github/stars/getActivity/AndroidGithubBoss.svg) ![](https://img.shields.io/github/forks/getActivity/AndroidGithubBoss.svg)

* 悬浮窗框架:[XToast](https://github.com/getActivity/XToast)
* Studio 精品插件:[StudioPlugins](https://github.com/getActivity/StudioPlugins) ![](https://img.shields.io/github/stars/getActivity/StudioPlugins.svg) ![](https://img.shields.io/github/forks/getActivity/StudioPlugins.svg)

* Gson 解析容错:[GsonFactory](https://github.com/getActivity/GsonFactory)
* 表情包大集合:[EmojiPackage](https://github.com/getActivity/EmojiPackage) ![](https://img.shields.io/github/stars/getActivity/EmojiPackage.svg) ![](https://img.shields.io/github/forks/getActivity/EmojiPackage.svg)

* 日志查看框架:[Logcat](https://github.com/getActivity/Logcat)
* 省市区 Json 数据:[ProvinceJson](https://github.com/getActivity/ProvinceJson) ![](https://img.shields.io/github/stars/getActivity/ProvinceJson.svg) ![](https://img.shields.io/github/forks/getActivity/ProvinceJson.svg)

#### 微信公众号:Android轮子哥

![](https://raw.githubusercontent.com/getActivity/Donate/master/picture/official_ccount.png)

#### Android 技术分享 QQ 群:78797078
#### Android 技术 Q 群:10047167

#### 如果您觉得我的开源库帮你节省了大量的开发时间,请扫描下方的二维码随意打赏,要是能打赏个 10.24 :monkey_face:就太:thumbsup:了。您的支持将鼓励我继续创作:octocat:

Expand Down
Loading

0 comments on commit 784e712

Please sign in to comment.