你好,游客 登录 注册 搜索
背景:
阅读新闻

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

[日期:2011-10-15] 来源:Linux社区  作者:ericbaner [字体:大 中 小]

其效果如下图所示: 

 

关于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 <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:
linux
相关资讯       Android基础教程  Android开发教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款