Android分享一个开源项目ShapeView

从效果图中可以看出,覆盖了App中常用的按钮效果,弧度、按压效果、标签选择、按钮渐变色等。如果用shape.xml实现的话,项目drawable目录下如图所示:



你会发现需要写很多shape文件,很难管理。如果使用自定义属性的话,有以下优点:
1.减少项目中95%以上的shape文件,还能减少安装包体积
2.一键复制,增加开发效率
3.代码灵活修改Shape属性,例如按钮弧度,渐变色等

Shape框架如何使用?
使用Android Studio开发的童鞋在线依赖,目前最新版本是1.1.1:

implementation 'com.ansen.shape:shape:1.1.1'

目前支持以下自定义属性,满足大部分需求

app:solid_color="#FF00FF" 填充颜色
app:select_solid_color="#FFFFFFFF" 选中填充颜色
app:pressed_solid_color="#FFDEE0E2" 按压填充颜色

app:corners_radius="5dp"  弧度

app:stroke_color="#00ff00" 边框颜色
app:select_stroke_color="#ffbababa" 选中情况边框色
app:stroke_width="2dp"  边框宽度
app:select_stroke_width="0.5dp" 选中情况边框宽度

app:top_left_radius="15dp" 左上弧度
app:top_right_radius="15dp" 右上弧度
app:bottom_left_radius="15dp" 左下弧度
app:bottom_right_radius="15dp" 右下弧度
app:shape_view="oval" View形状
app:color_orientation="top_bottom" 颜色渐变色方向(背景/边框/文字)

app:start_color="#5BC9FF" 填充渐变色开始
app:center_color="#FF00FF" 填充渐变色中间
app:end_color="#4669F6" 填充渐变色结束

app:select_start_color="#EB89FF" 渐变色选中开始色
app:select_center_color="#FCCE5F" 渐变色选中中间色
app:select_end_color="#FCCE5F" 渐变色选中结束色

按压渐变色
app:pressed_start_color="#983458"
app:pressed_center_color="#98633F"
app:pressed_end_color="#98633F"

<!--   TextView/EditView控件独有属性     -->
app:text="点击我切换选中效果"
app:select_text="选中时显示的文字"
app:border_gradient="true/false" 边框渐变
app:text_gradient="true/false" 文字渐变

app:text_color="#FFFFFFFF" 文本未选中颜色
app:select_text_color="#BBBBBB" 文本选中颜色

app:unselect_drawable="@mipmap/icon_sex_select_male" 图片未选中
app:select_drawable="@mipmap/icon_sex_select_woman" 图片选中
app:drawable_direction="right" 图片显示TextView方向,left:左边 top:上面 right:右边 bottom:底部

首先需要在xml根布局控件增加一个属性:

xmlns:app="http://schemas.android.com/apk/res-auto"

例如给TextView设置弧度、填充颜色、边框颜色、边框宽度,代码如下所示:

<com.ansen.shape.AnsenTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="10dp"
android:text="Hello World!"
android:textColor="#FFFFFF"
app:corners_radius="5dp"
app:solid_color="#FF00FF"
app:stroke_color="#00ff00"
app:stroke_width="2dp" />

完整xml代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#F7F7F7"
    android:scrollbars="none">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <com.ansen.shape.AnsenTextView
                android:id="@+id/tv_one"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:onClick="onClick"
                android:padding="10dp"
                android:text="边框+弧度+填充色"
                android:textColor="#FFFFFF"
                app:corners_radius="5dp"
                app:solid_color="#FF00FF"
                app:stroke_color="#00ff00"
                app:stroke_direction="bottom|top"
                app:stroke_width="2dp" />

            <com.ansen.shape.AnsenTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:padding="10dp"
                android:text="左上+右上弧度"
                android:textColor="#FFFFFF"
                app:solid_color="#00FFFF"
                app:top_left_radius="15dp"
                app:top_right_radius="15dp" />

            <com.ansen.shape.AnsenTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:padding="10dp"
                android:text="左下+右下弧度"
                android:textColor="#FFFFFF"
                app:bottom_left_radius="15dp"
                app:bottom_right_radius="15dp"
                app:solid_color="#FF00FF" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <com.ansen.shape.AnsenTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:padding="10dp"
                android:text="椭圆形"
                android:textColor="#FFFFFF"
                app:shape_view="oval"
                app:solid_color="#FF00FF" />

            <com.ansen.shape.AnsenTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:padding="20dp"
                android:text="颜色从上到下"
                android:textColor="#FFFFFF"
                app:center_color="#FF00FF"
                app:color_orientation="top_bottom"
                app:end_color="#4669F6"
                app:start_color="#5BC9FF" />

            <com.ansen.shape.AnsenTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:padding="20dp"
                android:text="颜色从左到右"
                android:textColor="#FFFFFF"
                app:center_color="#FF00FF"
                app:color_orientation="left_right"
                app:corners_radius="5dp"
                app:end_color="#4669F6"
                app:start_color="#5BC9FF" />
        </LinearLayout>

        <com.ansen.shape.AnsenLinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:orientation="horizontal"
            android:padding="5dp"
            app:corners_radius="15dp"
            app:solid_color="#666666">

            <com.ansen.shape.AnsenView
                android:layout_width="30dp"
                android:layout_height="41dp"
                android:layout_marginLeft="10dp"
                app:bottom_right_radius="50dp"
                app:color_orientation="top_bottom"
                app:end_color="#F64848"
                app:start_color="#FF8B59"
                app:top_right_radius="50dp" />

            <com.ansen.shape.AnsenTextView
                android:id="@+id/tv_dynamic_alteration"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:padding="5dp"
                android:text="点击我通过代码改变View Shape属性"
                android:textColor="#FFFFFF"
                app:color_orientation="top_bottom"
                app:corners_radius="15dp"
                app:end_color="#F64848"
                app:start_color="#FF8B5A" />
        </com.ansen.shape.AnsenLinearLayout>

        <com.ansen.shape.AnsenEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginRight="10dp"
            android:hint="请输入密码"
            android:paddingLeft="10dp"
            app:corners_radius="5dp"
            app:solid_color="#f5f5f5" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:orientation="horizontal">

            <com.ansen.shape.AnsenTextView
                android:id="@+id/tv_change_select"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawablePadding="10dp"
                android:onClick="onClick"

                android:paddingLeft="10dp"
                android:paddingTop="10dp"
                android:paddingRight="10dp"

                android:paddingBottom="10dp"
                android:text="点击我切换选中效果"
                android:textColor="#FFFFFFFF"
                app:color_orientation="right_left"

                app:corners_radius="30dp"
                app:end_color="#B26BFF"
                app:select_drawable="@mipmap/icon_sex_select_woman"
                app:select_solid_color="#FFFFFFFF"

                app:select_stroke_color="#ffbababa"
                app:select_stroke_width="0.5dp"

                app:select_text_color="#BBBBBB"
                app:start_color="#FF88D9"
                app:stroke_width="0dp"
                app:text_color="#FFFFFFFF"
                app:unselect_drawable="@mipmap/icon_sex_select_male" />

            <com.ansen.shape.AnsenTextView
                android:id="@+id/tv_change_select_two"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:drawablePadding="10dp"
                android:onClick="onClick"
                android:paddingLeft="10dp"

                android:paddingTop="10dp"
                android:paddingRight="10dp"
                android:paddingBottom="10dp"

                android:textColor="#FFFFFFFF"
                app:color_orientation="right_left"
                app:corners_radius="30dp"
                app:end_color="#B26BFF"

                app:select_drawable="@mipmap/icon_sex_select_woman"
                app:select_end_color="#FCCE5F"
                app:select_start_color="#EB89FF"

                app:select_text="选中时显示的文字"
                app:select_text_color="#5B3B28"

                app:start_color="#FF88D9"
                app:stroke_width="0dp"
                app:text="点击我切换选中效果"
                app:text_color="#FFFFFFFF"
                app:unselect_drawable="@mipmap/icon_sex_select_male" />
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginRight="10dp"
            android:background="#1D1A2A" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:orientation="horizontal"
            android:paddingTop="10dp"
            android:paddingBottom="10dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="20dp"
                android:text="请选择性别" />

            <com.ansen.shape.AnsenTextView
                android:id="@+id/tv_male"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawablePadding="10dp"
                android:onClick="onClick"
                android:padding="10dp"
                android:text="男"
                app:drawable_direction="right"
                app:select_drawable="@mipmap/icon_sex_select_woman"
                app:select_text_color="#EE398E"
                app:text_color="#72C7F6"
                app:unselect_drawable="@mipmap/icon_sex_select_male" />

            <com.ansen.shape.AnsenTextView
                android:id="@+id/tv_woman"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:drawablePadding="10dp"
                android:onClick="onClick"
                android:padding="10dp"
                android:text="女"
                app:select_drawable="@mipmap/icon_sex_select_woman"
                app:select_text_color="#EE398E"
                app:text_color="#72C7F6"
                app:unselect_drawable="@mipmap/icon_sex_select_male" />
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:background="#1D1A2A" />

        <!--    com.ansen.shape.Ansen-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <com.ansen.shape.AnsenTextView
                android:id="@+id/tv_press_one"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginLeft="15dp"
                android:layout_weight="1"
                android:gravity="center"
                android:onClick="onClick"
                android:paddingTop="15dp"
                android:paddingBottom="15dp"
                android:text="点击我看按压效果"
                app:corners_radius="20dp"
                app:pressed_solid_color="#FFDEE0E2"
                app:solid_color="#ffffffff" />

            <com.ansen.shape.AnsenTextView
                android:id="@+id/tv_press_two"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginLeft="15dp"
                android:layout_weight="1"
                android:gravity="center"
                android:onClick="onClick"
                android:paddingTop="15dp"
                android:paddingBottom="15dp"
                android:text="点击我看按压效果"
                android:textColor="@color/white"
                app:corners_radius="20dp"
                app:end_color="#FEA967"
                app:pressed_end_color="#98633F"
                app:pressed_start_color="#983458"
                app:start_color="#FE5295" />
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:background="#1D1A2A" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:orientation="horizontal">

            <com.ansen.shape.AnsenTextView
                android:id="@+id/tv_tag"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="20dp"
                android:paddingTop="6dp"
                android:paddingRight="20dp"
                android:paddingBottom="6dp"
                android:text="标签效果"
                android:textColor="#7ED034"
                app:bottom_left_radius="3dp"
                app:bottom_right_radius="20dp"
                app:stroke_color="#7ED034"
                app:stroke_width="0.5dp"
                app:top_left_radius="20dp"
                app:top_right_radius="3dp" />

            <com.ansen.shape.AnsenLinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_above="@+id/svga"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:orientation="horizontal"
                app:corners_radius="30dp"
                app:solid_color="#4D000000">

                <com.ansen.shape.AnsenLinearLayout
                    android:id="@+id/ll_goddess"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:paddingTop="6.5dp"
                    android:paddingBottom="6.5dp"
                    app:corners_radius="30dp"

                    app:end_color="@android:color/transparent"
                    app:select_end_color="#EE398E"

                    app:select_start_color="#FF5EBA"
                    app:start_color="@android:color/transparent">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/goddess"
                        android:textColor="@color/white_normal"
                        android:textSize="15dp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="点击我"
                        android:textColor="@color/white_normal"
                        android:textSize="10sp" />
                </com.ansen.shape.AnsenLinearLayout>

                <com.ansen.shape.AnsenLinearLayout
                    android:id="@+id/ll_ordinary_girls"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:paddingTop="6.5dp"
                    android:paddingBottom="6.5dp"
                    app:corners_radius="30dp"
                    app:end_color="@android:color/transparent"
                    app:select_end_color="#EE398E"
                    app:select_start_color="#FF5EBA"
                    app:start_color="@android:color/transparent">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/ordinary_girls"
                        android:textColor="@color/white_normal"
                        android:textSize="15sp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="点击我"
                        android:textColor="@color/white_normal"
                        android:textSize="10sp" />
                </com.ansen.shape.AnsenLinearLayout>
            </com.ansen.shape.AnsenLinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:layout_marginRight="10dp"
            android:orientation="horizontal">

            <com.ansen.shape.AnsenTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="文字渐变"
                app:end_color="#F80FE4"
                app:start_color="#FFE61B"
                app:text_gradient="true" />

            <com.ansen.shape.AnsenTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:padding="5dp"
                android:text="边框渐变"
                app:border_gradient="true"
                app:corners_radius="15dp"
                app:end_color="#F80FE4"
                app:start_color="#FFE61B"
                app:stroke_width="1dp" />

            <com.ansen.shape.AnsenTextView
                android:id="@+id/atv_border_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:padding="5dp"
                android:text="边框跟文字渐变(点我动态改变)"
                app:border_gradient="true"
                app:center_color="#5BC9FF"
                app:corners_radius="15dp"
                app:end_color="#F80FE4"
                app:start_color="#FFE61B"
                app:stroke_width="1dp"
                app:text_gradient="true" />
        </LinearLayout>

        <TextView
            style="@style/QDCommonTitle"
            android:layout_marginTop="20dp"
            android:text="提高某些 child 的权重,不被压缩" />

        <com.ansen.shape.AnsenPriorityLinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"
                android:ellipsize="end"
                android:singleLine="true"
                android:text="1. 这个元素将不被压缩"
                app:layout_priority="incompressible" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"
                android:ellipsize="end"
                android:singleLine="true"
                android:text="测试文案测试文案测试" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"
                android:ellipsize="end"
                android:singleLine="true"
                android:text="测试文案测试文案测试" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"
                android:ellipsize="end"
                android:singleLine="true"
                android:text="测试文案测试文案测试" />
        </com.ansen.shape.AnsenPriorityLinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal">

            <com.ansen.shape.AnsenImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@mipmap/icon_image"
                app:corners_radius="50dp" />

            <com.ansen.shape.AnsenImageView
                android:id="@+id/aiv_one"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginLeft="10dp"
                android:src="@mipmap/icon_image"
                app:corners_radius="50dp"
                app:select_stroke_color="#ff00ff"
                app:select_stroke_width="2dp"
                app:stroke_color="#00ff00"
                app:stroke_width="2dp" />

            <com.ansen.shape.AnsenImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginLeft="10dp"
                android:src="@mipmap/icon_image"
                app:bottom_left_radius="15dp"
                app:stroke_color="#FF5EBA"
                app:stroke_width="3dp"
                app:top_right_radius="15dp" />

            <com.ansen.shape.AnsenImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginLeft="10dp"
                android:src="@mipmap/icon_image"
                app:bottom_left_radius="15dp"
                app:stroke_color="#FF5EBA"
                app:stroke_space="5dp"
                app:stroke_width="3dp"
                app:top_right_radius="15dp" />

            <com.ansen.shape.AnsenImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="10dp"
                android:gravity="center"
                android:paddingLeft="6.5dp"
                android:paddingTop="9dp"
                android:paddingRight="6.5dp"
                android:paddingBottom="9dp"
                android:src="@mipmap/icon_ok"
                android:textColor="@color/white_normal"
                app:corners_radius="30dp"
                app:solid_color="#35F587" />
        </LinearLayout>

        <com.ansen.shape.AnsenConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_marginTop="10dp"
            app:corners_radius="5dp"
            app:solid_color="#FF00FF">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="约束布局测试"
                android:textColor="#FFFFFF" />
        </com.ansen.shape.AnsenConstraintLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:orientation="horizontal">

            <com.ansen.shape.AnsenTextView
                android:id="@+id/tv_age"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:drawablePadding="3dp"
                android:gravity="center"
                android:onClick="onClick"
                android:paddingLeft="5.5dp"
                android:paddingTop="2dp"
                android:paddingRight="5.5dp"
                android:paddingBottom="2dp"
                android:text="年龄"
                android:textColor="#FFFFFF"
                android:textSize="10sp"

                app:color_orientation="left_right"
                app:corners_radius="15dp"
                app:drawable_direction="left"
                app:end_color="#69E3FE"
                app:select_drawable="@mipmap/icon_sex_select_woman"
                app:start_color="#FFFFFFFF"
                app:unselect_drawable="@mipmap/icon_sex_select_male" />

            <!--            app:unselect_drawable="@mipmap/icon_sex_select_woman"-->
            <com.ansen.shape.AnsenTextView
                android:id="@+id/tv_age_two"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="8dp"
                android:layout_toRightOf="@+id/tv_state"
                android:drawableLeft="@mipmap/icon_sex_select_woman"

                android:drawablePadding="2dp"
                android:gravity="center_vertical"

                android:onClick="onClick"
                android:paddingLeft="10dp"

                android:paddingTop="3dp"
                android:paddingRight="10dp"
                android:paddingBottom="3dp"
                app:corners_radius="30dp"
                app:drawable_direction="left"
                app:select_drawable="@null"
                app:select_stroke_color="#69E3FE"
                app:select_stroke_width="0.5dp"
                app:select_text="男"
                app:select_text_color="#69E3FE"
                app:select_text_size="20sp"
                app:stroke_color="#FE83C6"
                app:stroke_width="0.5dp"
                app:text="女"
                app:text_color="#FE83C6"
                app:text_size="10sp" />

            <com.ansen.shape.AnsenTextView
                android:id="@+id/tv_select"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="onClick"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:text="选择"
                app:corners_radius="2dp"
                app:select_solid_color="#EE398E"
                app:select_text_color="@color/white_normal"
                app:solid_color="#F1F1F1"
                app:text_color="#838491" />

            <com.ansen.shape.AnsenImageView
                android:id="@+id/iv_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="onClick"
                app:select_drawable="@mipmap/icon_sex_select_male"
                app:unselect_drawable="@mipmap/icon_sex_select_woman" />
        </LinearLayout>
    </LinearLayout>
</androidx.core.widget.NestedScrollView>

完整的java代码:

import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.ansen.shape.AnsenLinearLayout;
import com.ansen.shape.AnsenTextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private AnsenTextView tvDynamicAlteration;
    private boolean change = false;

    private AnsenTextView tvMale, tvWoman, tvTag, atvBorderText;
    private AnsenLinearLayout llGoddess, llOrdinaryGirls;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvDynamicAlteration = findViewById(R.id.tv_dynamic_alteration);
        tvDynamicAlteration.setOnClickListener(this);

        tvMale = findViewById(R.id.tv_male);
        tvMale.setSelected(true);//默认选中男
        tvWoman = findViewById(R.id.tv_woman);

        tvMale.setOnClickListener(this);
        tvWoman.setOnClickListener(this);

        tvTag = findViewById(R.id.tv_tag);
        tvTag.setOnClickListener(this);

        llGoddess = findViewById(R.id.ll_goddess);
        llGoddess.setSelected(true);
        llOrdinaryGirls = findViewById(R.id.ll_ordinary_girls);

        llGoddess.setOnClickListener(this);
        llOrdinaryGirls.setOnClickListener(this);

        atvBorderText = findViewById(R.id.atv_border_text);
        atvBorderText.setOnClickListener(this);

        findViewById(R.id.aiv_one).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_dynamic_alteration) {
            //改变View的渐变色
            if (change) {
                tvDynamicAlteration.setStartColor(0xFFFF8B59);
                tvDynamicAlteration.setEndColor(0xFFF64848);
                //背景渐变色方向从上到下
                tvDynamicAlteration.setColorOrientation(GradientDrawable.Orientation.TOP_BOTTOM);
            } else {
                tvDynamicAlteration.setStartColor(0xFFFF68FF);//直接写颜色
                tvDynamicAlteration.setEndColor(getResources().getColor(R.color.violet));//从资源文件获取颜色

                //背景渐变色方向从右边到左边
                tvDynamicAlteration.setColorOrientation(GradientDrawable.Orientation.RIGHT_LEFT);
            }

            change = !change;
            tvDynamicAlteration.resetBackground();//设置了属性之后需要调用这个方法
        } else if (v.getId() == R.id.tv_change_select) {//改变选中效果
            Log.i("ansen", "点击之后");
            v.setSelected(!v.isSelected());
        } else if (v.getId() == R.id.tv_change_select_two) {
            v.setSelected(!v.isSelected());
        } else if (v.getId() == R.id.tv_one) {
            v.setSelected(!v.isSelected());
        } else if (v.getId() == R.id.tv_male) {//男选中
            tvMale.setSelected(true);
            tvWoman.setSelected(false);
        } else if (v.getId() == R.id.tv_woman) {//女选中
            tvMale.setSelected(false);
            tvWoman.setSelected(true);
        } else if (v.getId() == R.id.tv_press_one) {
            Toast.makeText(this, "按压效果", Toast.LENGTH_SHORT).show();
        } else if (v.getId() == R.id.tv_tag) {
            tvTag.setStrokeColor(getResources().getColor(R.color.tag_clicl_color));
            tvTag.resetBackground();//设置了属性之后需要调用这个方法
        } else if (v.getId() == R.id.ll_goddess) {
            llGoddess.setSelected(true);
            llOrdinaryGirls.setSelected(false);
        } else if (v.getId() == R.id.ll_ordinary_girls) {
            llGoddess.setSelected(false);
            llOrdinaryGirls.setSelected(true);
        } else if (v.getId() == R.id.atv_border_text) {
//            Log.i("ansen","点击");
            atvBorderText.setSelected(!atvBorderText.isSelected());
            if (atvBorderText.isSelected()) {
                atvBorderText.setStartColor(0xFF5BC9FF);
                atvBorderText.setCenterColor(0);//0就是不设置
                atvBorderText.setEndColor(0xFF4669F6);
            } else {
                atvBorderText.setStartColor(0xFFFFE61B);
                atvBorderText.setCenterColor(0xFF5BC9FF);
                atvBorderText.setEndColor(0xFFF80FE4);
            }
            atvBorderText.invalidate();//重绘
        } else if (v.getId() == R.id.tv_age) {
            v.setSelected(!v.isSelected());
        } else if (v.getId() == R.id.tv_age_two) {
            v.setSelected(!v.isSelected());
        } else if (v.getId() == R.id.tv_select) {
            v.setSelected(!v.isSelected());
        } else if (v.getId() == R.id.iv_image) {
            v.setSelected(!v.isSelected());
        } else if (v.getId() == R.id.aiv_one) {
            v.setSelected(!v.isSelected());
        }
    }
}

分享一下Ansen大佬的源码下载地址:
https://github.com/ansen666/TestShape

推荐阅读更多精彩内容