GradientDrawable

GradientDrawable

extends  Drawable
java.lang.Object
   ↳ android.graphics.drawable.Drawable
     ↳ android.graphics.drawable.GradientDrawable



Shap可以是矩形(矩形可以变成圆角矩形、圆形),椭圆、线、或者圆环(控制内圆半径可以达到圆形的效果)

[java]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  3.              android:shape="rectangle|oval|line|ring"  
  4.              android:visible=""  
  5.              android:dither=""  
  6.              android:innerRadiusRatio=""  
  7.              android:thicknessRatio=""  
  8.              android:innerRadius=""   
  9.              android:thickness=""  
  10.              android:useLevel=""   
  11.              android:tint=""  
  12.              android:tintMode=""  
  13.     >  
  14.   
  15. </shape>  

shap :指定形状

visible:是否可见

dither:是否抗抖动

thickness:环的厚度

thicknessRatio
以环的宽度比率来表示环的厚度,例如,如果android:thicknessRatio="2",

    那么环的厚度就等于环的宽度除以2。这个值是可以被android:thickness覆盖的,默认值是3.

innerRadius :内环的半径。

innerRadiusRatio :以环的宽度比率来表示内环的半径, 例如,如果android:innerRadiusRatio,表示内环半径等于环的宽度除以5,这个值是可以被覆盖的,默认为9.


 useLevel :如果当做是LevelListDrawable使用时值为true,否则为false.


1.corners 圆角

[java]  view plain copy
  1. <!--  
  2.           
  3.       android:radius             整型 半径  
  4.       android:topLeftRadius      整型 左上角半径  
  5.       android:topRightRadius     整型 右上角半径  
  6.       android:bottomLeftRadius   整型 左下角半径  
  7.       android:bottomRightRadius  整型 右下角半径  
  8.      -->  
  9.      <corners    
  10.         android:radius="8dp"  
  11.         android:topLeftRadius="5dp"  
  12.         android:topRightRadius="15dp"  
  13.         android:bottomLeftRadius="20dp"  
  14.         android:bottomRightRadius="25dp"    
  15.         />  


 
2.Gradient 渐变

[java]  view plain copy
  1. <!--   
  2.          代表渐变颜色的角度, 0 is left to right, 90 is bottom to top. 
  3.          必须是45的整数倍.   
  4.          默认是 0.该属性只有在type=linear情况下起作用,默认的type为linear。  
  5.          android:angle="integer"         
  6.   
  7.          渐变开始色,渐变中间色,渐变结束色  
  8.          android:startColor="color"                 
  9.          android:centerColor="color"           
  10.          android:endColor="color"    
  11.            
  12.          这两个属性只有在type不为linear情况下起作用。  
  13.          相对X、Y的渐变位置  
  14.          android:centerX="float"           
  15.          android:centerY="float"    
  16.            
  17.          渐变颜色的半径,单位应该是像素点. 需要 android:type="radial".  
  18.          如果android:type="radial",没有设置android:gradientRadius,将会报              错, error inflating class.  
  19.          android:gradientRadius="float"   
  20.            
  21.           linear 线性渐变,这是默认设置  
  22.           radial 放射性渐变,以开始色为中心。  
  23.           sweep 扫描线式的渐变。  
  24.           android:type=["linear" | "radial" | "sweep"]      
  25.            
  26.           如果要使用LevelListDrawable对象,就要设置为true。设置为true无渐变。
  27.           false有渐变色  
  28.           android:useLevel=["true" | "false"]   
  29.   
  30.  -->  
  31.       
  32.     <gradient          
  33.          android:angle="integer"           
  34.          android:centerX="float"           
  35.          android:centerY="float"     
  36.          android:startColor="color"                
  37.          android:centerColor="color"           
  38.          android:endColor="color"           
  39.          android:gradientRadius="float"        
  40.          android:type=["linear" | "radial" | "sweep"]           
  41.          android:useLevel=["true" | "false"] />  



3.solid 填充色
[java]  view plain copy
  1. <solid  android:color=""  />  


4.padding 内边距
[java]  view plain copy
  1. <padding   
  2.        android:left=""  
  3.        android:top=""  
  4.        android:right=""  
  5.        android:bottom=""  
  6.        />  

5.size 形状大小

[java]  view plain copy
  1. <size android:width=""  
  2.        android:height=""  
  3.        />  

6.stroke 边框


[java]  view plain copy
  1. <!-- android:width=”2dp” 描边的宽度,android:color 描边的颜色。   
  2.       我们还可以把描边弄成虚线的形式,设置方式为:   
  3.        android:dashWidth=”5dp”   
  4.        android:dashGap=”3dp”   
  5.        其中android:dashWidth表示"-‘这样一个横线的宽度,android:dashGap表示之间隔开的距离 -->  
  6. <stroke   
  7.     android:width=""  
  8.     android:dashWidth=""  
  9.     android:color=""  
  10.     android:dashGap=""  
  11.     />  


sweep 渐变
[java]  view plain copy
  1. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:shape="rectangle"  
  3.      >  
  4.   
  5.     <gradient  
  6.         android:centerColor="#00ff00"  
  7.         android:endColor="#0000ff"  
  8.         android:startColor="#ff0000"  
  9.         android:type="sweep"  
  10.         android:useLevel="false" />  
  11.   
  12. </shape>  




[java]  view plain copy
  1. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:shape="rectangle"  
  3.      >  
  4.   
  5.     <gradient  
  6.         android:centerColor="#00ff00"  
  7.         android:endColor="#0000ff"  
  8.         android:startColor="#ff0000"  
  9.         android:type="sweep"  
  10.         android:centerX="0.2"  
  11.         android:centerY="0.2"  
  12.         android:useLevel="false" />  
  13.   
  14. </shape>  






linear 渐变  

该渐变下有个特有属性  android:angle=""

[java]  view plain copy
  1. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:shape="rectangle"  
  3.      >  
  4.   
  5.     <gradient  
  6.         android:centerColor="#00ff00"  
  7.         android:endColor="#0000ff"  
  8.         android:startColor="#ff0000"  
  9.         android:type="linear"  
  10.         android:useLevel="false" />  
  11.   
  12. </shape>  




[java]  view plain copy
  1. android:angle="45"  


[java]  view plain copy
  1. android:angle="90"  




[java]  view plain copy
  1. android:angle="225"  



radial 渐变  

该渐变下有个特有属性  android:gradientRadius=""

<?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="#0000ff"
        android:type="radial"
        android:gradientRadius="400"
        android:useLevel="false" />

</shape>



  android:centerX="0.2"
  android:centerY="0.2"




<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:innerRadiusRatio="8"
    android:thicknessRatio="3"
    android:useLevel="false"
     >

    <gradient
        android:startColor="#ff0000"
        android:centerColor="#00ff00"
        android:endColor="#0000ff"
        android:type="sweep"
        android:useLevel="false" />

</shape>





<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line"
    android:useLevel="false">
    
   <padding android:bottom="30dp" />
   
   <stroke
       android:width="2dp"
       android:color="#645454"
       android:dashWidth="5dp"
       android:dashGap="10dp"
       />
</shape>

你很可能看到的是一条实线,Android 4.0 默认打开了硬件加速,我们把硬件加速关掉就行,

yourView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);







<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    android:useLevel="false">
    
   <corners  android:radius="5dp" />
   <solid  android:color="@android:color/white"  />
   <stroke
       android:width="2dp"
       android:color="#a3d458"
       />
</shape>




<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    android:useLevel="false">
    
   <corners   android:radius="5dp"/>
   <solid  android:color="@android:color/white" />
   <stroke
       android:width="2dp"
       android:color="#a3d458"
       android:dashWidth="5dp"
       android:dashGap="10dp"
       />
</shape>



<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    android:useLevel="false">
    
   <corners 
       android:topLeftRadius="10dp"
       android:topRightRadius="10dp"
       />
   <solid  android:color="@android:color/white" />
   <stroke
       android:width="2dp"
       android:color="#a3d458"
       />
</shape>






<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    android:useLevel="false">
    
   <corners  android:radius="10dp"/>
   <gradient 
       android:startColor="#ff115a"
       android:endColor="#ff9083"
       android:angle="270"
       />
   <stroke
       android:width="2dp"
       android:color="#a3d458"
       />
</shape>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值