Android开发之实现QQ空间、美团首页沉浸式状态栏

前言:QQ空间和美团首页的状态栏是图片充满了状态栏,往上滑动,状态栏会慢慢变成ToolBar的颜色或者是我们自定义组件的背景颜色,今天我们就来实现这一效果!

-------------------------分割线-----------------------------

关于ToolBar的讲解请移步《Android开发之ToolBar的使用》,

如果你还对沉浸式这一概念比较模糊的话请移步我的博客:

Android开发之Android5.x的状态栏变色相关东西》、

Android开发之Android4.4的状态栏变色相关的东西》!

-------------------------分割线-----------------------------

效果图展示:


-------------------------分割线-----------------------------

整个UI的组成:

1.最上部是ToolBar(当然你也可以自定义TextView)

2.图片简单不解释,但是你也可以用相对布局包裹住图片然后在图片上加上自己想要的东西。

3.滑动的Hello页面,我这里比较简单用的都是TextView,当然你也可以用RecyclerView,这时候就要和最外层的ScrollView做滑动判断,本篇暂时不涉及ScrollView和RecyclerView兼容性判断!

4.最外层是加了带滑动监听的ScrollView,方便做状态栏的渐变!

-------------------------分割线-----------------------------

带滑动监听的ScrollView实现:

[java]  view plain  copy
  1. import android.content.Context;  
  2. import android.util.AttributeSet;  
  3. import android.widget.ScrollView;  
  4.   
  5. /** 
  6.  * 带滑动监听的scrollview 
  7.  */  
  8. public class GradationScrollView extends ScrollView {  
  9.   
  10.     private ScrollViewListener scrollViewListener = null;  
  11.       
  12.     public GradationScrollView(Context context, AttributeSet attrs) {  
  13.         super(context, attrs);  
  14.     }  
  15.   
  16.     @Override  
  17.     protected void onScrollChanged(int x, int y, int oldx, int oldy) {  
  18.         super.onScrollChanged(x, y, oldx, oldy);  
  19.         if (scrollViewListener != null) {  
  20.             scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);  
  21.         }  
  22.     }  
  23.   
  24.     public void setScrollViewListener(ScrollViewListener scrollViewListener) {  
  25.         this.scrollViewListener = scrollViewListener;  
  26.     }  
  27.   
  28.     public interface ScrollViewListener {  
  29.         void onScrollChanged(GradationScrollView scrollView, int x, int y, int oldx, int oldy);  
  30.     }  
  31. }  


渐变实现的方法:

滑动的时候,动态的获取图片的高度,然后根据高度,设置滑动的百分比,然后设置给ToolBar!

完整代码奉上(为了方便,本篇文章只做了5.0以上的效果,大家可以根据我的博客《Android开发之Android4.4的状态栏变色相关的东西》自行适配安卓4.4):

[java]  view plain  copy
  1. import android.app.Activity;  
  2. import android.graphics.Color;  
  3. import android.os.Build;  
  4. import android.support.annotation.RequiresApi;  
  5. import android.support.v7.app.AppCompatActivity;  
  6. import android.os.Bundle;  
  7. import android.support.v7.widget.Toolbar;  
  8. import android.view.View;  
  9. import android.view.ViewTreeObserver;  
  10. import android.view.Window;  
  11. import android.view.WindowManager;  
  12. import android.widget.ImageView;  
  13. import android.widget.TextView;  
  14.   
  15. public class MainActivity extends AppCompatActivity implements GradationScrollView.ScrollViewListener {  
  16.   
  17.     private Toolbar toolbar;  
  18.     private int height;  
  19.     private ImageView imageView;  
  20.     private GradationScrollView scrollView;  
  21.     private TextView toolbar_title;  
  22.   
  23.     @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)  
  24.     @Override  
  25.     protected void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         getWindow().requestFeature(Window.FEATURE_NO_TITLE);  
  28.         setImgTransparent(this);  
  29.         setContentView(R.layout.activity_main);  
  30.   
  31.         toolbar = (Toolbar) findViewById(R.id.toolbar);  
  32.         toolbar.setTitle("");  
  33.         setSupportActionBar(toolbar);  
  34.   
  35.         imageView = (ImageView) findViewById(R.id.iv_banner);  
  36.         toolbar_title = (TextView) findViewById(R.id.toolbar_title);  
  37.         scrollView = (GradationScrollView) findViewById(R.id.scrollview);  
  38.   
  39.         imageView.setFocusable(true);  
  40.         imageView.setFocusableInTouchMode(true);  
  41.         imageView.requestFocus();  
  42.   
  43.         ViewTreeObserver vto = imageView.getViewTreeObserver();  
  44.         vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {  
  45.             @Override  
  46.             public void onGlobalLayout() {  
  47.                 toolbar.getViewTreeObserver().removeGlobalOnLayoutListener(this);  
  48.                 height = imageView.getHeight();  
  49.                 scrollView.setScrollViewListener(MainActivity.this);  
  50.             }  
  51.         });  
  52.     }  
  53.   
  54.     @Override  
  55.     public void onScrollChanged(GradationScrollView scrollView, int x, int y, int oldx, int oldy) {  
  56.         if (y <= 0) {   //设置标题的背景颜色  
  57.             toolbar.setBackgroundColor(Color.argb((int0144151166));  
  58.         } else if (y > 0 && y <= height) { //滑动距离小于图片的高度时,设置背景和字体颜色颜色透明度渐变  
  59.             float scale = (float) y / height;  
  60.             float alpha = (255 * scale);  
  61.             toolbar_title.setTextColor(Color.argb((int) alpha, 255255255));  
  62.             toolbar.setBackgroundColor(Color.argb((int) alpha, 144151166));  
  63.         } else {    //滑动到下面设置普通颜色  
  64.             toolbar.setBackgroundColor(Color.argb((int255144151166));  
  65.         }  
  66.     }  
  67.   
  68.     //版本判断  
  69.     public void setImgTransparent(Activity activity) {  
  70.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {  
  71.             Window window = activity.getWindow();  
  72.             window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);  
  73.             window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);  
  74.             window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);  
  75.             window.setStatusBarColor(Color.TRANSPARENT);  
  76.             window.setNavigationBarColor(Color.TRANSPARENT);  
  77.         }  
  78.     }  
  79. }  
下面贴出布局:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     tools:context=".MainActivity">  
  7.   
  8.     <com.fly.lsn29_qzone1.GradationScrollView  
  9.         android:id="@+id/scrollview"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="match_parent"  
  12.         android:scrollbars="none">  
  13.   
  14.         <LinearLayout  
  15.             android:layout_width="match_parent"  
  16.             android:layout_height="wrap_content"  
  17.             android:orientation="vertical">  
  18.   
  19.             <ImageView  
  20.                 android:id="@+id/iv_banner"  
  21.                 android:layout_width="match_parent"  
  22.                 android:layout_height="200dp"  
  23.                 android:scaleType="fitXY"  
  24.                 android:src="@drawable/img" />  
  25.   
  26.             <TextView  
  27.                 android:layout_width="match_parent"  
  28.                 android:layout_height="50dp"  
  29.                 android:gravity="center"  
  30.                 android:text="Hello"  
  31.                 android:textSize="20sp" />  
  32.   
  33.             //......省略N个TextView  
  34.   
  35.             <TextView  
  36.                 android:layout_width="match_parent"  
  37.                 android:layout_height="50dp"  
  38.                 android:gravity="center"  
  39.                 android:text="Hello"  
  40.                 android:textSize="20sp" />  
  41.   
  42.             <TextView  
  43.                 android:layout_width="match_parent"  
  44.                 android:layout_height="50dp"  
  45.                 android:gravity="center"  
  46.                 android:text="Hello"  
  47.                 android:textSize="20sp" />  
  48.         </LinearLayout>  
  49.     </com.fly.lsn29_qzone1.GradationScrollView>  
  50.   
  51.     <android.support.v7.widget.Toolbar  
  52.         android:id="@+id/toolbar"  
  53.         android:layout_width="match_parent"  
  54.         android:layout_height="wrap_content"  
  55.         android:minHeight="?attr/actionBarSize">  
  56.   
  57.         <TextView  
  58.             android:id="@+id/toolbar_title"  
  59.             android:layout_width="wrap_content"  
  60.             android:layout_height="wrap_content"  
  61.             android:layout_gravity="center"  
  62.             android:background="#00000000"  
  63.             android:singleLine="true"  
  64.             android:text="fly学院"  
  65.             android:textColor="#00000000"  
  66.             android:textSize="20sp" />  
  67.     </android.support.v7.widget.Toolbar>  
  68. </RelativeLayout>  

猜你喜欢

转载自blog.csdn.net/fengyenom1/article/details/80134952