shape使用总结

xiaoxiao2021-02-28  69

圆角矩形

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <corners android:radius="5.0dp" /> <solid android:color="#f09738" /> </shape>


圆柱

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@color/colorAccent" /> <size android:width="15dp" android:height="15dp" /> <corners android:radius="40dp" /> </shape>


<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <size android:width="15dp" android:height="15dp" /> <solid android:color="#f09738" /> </shape>


带边框的圆角矩形(圆柱带边框,圆形带边框同理)

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <corners android:radius="5.0dp" /> <solid android:color="#f09738" /> <stroke android:width="3dp" android:color="#ffffff"/> </shape>


渐变

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <gradient android:startColor="#ff0000" android:centerColor="#00ff00" android:endColor="#ff0000" android:angle="45" /> </shape>


环形渐变(起始,终点颜色相同)

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <gradient android:endColor="#0000ff" android:centerColor="#00ff00" android:startColor="#0000ff" android:type="sweep" /> </shape>


发光圆圈

<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="oval"> <stroke android:width="4dp" android:color="#0C00F9" /> <corners android:radius="25dp" /> </shape> </item> <item android:bottom="4dp" android:left="4dp" android:right="4dp" android:top="4dp"> <shape android:shape="oval"> <stroke android:width="4dp" android:color="#FFF" /> <corners android:radius="25dp" /> </shape> </item> <item android:bottom="6dp" android:left="6dp" android:right="6dp" android:top="6dp"> <shape android:shape="oval"> <stroke android:width="3dp" android:color="#0C00F9" /> <corners android:radius="25dp" /> </shape> </item> </layer-list>


发光矩形

<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape> <stroke android:width="4dp" android:color="#0C00F9" /> <corners android:radius="2dp" /> </shape> </item> <item android:bottom="4dp" android:left="4dp" android:right="4dp" android:top="4dp"> <shape> <stroke android:width="4dp" android:color="#FFF" /> <corners android:radius="2dp" /> </shape> </item> <item android:bottom="6dp" android:left="6dp" android:right="6dp" android:top="6dp"> <shape> <stroke android:width="3dp" android:color="#0C00F9" /> <corners android:radius="2dp" /> </shape> </item> </layer-list>


渐变圆环

<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="oval"> <!--android:centerColor="#00ff00"--> <gradient android:startColor="#00ff00" android:endColor="#ff0000" android:angle="45" /> </shape> </item> <item android:bottom="6dp" android:left="6dp" android:right="6dp" android:top="6dp"> <shape android:shape="oval"> <corners android:radius="25dp" /> <solid android:color="#ffffff"/> </shape> </item> </layer-list>


虚线边框

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <stroke android:dashGap="2dp" android:dashWidth="2dp" android:width="1px" android:color="#ffffff" /> <size android:height="1.0dp"/> <corners android:radius="5.0dp" /> <solid android:color="#cc000000" /> </shape>


带阴影的渐变圆环

<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="oval"> <!--android:centerColor="#00ff00"--> <gradient android:startColor="#00ff00" android:endColor="#ff0000" android:angle="45" /> </shape> </item> <item android:bottom="6dp" android:left="6dp" android:right="6dp" android:top="6dp"> <shape android:shape="oval"> <corners android:radius="25dp" /> <solid android:color="#ffffff"/> </shape> </item> <item android:bottom="9dp" android:left="9dp" android:right="9dp" android:top="12dp"> <shape android:shape="oval"> <corners android:radius="25dp" /> <solid android:color="#cccccccc"/> </shape> </item> <item android:bottom="12dp" android:left="12dp" android:right="12dp" android:top="12dp"> <shape android:shape="oval"> <corners android:radius="25dp" /> <solid android:color="#ffffff"/> </shape> </item> </layer-list>


虚线

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line" > <!--线宽为dashWith,线之间空隙dashGap,dashGap=0dp时,是实线 --> <stroke android:dashGap="2dip" android:dashWidth="4dip" android:width="1px" android:color="@color/white" /> <size android:height="1px" /> </shape>

使用虚线的时候,要在代码中加上: dottedLine.setLayerType(View.LAYER_TYPE_SOFTWARE, null); 这句是去掉虚线的硬件加速,否则在某些手机上,显示的是一条实线


目前只研究了这些,欢迎大家补充

转载请注明原文地址: https://www.6miu.com/read-82836.html

最新回复(0)