Android (Lollipop/5.0) Material Design (I) Introduction
To use Material Design, api21 is required, that is, Lollipop/5.0 or above.
Material Design provides applications with a new topic, new widgets that combine views, and new APIs for custom shadows and animations.
Material topic
In manifest. xml, <... android: theme = @ android: style/Theme. Material/> prompts that there are many related topics.
Lists and Cards
5.0 provides two new widgets that use the style and animation of Material Design:
RecyclerView is a more pluggable ListView that supports different layout types and improves performance. Simplified
CardView a card that allows you to display important information in it and maintain consistent vision and feeling
It is located in sdk/extras/android/support/v7/cardview and sdk/extras/android/support/v7/RecyclerView
View shadow
In addition to the x and y attributes, the View also has z, which represents the elevation of an view (elevation, let's translate it like this)
The larger the value of z, the larger the shadow. The larger the value of z, the view will appear at the top of other views.
Animation
The new animation Api allows you to create touch feedback in the UI control, change the View status, and switch a series of custom animations of the activity.
Specifically:
Touch feedback animation for touch events in response to View
Hide and display the circular animation of a View
Animation for switching between two activities
More natural animated curve movement
You can use the state change animation of a View to change attributes of one or more views.
Displays the status list animation when the View status changes.
These new animations APIs have been built into standard widgets, such as buttons. You can also use these Apis when customizing a view.
Image
Scalable Vector images do not lose definition, and the app-icon in a single color is perfect.
You can define a bitmap as the transparency (alpha) and runtime color.
You can color a bitmap image to retrieve its conspicuous color.
Example of RecyclerView:
[Java]View plaincopyprint?
- Import android. app. Activity;
- Import android. OS. Bundle;
- Import android. support. v7.widget. GridLayoutManager;
- Import android. support. v7.widget. RecyclerView;
- Import android. support. v7.widget. RecyclerView. LayoutParams;
- Import android. view. LayoutInflater;
- Import android. view. ViewGroup;
- Import android. widget. TextView;
-
- Public class RecyclerViewActivity extends Activity {
- /*
- * Recyclerview provides these built-in layout managers:
- * Linearlayoutmanager displays the vertical scrolling list or horizontal items.
- * Gridlayoutmanager is displayed in a grid project.
- * Staggeredgridlayoutmanager is displayed in a staggered grid project.
- * The custom layout manager must inherit the recyclerview. layoutmanager class.
- *
- * The animation is enabled by default when you add/remove items.
- * To customize these animations, you must inherit RecyclerView. ItemAnimator and implement RecyclerView. setItemAnimator ()
- */
- Private RecyclerView mRecyclerView;
- Private RecyclerView. Adapter mAdapter;
- Private RecyclerView. LayoutManager mLayoutManager;
- Private String [] myDataset;
-
- @ Override
- Protected void onCreate (Bundle savedInstanceState ){
- Super. onCreate (savedInstanceState );
-
- SetContentView (R. layout. recycler_view );
- MRecyclerView = (RecyclerView) findViewById (R. id. my_recycler_view );
-
- // Use this setting to improve performance if you know that changes
- // In content do not change the layout size of the RecyclerView
- MRecyclerView. setHasFixedSize (true );
-
- // Use a linear layout manager
- // MLayoutManager = new LinearLayoutManager (this );
-
- // MLayoutManager = new GridLayoutManager (this, 3, GridLayoutManager. VERTICAL, true );
- // True indicates that layout content is reversed.
- MLayoutManager = new GridLayoutManager (this, 3, GridLayoutManager. VERTICAL, false );
- // HORIZONTAL scrolling display VERTICAL
- // MLayoutManager = new GridLayoutManager (this, 3, GridLayoutManager. HORIZONTAL, false );
-
- // The direction also indicates the scroll direction. In this example, the data starting from the horizontal direction is a little staggered and the vertical direction is not staggered.
- // MLayoutManager = new StaggeredGridLayoutManager (3, StaggeredGridLayoutManager. HORIZONTAL );
- // MLayoutManager = new StaggeredGridLayoutManager (4, StaggeredGridLayoutManager. VERTICAL );
-
- MRecyclerView. setLayoutManager (mLayoutManager );
- // MRecyclerView. setLayoutManager (new MyLayoutMnager (); // The data is not displayed and you may need to rewrite something ..
-
- // Specify an adapter (see also next example)
-
- SetDatas ();
- MAdapter = new MyAdapter (myDataset );
- MRecyclerView. setAdapter (mAdapter );
- }
-
- Private void setDatas (){
- Int len = 200;
- MyDataset = new String [len];
- For (int I = 0; I <len; I ++ ){
- Switch (I % 3 ){
- Case 0:
- MyDataset [I] = China + I;
- Break;
- Case 1:
- MyDataset [I] = US + I;
- Break;
- Case 2:
- MyDataset [I] = Australia + I;
- Break;
- }
- }
- }
-
- Class MyLayoutMnager extends RecyclerView. LayoutManager {
-
- @ Override
- Public LayoutParams generateDefaultLayoutParams (){
- LayoutParams params = new LayoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT );
- Params. topMargin = 5;
- Return params;
- }
- }
-
- Class MyAdapter extends RecyclerView. Adapter {
- Private String [] mDataset;
-
- // Provide a reference to the views for each data item
- // Complex data items may need more than one view per item, and
- // You provide access to all the views for a data item in a view holder
-
- // Provide a suitable constructor (depends on the kind of dataset)
- Public MyAdapter (String [] myDataset ){
- MDataset = myDataset;
- }
-
- // Create new views (invoked by the layout manager)
- @ Override
- Public ViewHolder onCreateViewHolder (ViewGroup parent, int viewType ){
- // Create a new view
- TextView TV = (TextView) LayoutInflater. from (parent. getContext ())
- . Inflate (R. layout. my_text_view, parent, false );
- // Set the view's size, margins, paddings and layout parameters
- //...
- ViewHolder h_= new ViewHolder (TV); // construct a ViewHolder
- Return VL;
- }
-
- // Replace the contents of a view (invoked by the layout manager)
- @ Override
- Public void onBindViewHolder (ViewHolder holder, int position ){
- //-Get element from your dataset at this position
- //-Replace the contents of the view with that element
- Holder. mTextView. setText (mDataset [position]);
-
- }
-
- // Return the size of your dataset (invoked by the layout manager)
- @ Override
- Public int getItemCount (){
- Return mDataset. length;
- }
- }
-
- Static class ViewHolder extends RecyclerView. ViewHolder {
- // Each data item is just a string in this case
- Public TextView mTextView;
- Public ViewHolder (TextView v ){
- Super (v );
- MTextView = v;
- }
- }
- }