從效果圖中可以看出元莫,覆蓋了App中常用的按鈕效果氮昧,弧度框杜、按壓效果、標(biāo)簽選擇袖肥、按鈕漸變色等咪辱。如果用shape.xml實(shí)現(xiàn)的話,項(xiàng)目drawable目錄下如圖所示:
你會發(fā)現(xiàn)需要寫很多shape文件椎组,很難管理油狂。如果使用自定義屬性的話,有以下優(yōu)點(diǎn):
1.減少項(xiàng)目中95%以上的shape文件寸癌,還能減少安裝包體積
2.一鍵復(fù)制专筷,增加開發(fā)效率
3.代碼靈活修改Shape屬性,例如按鈕弧度蒸苇,漸變色等
Shape框架如何使用磷蛹?
Jcenter在線依賴(默認(rèn))不再更新:
implementation 'com.ansen.shape:shape:1.3.1'
JitPack在線依賴(因?yàn)镴center不在支持開發(fā)者更新項(xiàng)目
在你的項(xiàng)目跟目錄的build.gradle文件的增加依賴
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
使用跟Jcenter在線依賴一樣的,在moudle里面依賴
implementation 'com.github.ansen666:ShapeView:1.3.5'
目前支持以下自定義屬性,滿足大部分需求
app:solid_color="#FF00FF" 填充顏色
app:corners_radius="5dp" 弧度
app:stroke_color="#00ff00" 邊框顏色
app:stroke_width="2dp" 邊框?qū)挾?app:stroke_direction="left|top|right|bottom" 需要顯示的邊框方向 默認(rèn)全邊框(不支持選中狀態(tài))
如果是虛線邊框增加以下兩個(gè)屬性:
app:dash_width="4dp" 虛線邊框的長度
app:dash_gap="2dp" 虛線邊框兩個(gè)線之間的間隙
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" 填充漸變色結(jié)束
按壓漸變色
app:pressed_start_color="#983458"
app:pressed_center_color="#98633F"
app:pressed_end_color="#98633F"
app:pressed_solid_color="#FFDEE0E2" 按壓填充顏色
選中屬性
app:select_solid_color="#FFFFFFFF" 選中填充顏色
app:select_start_color="#EB89FF" 漸變色選中開始色
app:select_center_color="#FCCE5F" 漸變色選中中間色
app:select_end_color="#FCCE5F" 漸變色選中結(jié)束色
app:select_stroke_color="#ffbababa" 選中情況邊框色
app:select_stroke_width="0.5dp" 選中情況邊框?qū)挾?
TextView/EditView控件獨(dú)有屬性
app:text="點(diǎn)擊我切換選中效果"
app:select_text="選中時(shí)顯示的文字"
app:text_color="#FFFFFFFF" 文本未選中顏色
app:select_text_color="#BBBBBB" 文本選中顏色
app:text_size="10sp" 字體大小
app:select_text_size="20sp" 選中字體大小
app:border_gradient="true/false" 邊框漸變
app:text_gradient="true/false" 文字漸變
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:底部
app:selected_reset_background="true" 設(shè)置選中/未選中狀態(tài)時(shí)是否更新背景 默認(rèn)為true
圖片獨(dú)有屬性
app:scale_type="top/center/bottom/fitXY" 顯示類型
app:stroke_space="0.5dp" 描邊與圖片間距
AnsenPriorityLinearLayout控件才有的屬性:
app:layout_priority="incompressible" 提高當(dāng)前View在LinearLayout中的權(quán)重味咳,保證內(nèi)容不被壓縮
首先需要在xml根布局控件增加一個(gè)屬性:
xmlns:app="http://schemas.android.com/apk/res-auto"
例如給TextView設(shè)置弧度、填充顏色檬嘀、邊框顏色槽驶、邊框?qū)挾?代碼如下所示:
<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" />
注意:動(dòng)態(tài)修改填充色時(shí):
tvTag.setStrokeColor(getResources().getColor(R.color.tag_clicl_color));
tvTag.resetBackground();//設(shè)置了屬性之后需要調(diào)用這個(gè)方法
完整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="點(diǎn)擊我通過代碼改變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="點(diǎn)擊我切換選中效果"
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="選中時(shí)顯示的文字"
app:select_text_color="#5B3B28"
app:start_color="#FF88D9"
app:stroke_width="0dp"
app:text="點(diǎn)擊我切換選中效果"
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="點(diǎn)擊我看按壓效果"
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="點(diǎn)擊我看按壓效果"
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="標(biāo)簽效果"
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="點(diǎn)擊我"
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="點(diǎn)擊我"
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="邊框跟文字漸變(點(diǎn)我動(dòng)態(tài)改變)"
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 的權(quán)重悍及,不被壓縮" />
<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. 這個(gè)元素將不被壓縮"
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);//默認(rèn)選中男
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();//設(shè)置了屬性之后需要調(diào)用這個(gè)方法
} else if (v.getId() == R.id.tv_change_select) {//改變選中效果
Log.i("ansen", "點(diǎn)擊之后");
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();//設(shè)置了屬性之后需要調(diào)用這個(gè)方法
} 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","點(diǎn)擊");
atvBorderText.setSelected(!atvBorderText.isSelected());
if (atvBorderText.isSelected()) {
atvBorderText.setStartColor(0xFF5BC9FF);
atvBorderText.setCenterColor(0);//0就是不設(shè)置
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/ShapeView