Android自定义Shape 加上阴影shadow之方法

来源:互联网 发布:mina 客户端接收数据 编辑:程序博客网 时间:2024/05/21 15:48

Android支持自定义Shape, 以画出需要的形状,可以作为TextView, EditText, Button的背景drawable资源。Shape很简单,就是一个XML文件,SDK文档里描述其格式如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:shape=["rectangle" | "oval" | "line" | "ring"] >  
  5.     <corners  
  6.         android:radius="integer"  
  7.         android:topLeftRadius="integer"  
  8.         android:topRightRadius="integer"  
  9.         android:bottomLeftRadius="integer"  
  10.         android:bottomRightRadius="integer" />  
  11.     <gradient  
  12.         android:angle="integer"  
  13.         android:centerX="integer"  
  14.         android:centerY="integer"  
  15.         android:centerColor="integer"  
  16.         android:endColor="color"  
  17.         android:gradientRadius="integer"  
  18.         android:startColor="color"  
  19.         android:type=["linear" | "radial" | "sweep"]   
  20.         android:usesLevel=["true" | "false"] />  
  21.     <padding  
  22.         android:left="integer"  
  23.         android:top="integer"  
  24.         android:right="integer"  
  25.         android:bottom="integer" />  
  26.     <size  
  27.         android:width="integer"  
  28.         android:height="integer" />  
  29.     <solid  
  30.         android:color="color" />  
  31.     <stroke  
  32.         android:width="integer"  
  33.         android:color="color"  
  34.         android:dashWidth="integer"  
  35.         android:dashGap="integer" />  
  36. </shape>  

其支持的属性没有shadow, 做Web前端开发的同学写CSS可以很方便地加一个shadow属性值,如何给Android Shape加一个shadow,以得到类似的效果呢?

答案是使用layer-list !   直接上代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <item>  
  5.         <shape android:shape="rectangle">  
  6.             <solid android:color="#792a03" />  
  7.             <corners android:radius="19dp" />  
  8.         </shape>  
  9.     </item>  
  10.        
  11.     <item  android:top="1px">  
  12.         <shape android:shape="rectangle">  
  13.             <gradient android:startColor="#ffdb8f" android:endColor="#ffdb8f"  
  14.                 android:angle="270" />  
  15.             <padding android:left="5dp" android:top="3dp" android:right="5dp"  
  16.                 android:bottom="3dp" />  
  17.             <corners android:radius="20dp" />  
  18.         </shape>  
  19.   
  20.     </item>  
  21.   
  22.  </layer-list>  

将以上xml存成btn_test, 放到res/drawable/目录下。 将该drawable xml设为一个TextView的backgroiund,

  1. <TextView  
  2.        android:background="@drawable/btn_test"  
  3.   
  4.         android:layout_marginTop="20dip"  
  5.         android:layout_marginLeft="5dip"  
  6.     android:textColor="#792a03"            
  7.            
  8.         android:text="1天2小时14分20秒"  
  9.        android:layout_width="wrap_content"    

  1.        android:layout_height="wrap_content" />  
  2. 其效果如下图所示: 

     

    关于layer-list的进一步解释见SDK文档,如下: 

    Layer List

    LayerDrawable is a drawable object that manages an array of other drawables. Each drawable in the list is drawn in the order of the list—the last drawable in the list is drawn on top.

    Each drawable is represented by an <item> element inside a single <layer-list> element.

    file location:
    res/drawable/filename .xml 
    The filename is used as the resource ID.
    compiled resource datatype:
    Resource pointer to a LayerDrawable .
    resource reference:
    In Java: R.drawable.filename 
    In XML: @[package :]drawable/filename
    syntax:

    Xml代码
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <layer-list  
    3.     xmlns:Android="http://schemas.android.com/apk/res/android" >  
    4.     <item  
    5.         android:drawable="@[package:]drawable/drawable_resource"  
    6.         android:id="@[+][package:]id/resource_name"  
    7.         android:top="dimension"  
    8.         android:right="dimension"  
    9.         android:bottom="dimension"  
    10.         android:left="dimension" />  
    11. </layer-list>  
     

     

    elements:
    <layer-list>
    Required. This must be the root element. Contains one or more <item> elements.

    attributes:

    xmlns:android
    String . Required. Defines the XML namespace, which must be "http://schemas.android.com/apk/res/android" .
    <item>
    Defines a drawable to place in the layer drawable, in a position defined by its attributes. Must be a child of a <selector> element. Accepts child <bitmap> elements.

    attributes:

    android:drawable
    Drawable resource . Required . Reference to a drawable resource.
    android:id
    Resource ID . A unique resource ID for this drawable. To create a new resource ID for this item, use the form: "@+id/name " . The plus symbol indicates that this should be created as a new ID. You can use this identifier to retrieve and modify the drawable with View.findViewById() or Activity.findViewById() .
    android:top
    Integer . The top offset in pixels.
    android:right
    Integer . The right offset in pixels.
    android:bottom
    Integer . The bottom offset in pixels.
    android:left
    Integer . The left offset in pixels.

    All drawable items are scaled to fit the size of the containing View, by default. Thus, placing your images in a layer list at different positions might increase the size of the View and some images scale as appropriate. To avoid scaling items in the list, use a <bitmap>element inside the <item> element to specify the drawable and define the gravity to something that does not scale, such as "center" . For example, the following <item> defines an item that scales to fit its container View:

    Xml代码
    1. <item android:drawable="@drawable/image" />  

     

    To avoid scaling, the following example uses a <bitmap> element with centered gravity:

    Xml代码
    1. <item>  
    2.   <bitmap android:src="@drawable/image"  
    3.           android:gravity="center" />  
    4. </item>  
     
    example:
    XML file saved at res/drawable/layers.xml :

    Xml代码
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">  
    3.     <item>  
    4.       <bitmap android:src="@drawable/android_red"  
    5.         android:gravity="center" />  
    6.     </item>  
    7.     <item android:top="10dp" android:left="10dp">  
    8.       <bitmap android:src="@drawable/android_green"  
    9.         android:gravity="center" />  
    10.     </item>  
    11.     <item android:top="20dp" android:left="20dp">  
    12.       <bitmap android:src="@drawable/android_blue"  
    13.         android:gravity="center" />  
    14.     </item>  
    15. </layer-list>  


    Notice that this example uses a nested <bitmap> element to define the drawable resource for each item with a "center" gravity. This ensures that none of the images are scaled to fit the size of the container, due to resizing caused by the offset images.

    This layout XML applies the drawable to a View:

     

    This layout XML applies the drawable to a View:

     

    Xml代码
    1. <ImageView  
    2.     android:layout_height="wrap_content"  
    3.     android:layout_width="wrap_content"  
    4.     android:src="@drawable/layers" />  
     

    The result is a stack of increasingly offset images:

    see also:

0 1
原创粉丝点击