Defining Shadows and Clipping Views

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:

  1. Extend the ViewOutlineProvider class.
  2. Override the getOutline() method.
  3. 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的更多相关文章

  1. Material Design系列第六篇——Defining Custom Animations

    Defining Custom Animations //自定义动画 This lesson teaches you to //本节课知识点 Customize Touch Feedback //1. ...

  2. Material Design系列第八篇——Creating Lists and Cards

    Creating Lists and Cards //创建列表和卡片 To create complex lists and cards with material design styles in ...

  3. Material Design系列第七篇——Maintaining Compatibility

    Maintaining Compatibility This lesson teaches you to Define Alternative Styles Provide Alternative L ...

  4. Material Design系列第五篇——Working with Drawables

    Working with Drawables This lesson teaches you to Tint Drawable Resources Extract Prominent Colors f ...

  5. Material Design系列第三篇——Using the Material Theme

    Using the Material Theme This lesson teaches you to Customize the Color Palette Customize the Status ...

  6. Creating Apps With Material Design —— Defining Shadows and Clipping Views

    View转载请注明 http://blog.csdn.net/eclipsexys 翻译自Developer Android,时间仓促,有翻译问题请留言指出,谢谢 定义阴影和裁减 材料设计引入了深度的 ...

  7. Material Design系列第二篇——Getting Started

    Getting Started This lesson teaches you to Apply the Material Theme Design Your Layouts Specify Elev ...

  8. Material Design系列第一篇——Creating Apps with Material Design

    Creating Apps with Material Design //创建Material Design的App Material design is a comprehensive guide ...

  9. Android Material Design系列之主题样式介绍说明

    今天这篇文章应该算是Material Design系列的补充篇,因为这篇文章本来应该放到前面讲的,因为讲的是主题嘛,对于一些状态和颜色的介绍,因为我们一新建一个项目时,系统自带了三个属性的颜色,现在就 ...

随机推荐

  1. Java如何打印异常的堆栈?

    在Java编程中,如何打印异常的堆栈? 此示例显示如何使用异常类的printStack()方法打印异常的堆栈. package com.yiibai; public class PrintStackT ...

  2. Spring JDBC ResultSetExtractor接口示例

    org.springframework.jdbc.core.ResultSetExtractor接口是JdbcTemplate的查询方法使用的回调接口.此接口的实现执行从ResultSet提取结果的实 ...

  3. e810. 创建弹出菜单

    final JPopupMenu menu = new JPopupMenu(); // Create and add a menu item JMenuItem item = new JMenuIt ...

  4. thinkphp中的类库与引用import引入机制

    ThinkPHP的类库包括基类库和应用类库 控制器类 模块名+Action 例如 UserAction.InfoAction 模型类 模型名+Model 例如 UserModel.InfoModel ...

  5. datepicker clone 控件错误

    删除id,并删除hasDatepicker //+ -  function changeRows(sender,desc){ var tr = $(sender).closest("tr&q ...

  6. asp.net导出excel 问题及服务器的部署dcom组件配置

    一.服务器上没有装office 如果要用MS的,这个问题基本不用考虑,只有安装才能解决,没有其它办法! (即使有牛人弄出来 了,估计也是给自己找麻烦) 不过,我只在服务器上装了一个2003精简版, 我 ...

  7. 解决win8.1电脑无法安装手机驱动问题

    相信安装过win8.1的朋友都会有个问题,那就是自己的安卓手机怎么都连不上电脑,比如360助手.豌豆荚.91助手,都安不上驱动.下面几个简单步骤即可轻松解决. 1.首先声明,官方的驱动是完全支持win ...

  8. 自动换行后缩进怎么做(CSS)?(可用于 Li y 元素的排版)

    <style type="text/css">li{ width:100px; border:1px solid #ccc; padding-left:25px; te ...

  9. 教你ABBYY FineReader 12添加图像的技巧

    ABBYY FineReader 12是一款OCR图片文字识别软件,而且强大的它现在还可使用快速扫描窗口中的快速打开.扫描并保存为图像或任务自动化任务,在没有进行预处理和OCR的ABBYY FineR ...

  10. QT QTransform与QMatrix 有啥区别?

    刚开始学习QT,我使用的是QT5.12进行开发,要不时地查阅QT的官方帮助文档~ 仔细阅读QT官方帮助QTransform类以及QMatrix类,发现两个类的作用描述一模一样(“The QTransf ...