Android5.0新特性CollapsingToolbarLayout图片拉缩

来源:互联网 发布:数据库是什么文件 编辑:程序博客网 时间:2024/04/19 04:25

源码:http://download.csdn.net/detail/lm_zp/9562621

效果图


compile'com.android.support:design:22.2.0'  赋值到 build.gradle下

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:fitsSystemWindows="true"    tools:context="com.zs.myapplication.NewActivity.DesignActivity">    <android.support.design.widget.AppBarLayout        android:layout_width="match_parent"        android:layout_height="300dp"        android:fitsSystemWindows="true"        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">        <android.support.design.widget.CollapsingToolbarLayout            android:id="@+id/collapsing_toolbar"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:fitsSystemWindows="true"            app:contentScrim="?attr/colorPrimary"            app:expandedTitleMarginEnd="64dp"            app:expandedTitleMarginStart="48dp"            app:layout_scrollFlags="scroll|exitUntilCollapsed">            <ImageView                android:id="@+id/ivImage"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:fitsSystemWindows="true"                android:scaleType="centerCrop"                android:transitionName="tu"                app:layout_collapseMode="parallax"                app:layout_collapseParallaxMultiplier="0.7" />            <android.support.v7.widget.Toolbar                android:id="@+id/toolbar"                android:layout_width="match_parent"                android:layout_height="?attr/actionBarSize"                app:layout_collapseMode="pin"                app:layout_scrollFlags="scroll|enterAlways"                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />        </android.support.design.widget.CollapsingToolbarLayout>    </android.support.design.widget.AppBarLayout>    <android.support.v4.widget.NestedScrollView        android:layout_width="match_parent"        android:layout_height="match_parent"        app:layout_behavior="@string/appbar_scrolling_view_behavior">        <LinearLayout            android:layout_width="match_parent"            android:layout_height="match_parent"            android:orientation="vertical">            <TextView                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_marginTop="50dp"                android:text="1111111111111111111111"                android:transitionName="zi" />            <TextView                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_marginTop="50dp"                android:text="1111111111111111111111" />            <TextView                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_marginTop="50dp"                android:text="1111111111111111111111" />            <TextView                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_marginTop="50dp"                android:text="1111111111111111111111" />            <TextView                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_marginTop="50dp"                android:text="1111111111111111111111" />            <TextView                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_marginTop="50dp"                android:text="1111111111111111111111" />            <TextView                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_marginTop="50dp"                android:text="1111111111111111111111" />            <TextView                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_marginTop="50dp"                android:text="1111111111111111111111" />            <TextView                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_marginTop="50dp"                android:text="1111111111111111111111" />            <TextView                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_marginTop="50dp"                android:text="1111111111111111111111" />            <TextView                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_marginTop="50dp"                android:text="1111111111111111111111" />            <TextView                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_marginTop="50dp"                android:text="1111111111111111111111" />            <TextView                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_marginTop="50dp"                android:text="1111111111111111111111" />            <TextView                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_marginTop="50dp"                android:text="1111111111111111111111" />            <TextView                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_marginTop="50dp"                android:text="1111111111111111111111" />            <TextView                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_marginTop="50dp"                android:text="1111111111111111111111" />            <TextView                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_marginTop="50dp"                android:text="1111111111111111111111" />            <TextView                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_marginTop="50dp"                android:text="1111111111111111111111" />            <TextView                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_marginTop="50dp"                android:text="1111111111111111111111" />        </LinearLayout>    </android.support.v4.widget.NestedScrollView></android.support.design.widget.CoordinatorLayout>

这种效果在详情页面用的较多,展示个性化内容,图像有强烈的吸引力。这个效果重点使用了CollapsingToolbarLayout 。 
CollapsingToolbarLayout可实现Toolbar的折叠效果。CollapsingToolbarLayout的子视图类似与LinearLayout垂直方向排放。

CollapsingToolbarLayout 提供以下属性和方法是用: 
1. Collapsing title:ToolBar的标题,当CollapsingToolbarLayout全屏没有折叠时,title显示的是大字体,在折叠的过程中,title不断变小到一定大小的效果。你可以调用setTitle(CharSequence)方法设置title。 
2. Content scrim:ToolBar被折叠到顶部固定时候的背景,你可以调用setContentScrim(Drawable)方法改变背景或者 在属性中使用 app:contentScrim=”?attr/colorPrimary”来改变背景。 
3. Status bar scrim:状态栏的背景,调用方法setStatusBarScrim(Drawable)。还没研究明白,不过这个只能在Android5.0以上系统有效果。 
4. Parallax scrolling children:CollapsingToolbarLayout滑动时,子视图的视觉差,可以通过属性app:layout_collapseParallaxMultiplier=”0.6”改变。值de的范围[0.0,1.0],值越大视察越大。 
5. CollapseMode :子视图的折叠模式,在子视图设置,有两种“pin”:固定模式,在折叠的时候最后固定在顶端;“parallax”:视差模式,在折叠的时候会有个视差折叠的效果。我们可以在布局中使用属性app:layout_collapseMode=”parallax”来改变。

CoordinatorLayout 还提供了一个 layout_anchor 的属性,连同 layout_anchorGravity 一起,可以用来放置与其他视图关联在一起的悬浮视图(如 FloatingActionButton)。本例中使用FloatingActionButton。


设置为NoActionBar

MainActivity

package com.example.lenovo.mycoordinatorlayoutdemo;import android.os.Bundle;import android.support.design.widget.CollapsingToolbarLayout;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.Toolbar;import android.widget.ImageView;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);       ImageView imageView = (ImageView) findViewById(R.id.ivImage);        imageView.setImageResource(R.drawable.ic_launcher);        // Toolbar代替ActionBar        Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar);        setSupportActionBar(toolbar);        //设置工具栏标题        CollapsingToolbarLayout collapsingToolbarLayout= (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);        collapsingToolbarLayout.setTitle("cheeseName");    }}





1 1
原创粉丝点击