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

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"    
  10.        Android:layout_height="wrap_content" />  

其效果如下图所示: 

 

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

Layer List

A 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 element inside a single 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:
Required. This must be the root element. Contains one or more elements.

attributes:

xmlns:Android
String . Required. Defines the XML namespace, which must be "http://schemas.android.com/apk/res/android" .
Defines a drawable to place in the layer drawable, in a position defined by its attributes. Must be a child of a element. Accepts child 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 element inside the element to specify the drawable and define the gravity to something that does not scale, such as "center" . For example, the following 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 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 :
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值