Detailed analysis of the implementation of shadow effect in Android material design _android

Source: Internet
Author: User

View can cast a shadow, the elevation value of a view determines the size of its shadow and the order in which it is drawn. You can set the elevation of a view to use attributes in a layout: android:elevation

<textview
  android:id= "@+id/my_textview"
  android:layout_width= "wrap_content"
  android:layout_ height= "Wrap_content"
  android:text= "@string/next"
  android:background= "@color/white"
  android: elevation= "5DP"/>

The new Translationz property enables you to create an animation that reflects a temporary elevation change. Elevation changes can be useful in response to touch gestures.
The elevation value of each view, equivalent to the Z property, determines the size of the shadow: the larger the z, the larger the shadow.
Views are projected only on the z=0 plane, and they are not projected onto other views placed underneath and above them.
Views with a higher Z value will block views that use lower Z values.
However, the z value of a view does not affect the size of the view.
Elevation is useful for creating animations that do something when a component needs to be temporarily elevated to the view plane.
For more information on elevation, see http://www.google.com/design/spec/what-is-material/elevation-shadows.html

Assign elevation to the Your views are specified in the views elevation
the z value contains two components: Elevation and translation. Elevation is a static component, translation uses the animation: Z = elevation + Translationz

Set the elevation in layout and use the Android:elevation property. Set in code, using the View.setelevation () method.
Set the translation of a view using the View.settranslationz () method.
Viewpropertyanimator.z () and Viewpropertyanimator.translationz () can make it easier for you to push the views of the elevation.
You can also use a statelistanimator to specify how these animation declarations are made. This is especially useful in situations where state changes trigger animations, such as when a user presses a button.

Customize View Shadows and outlines custom view shadows and Outlines
the border of the view's back picture determines the default graphics for the shadow. The contour represents the external shape of a drawing object and the defined ripple area used for touch feedback.
Reference:

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

A background picture can be 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>

Because the background image defines the view's contour (outline), the view will put a shadow with rounded corners. Provides a custom contour override view's default shadow graphic.
Custom outlines in code:

    • Inherit from Viewoutlineprovider
    • Rewrite Getoutline ()
    • Use View.setoutlineprovider () to assign a new profile

Use some of the methods of the outline class to create rounded rectangles and outlines of ellipses. The default outline provider contains the silhouette of the view. Block a view's projection, you can set the view's Ouline provider to NULL.

Clip views cropping

Trimming the view allows you to easily change the shape of the view. You can tailor it to use other design elements, or change the view shape in response to user input. You can crop the outer contour area of the view, using the View.setcliptooutline () or the Android:cliptooutline property. Only rectangular, rounded, rounded rectangles support contour clipping, determined by the Outline.canclip () method.
When you want to crop a shape to a picture, you need to set the picture as the background of the view and call View.setcliptooutline ().
Cropping is a luxury operation, do not use dynamic graphics to crop. In order to achieve this effect, you can use animation to show.

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.