Android视差效果

简介: 这次带来的是结合CoordinatorLayout、AppBarLayout、CollapsingToolbarLayout和FloatingActionButton打造炫酷的视觉差效果。

这次带来的是结合CoordinatorLayout、AppBarLayout、CollapsingToolbarLayout和FloatingActionButton打造炫酷的视觉差效果。

img_6c7d281d6c14fb3c1d022a5c5bf6d2ab.gif

实现步骤

一:需要在主题文件中将Activity的标题栏去掉

img_56c89714ddf6a17ed61a06b471983533.png

二:编写Activity的布局文件

<?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"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            app:contentScrim="@color/colorPrimary"
            app:expandedTitleMarginStart="15dp"
            app:collapsedTitleTextAppearance="@style/ToolbarTextStyle" //设置收缩时的标题样式
            app:expandedTitleTextAppearance="@style/CollapseexpandedTextStyle" //设置展开时的标题样式
            app:layout_scrollFlags="scroll|exitUntilCollapsed" //这个属性使得CollapsingToolbarLayout在滑动的时候显示隐藏
            android:layout_height="wrap_content">
            <ImageView
                android:layout_width="match_parent"
                android:src="@mipmap/bg"
                android:scaleType="fitXY"
                app:layout_collapseMode="parallax" //这个属性使得ImageView在CollapsingToolbarLayout展开的时候显示
                android:layout_height="200dp" />
            <android.support.v7.widget.Toolbar
                android:layout_width="match_parent"
                android:id="@+id/toolbar"
                app:layout_collapseMode="pin" //这个属性使得Toolbar在CollapsingToolbarLayout收缩的时候显示
                android:layout_height="50dp">
            </android.support.v7.widget.Toolbar>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:id="@+id/recycle"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" //这个属性是让控件在AppBarLayout下边
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>
    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:src="@drawable/ic_logo"
        app:layout_anchor="@id/appbar" //这个属性是让FloatingActionButton以AppBarLayout为锚点,同时这也是使FloatingActionButton实现显示隐藏的关键
        app:layout_anchorGravity="bottom|end" //在AppBarLayout的右下方
        app:borderWidth="0dp"
        app:rippleColor="#E23B74"
        app:elevation="6dp"
        app:pressedTranslationZ="12dp"
        android:clickable="true"
        android:layout_marginRight="10dp"
        android:layout_height="wrap_content" />
</android.support.design.widget.CoordinatorLayout>

style/ToolbarTextStyle和style/CollapseexpandedTextStyle

 <style name="CollapseexpandedTextStyle" parent="TextAppearance.AppCompat">
        <item name="android:textColor">#fff</item>
        <item name="android:textSize">23sp</item>
    </style>
    <style name="ToolbarTextStyle" parent="TextAppearance.AppCompat">
        <item name="android:textColor">#fff</item>
        <item name="android:textSize">18sp</item>
</style>

三:编写Activity代码

public class MainActivity extends AppCompatActivity {
    Toolbar toolbar;
    RecyclerView recyclerView;
    AppBarLayout appBarLayout;
    private BaseQuickAdapter<String, BaseViewHolder> baseQuickAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar=findViewById(R.id.toolbar);
        appBarLayout=findViewById(R.id.appbar);
        toolbar.setTitle("我是标题");
        setSupportActionBar(toolbar);
//        设置条目(可以省略)========================
        recyclerView=findViewById(R.id.recycle);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        final ArrayList<String> strings = new ArrayList<>();
        for(int x=0;x<10;x++){
            strings.add("我是条目"+x);
        }
        baseQuickAdapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item, strings) {
            @Override
            protected void convert(BaseViewHolder helper, String item) {
                helper.setText(R.id.tv_title, item);
            }
        };
        baseQuickAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        baseQuickAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    
            }
        });
        recyclerView.setAdapter(baseQuickAdapter);
//        ==================================================
        appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
            @Override
            public void onStateChanged(AppBarLayout appBarLayout, State state) {
                if( state == State.EXPANDED ) {
                    //展开状态
                    toolbar.setNavigationIcon(R.drawable.ic_backfour);
                }else if(state == State.COLLAPSED){
                    //折叠状态
                    toolbar.setNavigationIcon(R.drawable.ic_back);
                }else {
                    //中间状态
                    toolbar.setNavigationIcon(R.drawable.ic_back);
                }
            }
        });

    }
    private abstract static class AppBarStateChangeListener implements AppBarLayout.OnOffsetChangedListener {
        public enum State {
            EXPANDED,
            COLLAPSED,
            IDLE
        }

        private State mCurrentState = State.IDLE;
        @Override
        public final void onOffsetChanged(AppBarLayout appBarLayout, int i) {
            if (i == 0) {
                if (mCurrentState != State.EXPANDED) {
                    onStateChanged(appBarLayout, State.EXPANDED);
                }
                mCurrentState = State.EXPANDED;
            } else if (Math.abs(i) >= appBarLayout.getTotalScrollRange()) {
                if (mCurrentState != State.COLLAPSED) {
                    onStateChanged(appBarLayout, State.COLLAPSED);
                }
                mCurrentState = State.COLLAPSED;
            } else {
                if (mCurrentState != State.IDLE) {
                    onStateChanged(appBarLayout, State.IDLE);
                }
                mCurrentState = State.IDLE;
            }
        }
        public abstract void onStateChanged(AppBarLayout appBarLayout, State state);
    }
  }

目录
相关文章
|
17天前
|
Java Android开发
Android 开发获取通知栏权限时会出现两个应用图标
Android 开发获取通知栏权限时会出现两个应用图标
12 0
|
1月前
|
XML 缓存 Android开发
Android开发,使用kotlin学习多媒体功能(详细)
Android开发,使用kotlin学习多媒体功能(详细)
89 0
|
2月前
|
Android开发
安卓SO层开发 -- 编译指定平台的SO文件
安卓SO层开发 -- 编译指定平台的SO文件
29 0
|
1月前
|
设计模式 人工智能 开发工具
安卓应用开发:构建未来移动体验
【2月更文挑战第17天】 随着智能手机的普及和移动互联网技术的不断进步,安卓应用开发已成为一个热门领域。本文将深入探讨安卓平台的应用开发流程、关键技术以及未来发展趋势。通过分析安卓系统的架构、开发工具和框架,本文旨在为开发者提供全面的技术指导,帮助他们构建高效、创新的移动应用,以满足不断变化的市场需求。
17 1
|
3月前
|
Android开发 开发者 iOS开发
APP开发后如何上架,上架Android应用市场前要准备什么
移动应用程序(APP)的开发已经成为现代企业和开发者的常见实践。然而,开发一个成功的APP只是第一步,将其上架到应用商店让用户下载和使用是实现其潜力的关键一步。
|
1月前
|
机器学习/深度学习 调度 Android开发
安卓应用开发:打造高效通知管理系统
【2月更文挑战第14天】 在移动操作系统中,通知管理是影响用户体验的关键因素之一。本文将探讨如何在安卓平台上构建一个高效的通知管理系统,包括服务、频道和通知的优化策略。我们将讨论最新的安卓开发工具和技术,以及如何通过这些工具提高通知的可见性和用户互动性,同时确保不会对用户造成干扰。
31 1
|
8天前
|
XML 开发工具 Android开发
构建高效的安卓应用:使用Jetpack Compose优化UI开发
【4月更文挑战第7天】 随着Android开发不断进化,开发者面临着提高应用性能与简化UI构建流程的双重挑战。本文将探讨如何使用Jetpack Compose这一现代UI工具包来优化安卓应用的开发流程,并提升用户界面的流畅性与一致性。通过介绍Jetpack Compose的核心概念、与传统方法的区别以及实际集成步骤,我们旨在提供一种高效且可靠的解决方案,以帮助开发者构建响应迅速且用户体验优良的安卓应用。
|
17天前
|
Android开发
Android开发小技巧:怎样在 textview 前面加上一个小图标。
Android开发小技巧:怎样在 textview 前面加上一个小图标。
10 0
|
17天前
|
Android开发
Android 开发 pickerview 自定义选择器
Android 开发 pickerview 自定义选择器
10 0
|
24天前
|
Java Android开发
Android开发系列全套课程
本系列课程面向有java基础,想进入企业从事android开发的计算机专业者。学习搭配实战案例,高效掌握岗位知识。
16 1