Android-android 5.X new features View and shadows

Source: Internet
Author: User

One of the most important features of Material design is the flattening, which simulates a sporty, bold visual with the use of shadows and light in Material design, combined with the perfect animated effect.

1. Shadow effect

In previous versions of Android, view typically had only two properties--x and Y, while in Android 5.X, Google added a new attribute--z, which corresponds to a height change above the vertical. In Android 5.X, the z-value of view is made up of two parts, elevation and Translationz (which are all newly introduced properties of Android 5.X). Elevation are static members, Translationz can be used in code to achieve animation effects, and their relationship is as follows

Z = elevation + Translationz

We set the view height by using the following properties in the XML layout

Android:elevation = ""

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "vertical">6     <TextView7         android:elevation= "20DP"8 Android:id= "@+id/id_textview"9 Android:background= "#5FF"Ten Android:layout_width= "300DP" One Android:layout_height= "300DP" /> A </LinearLayout>

Effect

Look carefully at a piece, you will find a part of the shadow behind the TextView.

Again, we can use Java code to dynamically change the height of the TextView.

View.settranslationz ();

2.Tinting and clipping

Android 5.X has more features in the operation of the image. Here's a look at the new features--tinting (shading) and clipping (cropping) for Android 5.X's two operating images.

(1) tinting (coloring)

The use of tint is very simple, as long as you configure tint and Tintmode in the XML code.

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:gravity= "Center"6 android:orientation= "vertical">7     <ImageView8         android:src= "@mipmap/ic_launcher"9 Android:layout_width= "Wrap_content"Ten Android:layout_height= "Wrap_content" /> One     <ImageView A         Android:tint= "#5FF" - android:src= "@mipmap/ic_launcher" - Android:layout_width= "Wrap_content" the Android:layout_height= "Wrap_content" /> -     <ImageView -         Android:tintmode= "Add" - Android:tint= "#5FF" + android:src= "@mipmap/ic_launcher" - Android:layout_width= "Wrap_content" + Android:layout_height= "Wrap_content" /> A     <ImageView at         Android:tintmode= "Multiply" - Android:tint= "#5FF" - android:src= "@mipmap/ic_launcher" - Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" /> -     <ImageView in         Android:tintmode= "Screen" - Android:tint= "#5FF" to android:src= "@mipmap/ic_launcher" + Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" /> the  * </LinearLayout>

Effect

(2) cliping (crop)

Clipping allows us to change the shape of a view. To use clipping, you first need to use Viewoutlineprovider to modify the outline, and then Setoutlineprovider outline to the view

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:gravity= "Center"6 android:orientation= "vertical">7 8     <TextView9         Android:id= "@+id/id_textview1"Ten android:elevation= "1DP" One Android:layout_width= "200DP" A Android:layout_height= "200DP" -         /> -  the     <TextView -         Android:id= "@+id/id_textview2" - android:elevation= "1DP" - Android:layout_width= "200DP" + Android:layout_height= "200DP" - Android:layout_margintop= "10DP" +         /> A </LinearLayout>
1  Public classMainactivityextendsappcompatactivity {2     PrivateTextView MTextView1 =NULL;3     PrivateTextView mTextView2 =NULL;4 5 @Override6     protected voidonCreate (@Nullable Bundle savedinstancestate) {7         Super. OnCreate (savedinstancestate);8 Setcontentview (r.layout.activity_main);9MTextView1 =(TextView) Findviewbyid (r.id.id_textview1);TenMTextView2 =(TextView) Findviewbyid (R.ID.ID_TEXTVIEW2); OneViewoutlineprovider Viewoutlineprovider =NewViewoutlineprovider () { A @Override -              Public voidgetoutline (view view, Outline Outline) { -                 //crop to rounded rectangle theOutline.setroundrect (0, 0, view.getwidth (), View.getheight (), 30); -             } -         }; -Viewoutlineprovider ViewOutlineProvider1 =NewViewoutlineprovider () { + @Override -              Public voidgetoutline (view view, Outline Outline) { +                 //Crop to Circle AOutline.setoval (0, 0, View.getwidth (), View.getheight ()); at             } -         }; - Mtextview1.setoutlineprovider (viewoutlineprovider); - Mtextview2.setoutlineprovider (viewOutlineProvider1); -     } -}

Effect

Note that if the use of the above method can not be successfully cropped, try to change the Min API to 21 and above, so try again

Android-android 5.X new features View and shadows

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.