Material Design系列第四篇——Defining Shadows and Clipping Views
Defining Shadows and Clipping Views
This lesson teaches you to
You should also read
Material design introduces elevation for UI elements. Elevation helps users understand the relative importance of each element and focus their attention to the task at hand.
The elevation of a view, represented by the Z property, determines the visual appearance of its shadow: views with higher Z values cast larger, softer shadows. Views with higher Z values occlude views with lower Z values; however, the Z value of a view does not affect the view's size.
Shadows are drawn by the parent of the elevated view, and thus subject to standard view clipping, clipped by the parent by default.
Elevation is also useful to create animations where widgets temporarily rise above the view plane when performing some action.
For more information about elevation in material design, see Objects in 3D space.
Assign Elevation to Your Views
The Z value for a view has two components:
- Elevation: The static component.
- Translation: The dynamic component used for animations.
Z = elevation + translationZ

Figure 1 - Shadows for different view elevations.
To set the elevation of a view in a layout definition, use the android:elevation attribute. To set the elevation of a view in the code of an activity, use the View.setElevation() method.
To set the translation of a view, use the View.setTranslationZ() method.
The new ViewPropertyAnimator.z() and ViewPropertyAnimator.translationZ() methods enable you to easily animate the elevation of views. For more information, see the API reference for ViewPropertyAnimator and the Property Animation developer guide.
You can also use a StateListAnimator to specify these animations in a declarative way. This is especially useful for cases where state changes trigger animations, like when a user presses a button. For more information, see Animate View State Changes.
The Z values are measured in dp (density-independent pixels).
Customize View Shadows and Outlines
The bounds of a view's background drawable determine the default shape of its shadow. Outlines represent the outer shape of a graphics object and define the ripple area for touch feedback.
Consider this view, defined with a background drawable:
<TextView
android:id="@+id/myview"
...
android:elevation="2dp"
android:background="@drawable/myrect"/>
The background drawable is defined as a rectangle with rounded corners:
<!-- res/drawable/myrect.xml -->
<shapexmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solidandroid:color="#42000000"/>
<cornersandroid:radius="5dp"/>
</shape>
The view casts a shadow with rounded corners, since the background drawable defines the view's outline. Providing a custom outline overrides the default shape of a view's shadow.
To define a custom outline for a view in your code:
- Extend the
ViewOutlineProviderclass. - Override the
getOutline()method. - Assign the new outline provider to your view with the
View.setOutlineProvider()method.
You can create oval and rectangular outlines with rounded corners using the methods in the Outline class. The default outline provider for views obtains the outline from the view's background. To prevent a view from casting a shadow, set its outline provider to null.
Clip Views
Clipping views enables you to easily change the shape of a view. You can clip views for consistency with other design elements or to change the shape of a view in response to user input. You can clip a view to its outline area using the View.setClipToOutline() method or the android:clipToOutline attribute. Only rectangle, circle, and round rectangle outlines support clipping, as determined by the Outline.canClip() method.
To clip a view to the shape of a drawable, set the drawable as the background of the view (as shown above) and call the View.setClipToOutline() method.
Clipping views is an expensive operation, so don't animate the shape you use to clip a view. To achieve this effect, use the Reveal Effect animation.
Material Design系列第四篇——Defining Shadows and Clipping Views的更多相关文章
- Material Design系列第六篇——Defining Custom Animations
Defining Custom Animations //自定义动画 This lesson teaches you to //本节课知识点 Customize Touch Feedback //1. ...
- Material Design系列第八篇——Creating Lists and Cards
Creating Lists and Cards //创建列表和卡片 To create complex lists and cards with material design styles in ...
- Material Design系列第七篇——Maintaining Compatibility
Maintaining Compatibility This lesson teaches you to Define Alternative Styles Provide Alternative L ...
- Material Design系列第五篇——Working with Drawables
Working with Drawables This lesson teaches you to Tint Drawable Resources Extract Prominent Colors f ...
- Material Design系列第三篇——Using the Material Theme
Using the Material Theme This lesson teaches you to Customize the Color Palette Customize the Status ...
- Creating Apps With Material Design —— Defining Shadows and Clipping Views
View转载请注明 http://blog.csdn.net/eclipsexys 翻译自Developer Android,时间仓促,有翻译问题请留言指出,谢谢 定义阴影和裁减 材料设计引入了深度的 ...
- Material Design系列第二篇——Getting Started
Getting Started This lesson teaches you to Apply the Material Theme Design Your Layouts Specify Elev ...
- Material Design系列第一篇——Creating Apps with Material Design
Creating Apps with Material Design //创建Material Design的App Material design is a comprehensive guide ...
- Android Material Design系列之主题样式介绍说明
今天这篇文章应该算是Material Design系列的补充篇,因为这篇文章本来应该放到前面讲的,因为讲的是主题嘛,对于一些状态和颜色的介绍,因为我们一新建一个项目时,系统自带了三个属性的颜色,现在就 ...
随机推荐
- Java如何处理运行时异常?
在Java编程中,如何处理运行时异常? 此示例显示如何处理java程序中的运行时异常. package com.yiibai; public class RuntimeExceptions { sta ...
- Java如何比较两个数组是否相等?
在Java中,如何检查两个数组是否相等? 示例 以下示例显示如何使用数组的equals()方法来检查两个数组是否相等. package com.yiibai; import java.util.*; ...
- int[,] 和 int[][] 有什么区别
int[,] 是二维数组,它就是传统意义上 n x m 的表,和 C++ 里的 int[][] 是一个意思. int[][] 是交错数组,与 C++ 里的 int[][] 不同.它其实是一个 int[ ...
- u3d DontDestroyOnLoad多场景问题
using UnityEngine; using System.Collections; public class DontDel : MonoBehaviour { public GameObjec ...
- Nginx负载均衡 ssl原理 生成ssl密钥对 Nginx配置ssl
- adb 架构
adb是什么? adb就是Android调试桥,很形象啊. 先来看adb原理的逻辑图: ADB架构adb由两个物理文件组成:adb/adb.exe运行于PC端,包括Linux.Windows.Mac ...
- 【中文分词】DAG、DP、HMM、Viterbi
http://blog.sina.com.cn/s/blog_8267db980102wq41.html http://www.cnblogs.com/leeshine/p/5804679.html ...
- 如何在IntelliJ IDEA中快速配置Tomcat
近来使用idea编写java代码的人越来越多,最关键的就是idea强大的代码提示功能,能极高的提升程序员的开发效率,但是毕竟各有所长,idea中tomcat的配置就没有eclipse那么轻松,这里简单 ...
- Nginx系列6之-rewirte功能使用案例总结
使用案例1 网站架构如下: 公司部分服务器目前通过二级目录来调度到不同的服务器,如下所示: 按照上述需求,我们此时就配置了两个location,如下所示: [root@localhost nginx] ...
- 7 云计算系列之Neutron安装与配置
preface 紧接上一章,我们这里开始部署网络服务Neutron,可参考官网文档:http://docs.openstack.org/newton/install-guide-rdo/neutron ...