Android5.0 new Features-shadows and clipping (shadow)

Source: Internet
Author: User
Tags transparent color

The Z property of the shadow and trim view

Material Design recommends using a shadow effect to highlight the layout, and Android L expands the view to make it easier for everyone to create a shadow effect:

Added a new attribute to view: A Z property that describes the height of the view from its parent view:

Before 5.0, our views were two-dimensional, with only the X and Y axes, and now Android has a new z-axis. The x-axis and y-axis describe the size and position of a view, while the z-axis describes the visual that the view lifts on the parent view, reflecting the effect of the shadow. The two view's Z properties are 2DP and 8DP, respectively, for visual effects:

The properties of the view Z can be modified by elevation and Translationz.

z = elevation+translationZ

Before 5.0, if we want to add a shadow effect to the view to reflect its layering, it is common practice to set the view to a shaded background image, now, we just need to simply modify the view's Z properties, we can let it have a shadow of the layered sense.

The Z property expands the display area of the view, and if its size is greater than or equal to the size of the parent view, its shadow effect cannot be displayed, and view does not make room for the shadow by reducing itself by the Z attribute.

The z attribute affects not only the shadow effect of the view, but also the drawing order of the view, and the smaller the Z property within the same parent view, the sooner the drawing time is. That is, priority is drawn, and the larger the Z property, the later the drawing time is drawn, and then the drawing will be obscured, and only the Z property is the same, and it is drawn in the order of addition.

The outline of the view

In the Android world, all the view is a rectangle, although you can set the view background circular picture, the interface to display the circular content, but the size of the view is actually still a rectangle, and set the picture is actually also a rectangle, but the area of the circle unexpectedly transparent color.

If the system generates a shadow for us based on the size of the view, there are sometimes strange effects.

To solve this kind of problem, view adds a new description to indicate the shape of the content display, which is the outline. With the background set by shape, view automatically outlines the shape based on the shapes, and the view defaults to the same size as the view by the background of the color setting. But with the background setting of the picture, the view is not able to know the shape of the contour, this time we need the programmer to display the specified.

In an XML layout, you can android:outlineProvider specify how outlines are judged by:

    1. noneNo shadows are displayed even if the Z property is set
    2. backgroundThe shadow shape is set according to the background
    3. boundsShadows are depicted according to the size of the view
    4. paddedBoundsSimilar to bounds, but the shadows are slightly shifted to the right.

In the code, we can setOutlineProvider specify the outline of a view by:

ViewOutlineProvider viewOutlineProvider = new ViewOutlineProvider() {    public void getOutline(View view, Outline outline) {        // 可以指定圆形,矩形,圆角矩形,path        outline.setOval(0, 0, view.getWidth(), view.getHeight());    }};View.setOutlineProvider(viewOutlineProvider );

注意:如果采用图片作为背景,即使在xml布局中指定android:outlineProvider为background也不会显示阴影,只有通过代码中指定轮廓来显示。

Clipping of view

Specify the outline of the view, you can determine the shape of the shadow, if the view is assigned a smaller than its own size of the outline, then the shadow is usually obscured by the view, this time the view is not due to the outline of the narrowing of the display.

If you want to narrow a view by its outline, you can trim it. If a view specifies an outline, the method is called to setClipToOutline trim a view based on the contour. To trim the outline, you must specify the outline for the view, and the outline can be clipped, currently only rounded, rectangular, rounded rectangles support clipping, and outline.canclip () can be used to determine whether a profile supports clipping.

The path clipping does not change the size of the view, but if the range of path is smaller than the bounds of the view, the clipping changes the position of the view, the position offset and the Z property, which may be a bug,view designer that offsets the canvas based on the contour when the shadow is drawn. And forget to restore the canvas after you've finished drawing it.

Clipping does not change the view's measurement size and layout size, nor does it change the view's touch area, tailoring the canvas only when OnDraw, tailoring it differently than Scale,scale is adjusting the zoom properties of the canvas matrix, and when adjusted, the view is still fully displayed. Trimming is the clipping area of the canvas, and we can only see the part of the view after clipping.

Trying to trim a larger outline of the view is unsuccessful, and the experiment proves that the cropped view can only be smaller than the original size. Enlarging the contour will only enlarge the drawing area of the contour.

Tailoring is a very resource-intensive operation, and we should not use this to animate, and if you want to implement such an animation, you can use reveal Effect

Android5.0 new Features-shadows and clipping (shadow)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.