Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Shape 支持在布局中直接定义啦,支持设置阴影,文字渐变色,状态选择器

License

NotificationsYou must be signed in to change notification settings

getActivity/ShapeView

Repository files navigation

ShapeView 框架

集成步骤

  • 如果你的项目 Gradle 配置是在7.0 以下,需要在build.gradle 文件中加入
allprojects {    repositories {// JitPack 远程仓库:https://jitpack.io        maven { url'https://jitpack.io' }    }}
  • 如果你的 Gradle 配置是7.0 及以上,则需要在settings.gradle 文件中加入
dependencyResolutionManagement {    repositories {// JitPack 远程仓库:https://jitpack.io        maven { url'https://jitpack.io' }    }}
  • 配置完远程仓库后,在项目 app 模块下的build.gradle 文件中加入远程依赖
android {// 支持 JDK 1.8    compileOptions {        targetCompatibilityJavaVersion.VERSION_1_8        sourceCompatibilityJavaVersion.VERSION_1_8    }}dependencies {// ShapeView:https://github.com/getActivity/ShapeView    implementation'com.github.getActivity:ShapeView:10.0'// ShapeDrawable:https://github.com/getActivity/ShapeDrawable    implementation'com.github.getActivity:ShapeDrawable:3.3'}

AndroidX 兼容

  • 如果项目是基于AndroidX 库,请在项目gradle.properties 文件中加入以下配置,如果已经加入则可以忽略
# 表示使用 AndroidXandroid.useAndroidX = true# 表示将第三方库迁移到 AndroidXandroid.enableJetifier = true
  • 如果项目是基于Support 库则不需要加入此配置

框架文档

  • Java 代码设置
ShapeButtonshapeButton =findViewById(R.id.btn_main_test);shapeButton.setOnClickListener(newView.OnClickListener() {@OverridepublicvoidonClick(Viewv) {shapeButton.getShapeDrawableBuilder()                .setSolidColor(0xFF000000)                .setStrokeColor(0xFF5A8DDF)// 注意:最后需要调用一下 intoBackground 方法才能生效                .intoBackground();shapeButton.getTextColorBuilder()                .setTextColor(0xFFFFFFFF)// 注意:最后需要调用一下 intoTextColor 方法才能生效                .intoTextColor();shapeButton.setText("颜色已经改变啦");    }});

布局属性大全

<resources><!-- Shape 形状(默认是矩形)-->    <attrname="shape_type"><!-- 矩形-->        <enumname="rectangle"value="0" /><!-- 椭圆形-->        <enumname="oval"value="1" /><!-- 线条-->        <enumname="line"value="2" /><!-- 圆环-->        <enumname="ring"value="3" />    </attr><!-- Shape 宽度-->    <attrname="shape_width"format="dimension" /><!-- Shape 高度-->    <attrname="shape_height"format="dimension" /><!-- 圆角大小-->    <attrname="shape_radius"format="dimension" /><!-- 左上角的圆角大小-->    <attrname="shape_radiusInTopLeft"format="dimension" />    <attrname="shape_radiusInTopStart"format="dimension" /><!-- 右上角的圆角大小-->    <attrname="shape_radiusInTopRight"format="dimension" />    <attrname="shape_radiusInTopEnd"format="dimension" /><!-- 左下角的圆角大小-->    <attrname="shape_radiusInBottomLeft"format="dimension" />    <attrname="shape_radiusInBottomStart"format="dimension" /><!-- 右下角的圆角大小-->    <attrname="shape_radiusInBottomRight"format="dimension" />    <attrname="shape_radiusInBottomEnd"format="dimension" /><!-- 填充色(默认状态)-->    <attrname="shape_solidColor"format="color" /><!-- 填充色(按下状态)-->    <attrname="shape_solidPressedColor"format="color" /><!-- 填充色(选中状态)-->    <attrname="shape_solidCheckedColor"format="color" /><!-- 填充色(禁用状态)-->    <attrname="shape_solidDisabledColor"format="color" /><!-- 填充色(焦点状态)-->    <attrname="shape_solidFocusedColor"format="color" /><!-- 填充色(选择状态)-->    <attrname="shape_solidSelectedColor"format="color" /><!-- 填充色渐变色起始颜色-->    <attrname="shape_solidGradientStartColor"format="color" /><!-- 填充色渐变色中间颜色(可不设置)-->    <attrname="shape_solidGradientCenterColor"format="color" /><!-- 填充色渐变色结束颜色-->    <attrname="shape_solidGradientEndColor"format="color" /><!-- 填充色渐变方向(仅用于线性渐变)-->    <attrname="shape_solidGradientOrientation" ><!-- 从左到右绘制渐变(0 度)-->        <enumname="leftToRight"value="0" />        <enumname="startToEnd"value="10" /><!-- 从右到左绘制渐变(180 度)-->        <enumname="rightToLeft"value="180" />        <enumname="endToStart"value="1800" /><!-- 从下到上绘制渐变(90 度)-->        <enumname="bottomToTop"value="90" /><!-- 从上到下绘制渐变(270 度)-->        <enumname="topToBottom"value="270" /><!-- 从左上角到右下角绘制渐变(315 度)-->        <enumname="topLeftToBottomRight"value="315" />        <enumname="topStartToBottomEnd"value="3150" /><!-- 从左下角到右上角绘制渐变(45 度)-->        <enumname="bottomLeftToTopRight"value="45" />        <enumname="bottomStartToTopEnd"value="450" /><!-- 从右上角到左下角绘制渐变(225 度)-->        <enumname="topRightToBottomLeft"value="225" />        <enumname="topEndToBottomStart"value="2250" /><!-- 从右下角到左上角绘制渐变(135 度)-->        <enumname="bottomRightToTopLeft"value="135" />        <enumname="bottomEndToTopStart"value="1350" />    </attr><!-- 填充色渐变类型(默认类型是线性渐变)-->    <attrname="shape_solidGradientType"><!-- 线性渐变-->        <enumname="linear"value="0" /><!-- 径向渐变-->        <enumname="radial"value="1" /><!-- 扫描渐变-->        <enumname="sweep"value="2" />    </attr><!-- 填充色渐变中心 X 点坐标的相对位置(默认值为 0.5)-->    <attrname="shape_solidGradientCenterX"format="float|fraction" /><!-- 填充色渐变中心 Y 点坐标的相对位置(默认值为 0.5)-->    <attrname="shape_solidGradientCenterY"format="float|fraction" /><!-- 填充色渐变色半径(仅用于径向渐变)-->    <attrname="shape_solidGradientRadius"format="float|fraction|dimension" /><!-- 边框色(默认状态)-->    <attrname="shape_strokeColor"format="color" /><!-- 边框色(按下状态)-->    <attrname="shape_strokePressedColor"format="color" /><!-- 边框色(选中状态)-->    <attrname="shape_strokeCheckedColor"format="color" /><!-- 边框色(禁用状态)-->    <attrname="shape_strokeDisabledColor"format="color" /><!-- 边框色(焦点状态)-->    <attrname="shape_strokeFocusedColor"format="color" /><!-- 边框色(选择状态)-->    <attrname="shape_strokeSelectedColor"format="color" /><!-- 边框色渐变色起始颜色-->    <attrname="shape_strokeGradientStartColor"format="color" /><!-- 边框渐变色中间颜色(可不设置)-->    <attrname="shape_strokeGradientCenterColor"format="color" /><!-- 边框渐变色结束颜色-->    <attrname="shape_strokeGradientEndColor"format="color" /><!-- 边框色渐变方向(仅用于线性渐变)-->    <attrname="shape_strokeGradientOrientation" ><!-- 从左到右绘制渐变(0 度)-->        <enumname="leftToRight"value="0" />        <enumname="startToEnd"value="10" /><!-- 从右到左绘制渐变(180 度)-->        <enumname="rightToLeft"value="180" />        <enumname="endToStart"value="1800" /><!-- 从下到上绘制渐变(90 度)-->        <enumname="bottomToTop"value="90" /><!-- 从上到下绘制渐变(270 度)-->        <enumname="topToBottom"value="270" /><!-- 从左上角到右下角绘制渐变(315 度)-->        <enumname="topLeftToBottomRight"value="315" />        <enumname="topStartToBottomEnd"value="3150" /><!-- 从左下角到右上角绘制渐变(45 度)-->        <enumname="bottomLeftToTopRight"value="45" />        <enumname="bottomStartToTopEnd"value="450" /><!-- 从右上角到左下角绘制渐变(225 度)-->        <enumname="topRightToBottomLeft"value="225" />        <enumname="topEndToBottomStart"value="2250" /><!-- 从右下角到左上角绘制渐变(135 度)-->        <enumname="bottomRightToTopLeft"value="135" />        <enumname="bottomEndToTopStart"value="1350" />    </attr><!-- 边框大小-->    <attrname="shape_strokeSize"format="dimension" /><!-- 边框虚线大小(为 0 就是实线,大于 0 就是虚线)-->    <attrname="shape_strokeDashSize"format="dimension" /><!-- 边框虚线间隔(虚线与虚线之间的间隔)-->    <attrname="shape_strokeDashGap"format="dimension" /><!-- 阴影大小-->    <attrname="shape_shadowSize"format="dimension" /><!-- 阴影颜色-->    <attrname="shape_shadowColor"format="color" /><!-- 阴影水平偏移-->    <attrname="shape_shadowOffsetX"format="dimension" /><!-- 阴影垂直偏移-->    <attrname="shape_shadowOffsetY"format="dimension" /><!-- 内环的半径(仅在 shape="ring" 生效)-->    <attrname="shape_ringInnerRadiusSize"format="dimension" /><!-- 内环的半径比率(仅在 shape="ring" 生效),计算公式:整个圆环 / innerRadiusRatio = innerRadius-->    <attrname="shape_ringInnerRadiusRatio"format="float" /><!-- 外环的厚度(仅在 shape="ring" 生效)-->    <attrname="shape_ringThicknessSize"format="dimension" /><!-- 外环的厚度比率(仅在 shape="ring" 生效),计算公式:整个圆环 / thicknessRatio = thickness-->    <attrname="shape_ringThicknessRatio"format="float" /><!-- 线条重心(仅在 shape="line" 生效)-->    <attrname="shape_lineGravity">        <flagname="top"value="0x30" />        <flagname="bottom"value="0x50" />        <flagname="left"value="0x03" />        <flagname="right"value="0x05" />        <flagname="start"value="0x00800003" />        <flagname="end"value="0x00800005" />        <flagname="center"value="0x11" />    </attr><!-- 文本色(默认状态)-->    <attrname="shape_textColor"format="color" /><!-- 文本色(按下状态)-->    <attrname="shape_textPressedColor"format="color" /><!-- 文本色(选中状态)-->    <attrname="shape_textCheckedColor"format="color" /><!-- 文本色(禁用状态)-->    <attrname="shape_textDisabledColor"format="color" /><!-- 文本色(焦点状态)-->    <attrname="shape_textFocusedColor"format="color" /><!-- 文本色(选择状态)-->    <attrname="shape_textSelectedColor"format="color" /><!-- 文本渐变色起始颜色-->    <attrname="shape_textStartColor"format="color" /><!-- 文本渐变色中间颜色(可不设置)-->    <attrname="shape_textCenterColor"format="color" /><!-- 文本渐变色结束颜色-->    <attrname="shape_textEndColor"format="color" /><!-- 文本渐变方向(默认类型是水平渐变)-->    <attrname="shape_textGradientOrientation"><!-- 水平渐变-->        <enumname="horizontal"value="0" /><!-- 垂直渐变-->        <enumname="vertical"value="1" />    </attr><!-- 文本边框颜色-->    <attrname="shape_textStrokeColor"format="color" /><!-- 文本边框大小-->    <attrname="shape_textStrokeSize"format="dimension" /><!-- CheckBox 或者 RadioButton 图标(默认状态)-->    <attrname="shape_buttonDrawable"format="reference" /><!-- CheckBox 或者 RadioButton 图标(按下状态)-->    <attrname="shape_buttonPressedDrawable"format="reference" /><!-- CheckBox 或者 RadioButton 图标(选中状态)-->    <attrname="shape_buttonCheckedDrawable"format="reference" /><!-- CheckBox 或者 RadioButton 图标(禁用状态)-->    <attrname="shape_buttonDisabledDrawable"format="reference" /><!-- CheckBox 或者 RadioButton 图标(焦点状态)-->    <attrname="shape_buttonFocusedDrawable"format="reference" /><!-- CheckBox 或者 RadioButton 图标(选择状态)-->    <attrname="shape_buttonSelectedDrawable"format="reference" /></resources>
  • 目前支持这些属性的控件有:

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

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

框架亮点

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

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

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

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

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

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

作者的其他开源项目

微信公众号:Android轮子哥

Android 技术 Q 群:10047167

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

License

Copyright 2021 Huang JinQunLicensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at   http://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.

Packages

No packages published

Languages


[8]ページ先頭

©2009-2026 Movatter.jp