Material Design系列第四篇——Using the Material Theme

Defining Shadows and Clipping Views

This lesson teaches you to

  1. Assign Elevation to Your Views
  2. Customize View Shadows and Outlines
  3. Clip Views

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:

  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.

时间: 2024-08-13 23:02:59

Material Design系列第四篇——Using the Material Theme的相关文章

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

Using the Material Theme This lesson teaches you to Customize the Color Palette Customize the Status Bar Theme Individual Views You should also read Material design specification Material design on Android The new material theme provides: System widg

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

Defining Custom Animations This lesson teaches you to Customize Touch Feedback Use the Reveal Effect Customize Activity Transitions Animate View State Changes Animate Vector Drawables You should also read Material design specification Material design

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

Working with Drawables This lesson teaches you to Tint Drawable Resources Extract Prominent Colors from an Image Create Vector Drawables You should also read Material design specification Material design on Android The following capabilities for draw

Material Design系列第七篇——Maintaining Compatibility

Maintaining Compatibility This lesson teaches you to Define Alternative Styles Provide Alternative Layouts Use the Support Library Check the System Version You should also read Material design specification Material design on Android Some material de

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

今天这篇文章应该算是Material Design系列的补充篇,因为这篇文章本来应该放到前面讲的,因为讲的是主题嘛,对于一些状态和颜色的介绍,因为我们一新建一个项目时,系统自带了三个属性的颜色,现在就重点介绍这三个颜色属性的意义和作用.讲明白这个,留着以后讲别的用. 最常用的三个颜色属性 colorPrimary colorPrimaryDark colorAccent 这三个分别代表什么意思呢? colorPrimaryDark 是状态栏底色 colorPrimary 如果你不手动自己去修改t

Material Design系列,自定义Behavior支持所有View

Material Design系列,自定义Behavior支持所有View 版权声明:转载必须注明本文转自严振杰的博客: http://blog.csdn.net/yanzhenjie1003 友情连接: Material Design博客专栏 系列博客: 1. Material Design系列,Behavior之BottomSheetBehavior与BottomSheetDialog 2. Material Design系列,Behavior之SwipeDismissBehavior 3.

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

Creating Apps with Material Design Material design is a comprehensive guide for visual, motion, and interaction design across platforms and devices. To use material design in your Android apps, follow the guidelines described in the material design s

Material Design系列第二篇——Getting Started

Getting Started This lesson teaches you to Apply the Material Theme Design Your Layouts Specify Elevation in Your Views Create Lists and Cards Customize Your Animations You should also read Material design specification Material design on Android To

深入理解javascript作用域系列第四篇——块作用域

× 目录 [1]let [2]const [3]try 前面的话 尽管函数作用域是最常见的作用域单元,也是现行大多数javascript最普遍的设计方法,但其他类型的作用域单元也是存在的,并且通过使用其他类型的作用域单元甚至可以实现维护起来更加优秀.简洁的代码,比如块作用域.随着ES6的推广,块作用域也将用得越来越广泛.本文是深入理解javascript作用域系列第四篇——块作用域 let for (var i= 0; i<10; i++) { console.log(i); } 上面这段是很熟