Android Meterial Design-Defining Shadows and Clipping Views (defines shadow and cropping Views)-(4), deepawmeterial

Source: Internet
Author: User

Android Meterial Design-Defining Shadows and Clipping Views (defines shadow and cropping Views)-(4), deepawmeterial

Reprinted please indicate the source: http://blog.csdn.net/bbld_/article/details/40539131

 

Material Design introduces deep UI elements. It helps users understand the relative importance of each element and focus on what they are doing.

A view (control), represented by the elevation of the Z attribute, determines the size of its shadow. A view with a higher Z value has a larger shadow. Views only cast shadows on the Z = 0 plane; they are not projected under them and other views above the Z = 0 plane.

A view with a higher Z value (Control) Blocks A view with a lower Z value. However, the Z value of a view does not affect the size of the view.

When the control temporarily performs some operations over the plane view, elevation is useful for creating an animation.

 

Specify the elevation (elevatio) in your view (Control)N)

The Z value of a view consists of two parts: elevation and conversion. The elevation is a static component, and the conversion is used for animation.

Z = elevation + translationZ


Figure 1: Shadow of a view control with different elevation attribute values

To set the elevation of the View Control in the layout, use the android: elevation attribute. To set the View elevation in the code, use the View. setElevation () method.

To set a View conversion (translation), use the View. setTranslationZ () method.

The new ViewPropertyAnimator. z () and ViewPropertyAnimator. translationZ () methods allow you to easily change the view elevation. For more information, see the ViewPropertyAnimator API documentation and the property animation developer guide.

You can also use StateListAnimator to specify these animations in a declarative manner. This is particularly useful when an animation is triggered when the state changes. For example, a user presses a button. For more information, see Animate View State Changes (Defining Custom Animations) (not updated ).

The Z value is measured in the same unit as the X and Y values.

 

Custom view shadows and outlines

The background of a view determines the default shape of its shadow.ProfileRepresents the external shape of the graphic object, and limits the ripple area of the touch feedback.

See the following view (TextView) to define the background image resources:

<TextView    android:id="@+id/myview"    ...    android:elevation="2dp"    android:background="@drawable/myrect" />

The background resource is defined as a rectangle with rounded corners.

<!-- res/drawable/myrect.xml --><shape xmlns:android="http://schemas.android.com/apk/res/android"       android:shape="rectangle">    <solid android:color="#42000000" />    <corners android:radius="5dp" /></shape>

The view control is shaded with rounded corners because the background image resource defines the outline of the view. A custom profile is provided to overwrite the default shape of the view shadow.

Customize the view outline in your code:

1. inherit the ViewOutlineProvider class

2. Override the getOutline () method.

3. Use the View. setOutlineProvider () method to allocate a new outline to your View.

Using methods in the Outline class, you can create an elliptical and rectangular shape with rounded corners. The default profile provider (provider) of a view obtains its profile from the view background. To prevent a view from being shaded, set its profile provider to null.


Cropping View

Cropping a view allows you to easily change the view shape. You can use other design elements to crop a view for consistency or change the view shape in response to user input. You can use the View. setClipToOutline () method or the android: clipToOutline attribute to crop the View's own profile shape. Only the contour of the rectangle, circle, and rounded rectangle can be cropped. This is determined by the Outline. canClip () method.

To crop an image resource of a specified shape in a View, set the background resource of the View (as shown above) and call the View. setClipToOutline method.

Cropping a view is a costly operation, so do not crop the view to make the shape look vivid. To achieve this Effect, you should use the Reveal Effect (Defining Custom Animations) (not updated) animation.




The demo is relatively simple, so the above two pieces of code won't be pasted.














Related Article

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.