转载请注明出处 http://blog.csdn.net/xiaoyuan511

一、概述

现在大多数的电商APP的详情页长得几乎都差不多,几乎都是上面一个商品的图片,当你滑动的时候,会有Tab悬浮在上面,这样做用户体验确实不错,如果Tab滑上去,用户可能还需要滑下来,在来点击Tab,这样确实很麻烦。沉浸式状态栏那,郭霖说过谷歌并没有给出沉浸式状态栏这个明白,谷歌只说了沉浸式模式(Immersive Mode)。不过沉浸式状态栏这个名字其实听不粗,随大众吧,但是Android的环境并没有IOS环境一样特别统一,比如华为rom的跟小米rom的虚拟按键完全不一样,所有Android开发者不容易。。。。。

二、淘宝的效果

三、我们的效果


只能传2M,把我的美女都给压失真了。。。。。。

四、实现类

  • 自定义ScrollView (StickyScrollView)

  • StatusBarUtil //非常不错的状态栏工具

五、布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"><FrameLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"><com.xiaoyuan.StickyScrollViewandroid:id="@+id/scrollView"android:layout_width="match_parent"android:layout_height="match_parent"android:focusable="true"android:focusableInTouchMode="true"><LinearLayoutandroid:id="@+id/ll_content"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><ImageViewandroid:layout_width="match_parent"android:layout_height="500dip"android:background="@mipmap/meinv"/><TextViewandroid:id="@+id/title"android:layout_width="match_parent"android:layout_height="50dp"android:gravity="center"android:text="美" /><TextViewandroid:layout_width="match_parent"android:layout_height="50dip"android:gravity="center"android:text="女"/><TextViewandroid:layout_width="match_parent"android:layout_height="50dip"android:gravity="center"android:text="美"/><TextViewandroid:layout_width="match_parent"android:layout_height="50dip"android:gravity="center"android:text="不"/><TextViewandroid:layout_width="match_parent"android:layout_height="50dip"android:gravity="center"android:text="美"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:tag="sticky"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="45dp"android:background="#ffffff"android:orientation="horizontal"><TextViewandroid:id="@+id/infoText"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:text="美女信息"android:textColor="#000000"android:textSize="16dp" /><TextViewandroid:id="@+id/secondText"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:text="美女介绍"android:textColor="#000000"android:textSize="16dp" /></LinearLayout></LinearLayout><FrameLayoutandroid:id="@+id/tabMainContainer"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#ffffff"android:minHeight="400dp"></FrameLayout></LinearLayout></com.xiaoyuan.StickyScrollView><RelativeLayoutandroid:id="@+id/ll_good_detail"android:layout_width="match_parent"android:layout_height="49dp"android:background="#00000000"android:paddingTop="@dimen/spacing_normal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="#ffffff"android:layout_alignParentLeft="true"android:layout_marginLeft="10dip"android:layout_centerHorizontal="true"android:text="返回"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="#ffffff"android:layout_centerInParent="true"android:layout_centerHorizontal="true"android:layout_marginLeft="10dip"android:text="美女"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="#ffffff"android:layout_alignParentRight="true"android:layout_marginRight="10dip"android:layout_centerHorizontal="true"android:text="分享"/></RelativeLayout></FrameLayout></RelativeLayout>

注意:我们把要悬浮的Tab设置了android:tag=”sticky”这样的属性

六、实现代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener, StickyScrollView.OnScrollChangedListener {TextView oneTextView, twoTextView;
private StickyScrollView stickyScrollView;
private int height;
private LinearLayout llContent;
private RelativeLayout llTitle;
private FrameLayout frameLayout;
private TextView title;@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();initListeners();}/*** 初始化View*/
private void initView() {stickyScrollView = (StickyScrollView) findViewById(R.id.scrollView);frameLayout = (FrameLayout) findViewById(R.id.tabMainContainer);title = (TextView) findViewById(R.id.title);oneTextView = (TextView) findViewById(R.id.infoText);llContent = (LinearLayout) findViewById(R.id.ll_content);llTitle = (RelativeLayout) findViewById(R.id.ll_good_detail);oneTextView.setOnClickListener(this);twoTextView = (TextView) findViewById(R.id.secondText);twoTextView.setOnClickListener(this);stickyScrollView.setOnScrollListener(this);StatusBarUtil.setTranslucentForImageView(MainActivity.this, 0, title);FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) llTitle.getLayoutParams();params.setMargins(0, getStatusHeight(), 0, 0);llTitle.setLayoutParams(params);//默认设置一个FrggetSupportFragmentManager().beginTransaction().replace(R.id.tabMainContainer, Fragment.newInstance()).commit();
}/*** 获取状态栏高度* @return*/
private int getStatusHeight() {int resourceId = MainActivity.this.getResources().getIdentifier("status_bar_height", "dimen", "android");return getResources().getDimensionPixelSize(resourceId);}@Override
public void onClick(View v) {if (v.getId() == R.id.infoText) {getSupportFragmentManager().beginTransaction().replace(R.id.tabMainContainer, Fragment.newInstance()).commit();} else if (v.getId() == R.id.secondText) {getSupportFragmentManager().beginTransaction().replace(R.id.tabMainContainer, Fragment1.newInstance()).commit();}
}private void initListeners() {//获取内容总高度final ViewTreeObserver vto = llContent.getViewTreeObserver();vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {@Overridepublic void onGlobalLayout() {height = llContent.getHeight();//注意要移除llContent.getViewTreeObserver().removeGlobalOnLayoutListener(this);}});//获取Fragment高度ViewTreeObserver viewTreeObserver = frameLayout.getViewTreeObserver();viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {@Overridepublic void onGlobalLayout() {height = height - frameLayout.getHeight();//注意要移除frameLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);}});//获取title高度ViewTreeObserver viewTreeObserver1 = llTitle.getViewTreeObserver();viewTreeObserver1.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {@Overridepublic void onGlobalLayout() {height = height - llTitle.getHeight() - getStatusHeight();//计算滑动的总距离stickyScrollView.setStickTop(llTitle.getHeight() + getStatusHeight());//设置距离多少悬浮//注意要移除llTitle.getViewTreeObserver().removeGlobalOnLayoutListener(this);}});}@Override
public void onScrollChanged(int l, int t, int oldl, int oldt) {if (t <= 0) {llTitle.setBackgroundColor(Color.argb((int) 0, 255, 255, 255));StatusBarUtil.setTranslucentForImageView(MainActivity.this, 0, title);} else if (t > 0 && t <= height) {float scale = (float) t / height;int alpha = (int) (255 * scale);llTitle.setBackgroundColor(Color.argb((int) alpha, 227, 29, 26));//设置标题栏的透明度及颜色StatusBarUtil.setTranslucentForImageView(MainActivity.this, alpha, title);//设置状态栏的透明度} else {llTitle.setBackgroundColor(Color.argb((int) 255, 227, 29, 26));StatusBarUtil.setTranslucentForImageView(MainActivity.this, 255, title);}
}}

注意:stickyScrollView.setStickTop(int height)我们通过这个方法可以设置Tab距离多高开始悬浮

我们通过监听ScrollView滑动距离来不断改变我们标题栏跟状态栏的透明度来达到效果,在这里我们计算了几个高度(滑动距离)。最后来算出滑动总距离,根据滑动的距离跟滑动的总距离来算出透明度的数值。

StatusBarUtil.setTranslucentForImageView(MainActivity.this, 0, title);我们通过工具来实现图片深入状态栏。里面的传的View是图片下面的View。

六、总结

效果倒是不错,美女也不错、但是在Android4.4之前根本就没有沉浸式这个东西,大家可以下载源码来研究。自己动手实现一遍记得比较清楚。工作了。太忙了。最后感谢一下dota群的高叔(博客地址不知道)提供思路。

七、源码

csdn下载
githu下载

八、欢迎大家访问我的网站和我的公众号

极客导航—程序员自己的导航网站

极客导航

欢迎关注我的公众号

Android 沉浸式状态栏及悬浮效果相关推荐

  1. 高大上的Android沉浸式状态栏?

    背景 之前做过Android沉浸式状态栏的相关需求,但是一直忙于工作,没时间系统的整理下沉浸式相关的知识,所以今天抽出时间,写一篇 Android沉浸式状态栏的文章. 何为沉浸式 沉浸式就是要给用户提 ...

  2. android 5.0状态栏下载地址,Android沉浸式状态栏(5.0以上系统)

    Android沉浸式状态栏(5.0以上系统) 沉浸式状态栏可以分为两种: 1.直接给状态栏设置颜色 (如下图:) 这里写图片描述 java代码形式: if (Build.VERSION.SDK_INT ...

  3. android 沉浸式开源库,Android沉浸式状态栏

    关于Android沉浸式状态栏, 网上已经有很多开源库, 虽然开源库可以解决某些特定布局下的沉浸式状态栏问题, 但是遇到比较特殊的布局就无法解决了, 所以了解一下沉浸式状态栏如何实现是有必要的. 无论 ...

  4. android沉浸式 字体,Android沉浸式状态栏背景色以及字体颜色的修改

    在activity中设置透明状态栏 的思路: 1.让activity的布局全屏 此时布局会和状态栏重叠 2.让布局最上方预留出和状态栏高度一样的高度,将状态栏的背景色设置为透明 效果如下: 一般是在s ...

  5. android 沉浸式状态栏 19,Android 沉浸式状态栏 以及 伪沉浸式状态栏

    小菜最近在调整页面状态栏的效果,主要包括沉浸式状态栏和伪沉浸状态栏(同事唠嗑给定义的玩的). 前段时间整理过一篇 Android 沉浸式状态栏的多种样式,现在小菜在稍微的补充一下,都是在日常应用中测试 ...

  6. Android沉浸式状态栏(透明状态栏)最佳实现

    Android沉浸式状态栏(透明状态栏)最佳实现 在Android4.4之前,我们的应用没法改变手机的状态栏颜色,当我们打开应用时,会出现上图中左侧的画面,在屏幕的顶部有一条黑色的状态栏,和应用的风格 ...

  7. 使用主题Theme实现Android沉浸式状态栏

    使用主题Theme实现Android沉浸式状态栏 很早的时候,通过主题设置activity沉浸式,发现坑很多,就开始使用各种StatusBarUtils,放弃了主题修改沉浸式这种方式,不知道大家有没有 ...

  8. Android 沉浸式状态栏 渐变颜色的实现

    Android 沉浸式状态栏 渐变颜色的实现 最近在开发中遇到一种个性化的需求,类似于QQ顶部的渐变状态栏的实现,如下图 首先我们要了解在Android5.0以后,系统API提供直接设置StatusB ...

  9. Android 沉浸式状态栏(简单好用)

    在网上试了很多方法去写沉浸式状态栏,但是效果都不是很好,偶然间翻到一篇文章跟着学了一下解决的很完美,现与大家分享一下.第一次写可能存在很多不足,望大家谅解. demon地址:https://downl ...

最新文章

  1. C语言 学生宿舍管理系统
  2. linux下通过yum安装svn及实现SVN与WEB同步解决方案[阿里云]
  3. TOMCAT虚拟路径配置
  4. python相关函数_python列表相关函数
  5. 20151008_Android Application类
  6. 冰汽朋克侦查机器人_冰汽时代生病机制是什么 寒霜朋克所有机制漏洞一览
  7. 软件测试面试笔试题准备(sql增删改查语句超全整理!看这篇就够了)
  8. Java语音转文字功能
  9. 涉密专用计算机平台,涉密计算机及移动存储介质保密管理系统(三合一)
  10. sumifs多条件求和步骤?如何运用sumifs函数进行求和
  11. 朝阳群众举报阿里996造成交通严重堵塞!
  12. 如何锻炼提高自己的逻辑思维?这里给你7个方法!
  13. Bootstrap3 和 Bootstrap4 的区别
  14. 关于树莓派无法解析域名的错误
  15. CCTV主持人博客列表
  16. freemarker导出Word文档并在其中插入图片
  17. 教你购物只花一半钱,先领优惠券再购物,原来你一直都买贵了?
  18. 减肥就来红光光浴吧,健康又安全
  19. MySQL获取季初日期_用于取得当前日期相对应的月初,月末,季初,季末,年初,年末时间...
  20. 2020股票总结,1号仓库-3.37%,2号仓库-6.06%

热门文章

  1. 树莓派上手前的准备工作(三)——将树莓派开机
  2. oc引导开机直接进_利用OC Gen X工具适配自己的OC引导
  3. 拯救会议纪要!快用这三个音频转文字方法,让领导对你刮目相看
  4. Python使用QQ邮箱发送多收件人email
  5. Google 关键字 2012年度排行
  6. 林语堂:为什么现代教育培养不出有见识的人?
  7. iPhone UIWebView Estimated Progress
  8. 【环境搭建】机械革命 1650 Ti + Win10 + CUDA 8.0安装教程 CUDA 8.0网盘链接
  9. 40个Python可视化图表案例!
  10. 中期国际:细数MT4和MT5软件的区别点在哪里?