Android layer-list clip shape Selector 详细介绍及使用

Android UI设计中经常用到的layer-list, shape在美化控件中的作用是至关重要的。
 
  • shape
    1. 作用:在xml中定义几何形状
    2. 属性:
      <shape>  Android:shape=["rectangle" | "oval" | "line" | "ring"]   
      	其中rectagle矩形,oval椭圆,line水平直线,ring环形
<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape=["rectangle" | "oval" | "line" | "ring"] > 
    <gradient 渐变  
        android:angle="integer" 渐变角度,0从左到右,90表示从下到上,数值为45的整数倍,默认为0;  
        android:centerX="integer" 
       android:centerY="integer" 
       android:centerColor="integer" 
       android:endColor="color" 结束颜色          
        android:gradientRadius="integer" 
       android:startColor="color"         起始颜色  
        android:type=["linear" | "radial" | "sweep"]  渐变的样式 liner线性渐变 radial环形渐变 sweep  
       android:usesLevel=["true" | "false"] /> 
    <solid 填充          
        android:color="color" 填充的颜色 /> 
    <stroke 描边
        android:width="integer"     描边的宽度  
        android:color="color"         描边的颜色  
        android:dashWidth="integer" 表示横线的宽度  
        android:dashGap="integer"     表示横线之间的距离   /> 
    <padding 
       android:left="integer" 
       android:top="integer" 
       android:right="integer" 
       android:bottom="integer" /> 
    <corners 圆角  
        android:radius="integer"             圆角的半径 值越大角越圆  
        android:topLeftRadius="integer"     左上圆角半径  
        android:topRightRadius="integer"     右上圆角半径  
        android:bottomLeftRadius="integer"     左下圆角角半径  
        android:bottomRightRadius="integer" 右下圆角角半径  /> 
</shape>
  • layer-list,将多个图片或两种以上的效果按照顺序层叠;
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="0dip" />

            <solid android:color="#FDE5A2" />
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="0dip" />

                <gradient
                    android:angle="270"
                    android:centerColor="#80ffb600"
                    android:centerY="0.75"
                    android:endColor="#a0ffcb00"
                    android:startColor="#80ffd300" />
            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="0dip" />

                <solid android:color="#FD2702" />
            </shape>
        </clip>
    </item>

</layer-list>

layer-list层叠样式在程序中对应LayerDrawable,使用方法如下:

Resources resources = getResources(); 
Drawable[] layers = new Drawable[2]; 
layers[0] = r.getDrawable(R.drawable.white); 
layers[1] = r.getDrawable(R.drawable.logo_overlay); 
LayerDrawable layerDrawable = new LayerDrawable(layers)
((ImageView) findViewById(R.id.imageview)).setImageDrawable(layerDrawable);

参考文章:

http://tbwisk.github.io/lessons/2015/01/14/Android%E5%BC%80%E5%8F%91%EF%BC%8Cshape%EF%BC%8Cselector%EF%BC%8Clayer-list%E4%BB%8B%E7%BB%8D/

原文地址:https://www.cnblogs.com/zhangyulogin/p/4437825.html