公司突然来了个需求,做在圆弧里放置图片,点击并有背景效果,于是就采用了selector和shape结合,因做个记录;
首先属性定义在drawable目录下:
下面采用一个ImageView来实现圆线和点击背景效果,定义shape明名为round_normal和round_pressed,注意shape=”oval”属性:
round_normal.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" <!--注意 android:shape="oval"属性--> android:shape="oval" android:useLevel="false"> <solid android:color="@android:color/transparent"/> <size android:width="50dp" android:height="50dp"/> <stroke android:width="0.5dp" android:color="@android:color/white" /> </shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" android:useLevel="false"> <solid android:color="@android:color/white"/> <size android:width="50dp" android:height="50dp"/> <stroke android:width="0.5dp" android:color="@android:color/white" /> </shape>
上面两定义完成后在定义selector,命名为round_solid_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/round_normal" android:state_pressed="false"/> <item android:drawable="@drawable/round_pressed" android:state_pressed="true"/> </selector>
下面我们来引用selector来实现圆圈和点击背景效果;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp"> <ImageView android:id="@+id/imager_id android:layout_width="36dp" android:layout_height="36dp" android:layout_gravity="center" android:adjustViewBounds="true" android:background="@drawable/round_solid_selector" android:paddingBottom="8.2dp" android:paddingLeft="8.2dp" android:alpha="0.75" android:paddingRight="8.2dp" android:paddingTop="8.2dp" android:src="@drawable/ic_launcher" /> </LinearLayout> </LinearLayout>
以上算是大功告成了,谢谢!