Android自定义Shape 加上阴影shadow之方法
来源:互联网 发布:mina 客户端接收数据 编辑:程序博客网 时间:2024/05/21 15:48
Android支持自定义Shape, 以画出需要的形状,可以作为TextView, EditText, Button的背景drawable资源。Shape很简单,就是一个XML文件,SDK文档里描述其格式如下:
- <?xml version="1.0" encoding="utf-8"?>
- <shape
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:shape=["rectangle" | "oval" | "line" | "ring"] >
- <corners
- android:radius="integer"
- android:topLeftRadius="integer"
- android:topRightRadius="integer"
- android:bottomLeftRadius="integer"
- android:bottomRightRadius="integer" />
- <gradient
- android:angle="integer"
- android:centerX="integer"
- android:centerY="integer"
- android:centerColor="integer"
- android:endColor="color"
- android:gradientRadius="integer"
- android:startColor="color"
- android:type=["linear" | "radial" | "sweep"]
- android:usesLevel=["true" | "false"] />
- <padding
- android:left="integer"
- android:top="integer"
- android:right="integer"
- android:bottom="integer" />
- <size
- android:width="integer"
- android:height="integer" />
- <solid
- android:color="color" />
- <stroke
- android:width="integer"
- android:color="color"
- android:dashWidth="integer"
- android:dashGap="integer" />
- </shape>
其支持的属性没有shadow, 做Web前端开发的同学写CSS可以很方便地加一个shadow属性值,如何给Android Shape加一个shadow,以得到类似的效果呢?
答案是使用layer-list ! 直接上代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
- <item>
- <shape android:shape="rectangle">
- <solid android:color="#792a03" />
- <corners android:radius="19dp" />
- </shape>
- </item>
- <item android:top="1px">
- <shape android:shape="rectangle">
- <gradient android:startColor="#ffdb8f" android:endColor="#ffdb8f"
- android:angle="270" />
- <padding android:left="5dp" android:top="3dp" android:right="5dp"
- android:bottom="3dp" />
- <corners android:radius="20dp" />
- </shape>
- </item>
- </layer-list>
将以上xml存成btn_test, 放到res/drawable/目录下。 将该drawable xml设为一个TextView的backgroiund,
- <TextView
- android:background="@drawable/btn_test"
- android:layout_marginTop="20dip"
- android:layout_marginLeft="5dip"
- android:textColor="#792a03"
- android:text="1天2小时14分20秒"
- android:layout_width="wrap_content"
- 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
<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 version="1.0" encoding="utf-8"?>
- <layer-list
- xmlns:Android="http://schemas.android.com/apk/res/android" >
- <item
- android:drawable="@[package:]drawable/drawable_resource"
- android:id="@[+][package:]id/resource_name"
- android:top="dimension"
- android:right="dimension"
- android:bottom="dimension"
- android:left="dimension" />
- </layer-list>
- elements:
- <item android:drawable="@drawable/image" />
To avoid scaling, the following example uses a
<bitmap>
element with centered gravity:- <item>
- <bitmap android:src="@drawable/image"
- android:gravity="center" />
- </item>
- example:
- XML file saved at
res/drawable/layers.xml
: - <?xml version="1.0" encoding="utf-8"?>
- <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
- <item>
- <bitmap android:src="@drawable/android_red"
- android:gravity="center" />
- </item>
- <item android:top="10dp" android:left="10dp">
- <bitmap android:src="@drawable/android_green"
- android:gravity="center" />
- </item>
- <item android:top="20dp" android:left="20dp">
- <bitmap android:src="@drawable/android_blue"
- android:gravity="center" />
- </item>
- </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:
- <ImageView
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:src="@drawable/layers" />
The result is a stack of increasingly offset images:
- see also:
- Android自定义Shape 加上阴影shadow之方法
- Android自定义Shape 加上阴影shadow之方法
- Android自定义Shape 加上阴影shadow之方法
- Android自定义Shape 加上阴影shadow之方法
- Android:自定义Shape 加上阴影shadow之方法
- android小技巧--shape自定义阴影效果
- Android之阴影制作(Shadow)
- Android之阴影制作(Shadow)
- Android 阴影制作 -- Shadow
- Android Shadow阴影
- Android xml中为文字添加阴影(shadow方法)
- android shape阴影效果
- Android组件 <shape>阴影
- 使用shape 自定义ui之渐变半圆阴影
- Android进阶2之 阴影制作(Shadow)
- Android进阶2之 阴影制作(Shadow)
- Android进阶2之 阴影制作(Shadow)
- Android进阶2之 阴影制作(Shadow)
- 自己的笔记 NSDateFormat 将传入数字转为mm:ss 格式
- Yii的常用URL
- Sublime Text2 快捷键汇总
- C语言中字符串如何转换为二进制、八进制、十进制、十六进制
- log4php将不同级别的日志打印到不同的日志文件中
- Android自定义Shape 加上阴影shadow之方法
- android默认关机电量修改
- 使用psftp上传文件到服务器
- 企业IT管理员IE11升级指南【11】—— 通过SCCM 2012和WSUS部署Internet Explorer 11
- Python 正则表达式为XML中tags增加Attribute
- Android EditText控件即设置最小高度又运行高度随内容增加而变化
- mpu9150+audrino+9轴陀螺仪
- Android图像处理之Bitmap类
- 埃及总部通知