成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Android沉浸式狀態欄及懸浮效果

移動開發 Android
現在大多數的電商APP的詳情頁長得幾乎都差不多,幾乎都是上面一個商品的圖片,當你滑動的時候,會有Tab懸浮在上面,這樣做用戶體驗確實不錯,如果Tab滑上去,用戶可能還需要滑下來,在來點擊Tab,這樣確實很麻煩。沉浸式狀態欄那,郭霖說過谷歌并沒有給出沉浸式狀態欄這個明白,谷歌只說了沉浸式模式(Immersive Mode)。

 [[177375]]

一、概述

現在大多數的電商APP的詳情頁長得幾乎都差不多,幾乎都是上面一個商品的圖片,當你滑動的時候,會有Tab懸浮在上面,這樣做用戶體驗確實不錯,如果Tab滑上去,用戶可能還需要滑下來,在來點擊Tab,這樣確實很麻煩。沉浸式狀態欄那,郭霖說過谷歌并沒有給出沉浸式狀態欄這個明白,谷歌只說了沉浸式模式(Immersive Mode)。不過沉浸式狀態欄這個名字其實聽不粗,隨大眾吧,但是Android的環境并沒有IOS環境一樣特別統一,比如華為rom的跟小米rom的虛擬按鍵完全不一樣,所有Android開發者不容易。。。。。

二、淘寶的效果 

 

 

 

三、我們的效果 

 

 

 

只能傳2M,把我的美女都給壓失真了。。。。。。

四、實現類

自定義ScrollView (StickyScrollView)

StatusBarUtil //非常不錯的狀態欄工具

五、布局 

  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.  
  7. <FrameLayout 
  8.     android:layout_width="match_parent" 
  9.     android:layout_height="match_parent"
  10.  
  11.     <com.xiaoyuan.StickyScrollView 
  12.         android:id="@+id/scrollView" 
  13.         android:layout_width="match_parent" 
  14.         android:layout_height="match_parent" 
  15.         android:focusable="true" 
  16.         android:focusableInTouchMode="true"
  17.  
  18.         <LinearLayout 
  19.             android:id="@+id/ll_content" 
  20.             android:layout_width="match_parent" 
  21.             android:layout_height="match_parent" 
  22.             android:orientation="vertical"
  23.  
  24.             <ImageView 
  25.                 android:layout_width="match_parent" 
  26.                 android:layout_height="500dip" 
  27.                 android:background="@mipmap/meinv"/> 
  28.  
  29.             <TextView 
  30.                 android:id="@+id/title" 
  31.                 android:layout_width="match_parent" 
  32.                 android:layout_height="50dp" 
  33.                 android:gravity="center" 
  34.                 android:text="美" /> 
  35.  
  36.             <TextView 
  37.                 android:layout_width="match_parent" 
  38.                 android:layout_height="50dip" 
  39.                 android:gravity="center" 
  40.                 android:text="女"/> 
  41.  
  42.             <TextView 
  43.                 android:layout_width="match_parent" 
  44.                 android:layout_height="50dip" 
  45.                 android:gravity="center" 
  46.                 android:text="美"/> 
  47.             <TextView 
  48.                 android:layout_width="match_parent" 
  49.                 android:layout_height="50dip" 
  50.                 android:gravity="center" 
  51.                 android:text="不"/> 
  52.             <TextView 
  53.                 android:layout_width="match_parent" 
  54.                 android:layout_height="50dip" 
  55.                 android:gravity="center" 
  56.                 android:text="美"/> 
  57.  
  58.             <LinearLayout 
  59.                 android:layout_width="match_parent" 
  60.                 android:layout_height="wrap_content" 
  61.                 android:orientation="vertical" 
  62.                 android:tag="sticky"
  63.  
  64.                 <LinearLayout 
  65.                     android:layout_width="match_parent" 
  66.                     android:layout_height="45dp" 
  67.                     android:background="#ffffff" 
  68.                     android:orientation="horizontal"
  69.  
  70.                     <TextView 
  71.                         android:id="@+id/infoText" 
  72.                         android:layout_width="0dp" 
  73.                         android:layout_height="match_parent" 
  74.                         android:layout_weight="1" 
  75.                         android:gravity="center" 
  76.                         android:text="美女信息" 
  77.                         android:textColor="#000000" 
  78.                         android:textSize="16dp" /> 
  79.  
  80.                     <TextView 
  81.                         android:id="@+id/secondText" 
  82.                         android:layout_width="0dp" 
  83.                         android:layout_height="match_parent" 
  84.                         android:layout_weight="1" 
  85.                         android:gravity="center" 
  86.                         android:text="美女介紹" 
  87.                         android:textColor="#000000" 
  88.                         android:textSize="16dp" /> 
  89.                 </LinearLayout> 
  90.             </LinearLayout> 
  91.  
  92.             <FrameLayout 
  93.                 android:id="@+id/tabMainContainer" 
  94.                 android:layout_width="match_parent" 
  95.                 android:layout_height="wrap_content" 
  96.                 android:background="#ffffff" 
  97.                 android:minHeight="400dp"
  98.  
  99.             </FrameLayout> 
  100.         </LinearLayout> 
  101.     </com.xiaoyuan.StickyScrollView> 
  102.  
  103.     <RelativeLayout 
  104.         android:id="@+id/ll_good_detail" 
  105.         android:layout_width="match_parent" 
  106.         android:layout_height="49dp" 
  107.         android:background="#00000000" 
  108.         android:paddingTop="@dimen/spacing_normal"
  109.  
  110.         <TextView 
  111.             android:layout_width="wrap_content" 
  112.             android:layout_height="wrap_content" 
  113.             android:textColor="#ffffff" 
  114.             android:layout_alignParentLeft="true" 
  115.             android:layout_marginLeft="10dip" 
  116.             android:layout_centerHorizontal="true" 
  117.             android:text="返回"/> 
  118.  
  119.         <TextView 
  120.             android:layout_width="wrap_content" 
  121.             android:layout_height="wrap_content" 
  122.             android:textColor="#ffffff" 
  123.             android:layout_centerInParent="true" 
  124.             android:layout_centerHorizontal="true" 
  125.             android:layout_marginLeft="10dip" 
  126.             android:text="美女"/> 
  127.  
  128.         <TextView 
  129.             android:layout_width="wrap_content" 
  130.             android:layout_height="wrap_content" 
  131.             android:textColor="#ffffff" 
  132.             android:layout_alignParentRight="true" 
  133.             android:layout_marginRight="10dip" 
  134.             android:layout_centerHorizontal="true" 
  135.             android:text="分享"/> 
  136.  
  137.     </RelativeLayout> 
  138.  
  139. </FrameLayout> 
  140.  
  141. </RelativeLayout>  

注意:我們把要懸浮的Tab設置了android:tag=”sticky”這樣的屬性

六、實現代碼

  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener, StickyScrollView.OnScrollChangedListener { 
  2.  
  3. TextView oneTextView, twoTextView; 
  4. private StickyScrollView stickyScrollView; 
  5. private int height; 
  6. private LinearLayout llContent; 
  7. private RelativeLayout llTitle; 
  8. private FrameLayout frameLayout; 
  9. private TextView title; 
  10.  
  11. @Override 
  12. protected void onCreate(Bundle savedInstanceState) { 
  13.     super.onCreate(savedInstanceState); 
  14.     setContentView(R.layout.activity_main); 
  15.     initView(); 
  16.     initListeners(); 
  17.  
  18.  
  19. /** 
  20.  * 初始化View 
  21.  */ 
  22. private void initView() { 
  23.     stickyScrollView = (StickyScrollView) findViewById(R.id.scrollView); 
  24.     frameLayout = (FrameLayout) findViewById(R.id.tabMainContainer); 
  25.     title = (TextView) findViewById(R.id.title); 
  26.     oneTextView = (TextView) findViewById(R.id.infoText); 
  27.     llContent = (LinearLayout) findViewById(R.id.ll_content); 
  28.     llTitle = (RelativeLayout) findViewById(R.id.ll_good_detail); 
  29.     oneTextView.setOnClickListener(this); 
  30.     twoTextView = (TextView) findViewById(R.id.secondText); 
  31.     twoTextView.setOnClickListener(this); 
  32.  
  33.     stickyScrollView.setOnScrollListener(this); 
  34.     StatusBarUtil.setTranslucentForImageView(MainActivity.this, 0, title); 
  35.     FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) llTitle.getLayoutParams(); 
  36.     params.setMargins(0, getStatusHeight(), 0, 0); 
  37.     llTitle.setLayoutParams(params); 
  38.  
  39.     //默認設置一個Frg 
  40.     getSupportFragmentManager().beginTransaction().replace(R.id.tabMainContainer, Fragment.newInstance()).commit(); 
  41.  
  42. /** 
  43.  * 獲取狀態欄高度 
  44.  * @return 
  45.  */ 
  46. private int getStatusHeight() { 
  47.     int resourceId = MainActivity.this.getResources().getIdentifier("status_bar_height""dimen""android"); 
  48.     return getResources().getDimensionPixelSize(resourceId); 
  49.  
  50.  
  51. @Override 
  52. public void onClick(View v) { 
  53.     if (v.getId() == R.id.infoText) { 
  54.         getSupportFragmentManager().beginTransaction().replace(R.id.tabMainContainer, Fragment.newInstance()).commit(); 
  55.     } else if (v.getId() == R.id.secondText) { 
  56.         getSupportFragmentManager().beginTransaction().replace(R.id.tabMainContainer, Fragment1.newInstance()).commit(); 
  57.  
  58.     } 
  59.  
  60. private void initListeners() { 
  61.     //獲取內容總高度 
  62.     final ViewTreeObserver vto = llContent.getViewTreeObserver(); 
  63.     vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
  64.         @Override 
  65.         public void onGlobalLayout() { 
  66.             height = llContent.getHeight(); 
  67.             //注意要移除 
  68.             llContent.getViewTreeObserver() 
  69.                     .removeGlobalOnLayoutListener(this); 
  70.  
  71.         } 
  72.     }); 
  73.  
  74.     //獲取Fragment高度 
  75.     ViewTreeObserver viewTreeObserver = frameLayout.getViewTreeObserver(); 
  76.     viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
  77.         @Override 
  78.         public void onGlobalLayout() { 
  79.             height = height - frameLayout.getHeight(); 
  80.             //注意要移除 
  81.             frameLayout.getViewTreeObserver() 
  82.                     .removeGlobalOnLayoutListener(this); 
  83.         } 
  84.     }); 
  85.  
  86.     //獲取title高度 
  87.     ViewTreeObserver viewTreeObserver1 = llTitle.getViewTreeObserver(); 
  88.     viewTreeObserver1.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
  89.         @Override 
  90.         public void onGlobalLayout() { 
  91.             height = height - llTitle.getHeight() - getStatusHeight();//計算滑動的總距離 
  92.             stickyScrollView.setStickTop(llTitle.getHeight() + getStatusHeight());//設置距離多少懸浮 
  93.             //注意要移除 
  94.             llTitle.getViewTreeObserver() 
  95.                     .removeGlobalOnLayoutListener(this); 
  96.         } 
  97.     }); 
  98.  
  99.  
  100. @Override 
  101. public void onScrollChanged(int l, int t, int oldl, int oldt) { 
  102.     if (t <= 0) { 
  103.         llTitle.setBackgroundColor(Color.argb((int) 0, 255, 255, 255)); 
  104.         StatusBarUtil.setTranslucentForImageView(MainActivity.this, 0, title); 
  105.     } else if (t > 0 && t <= height) { 
  106.         float scale = (float) t / height; 
  107.         int alpha = (int) (255 * scale); 
  108.         llTitle.setBackgroundColor(Color.argb((int) alpha, 227, 29, 26));//設置標題欄的透明度及顏色 
  109.         StatusBarUtil.setTranslucentForImageView(MainActivity.this, alpha, title);//設置狀態欄的透明度 
  110.     } else { 
  111.         llTitle.setBackgroundColor(Color.argb((int) 255, 227, 29, 26)); 
  112.         StatusBarUtil.setTranslucentForImageView(MainActivity.this, 255, title); 
  113.     } 
  114.  }  

注意:stickyScrollView.setStickTop(int height)我們通過這個方法可以設置Tab距離多高開始懸浮

我們通過監聽ScrollView滑動距離來不斷改變我們標題欄跟狀態欄的透明度來達到效果,在這里我們計算了幾個高度(滑動距離)。最后來算出滑動總距離,根據滑動的距離跟滑動的總距離來算出透明度的數值。

StatusBarUtil.setTranslucentForImageView(MainActivity.this, 0, title);我們通過工具來實現圖片深入狀態欄。里面的傳的View是圖片下面的View。

六、總結

效果倒是不錯,美女也不錯、但是在Android4.4之前根本就沒有沉浸式這個東西,大家可以下載源碼來研究。自己動手實現一遍記得比較清楚。工作了。太忙了。最后感謝一下dota群的高叔(博客地址不知道)提供思路。

責任編輯:龐桂玉 來源: Android開發中文站
相關推薦

2017-02-17 11:00:57

狀態欄Android

2017-12-05 12:44:57

Android沉浸式狀態欄APP

2021-08-09 20:29:27

Android沉浸式狀態欄

2022-11-23 14:47:29

北向開發鴻蒙

2015-02-12 14:49:36

CGToast狀態欄提示Status

2013-07-18 16:09:10

自定義iOS狀態欄iOS開發iOS學習

2022-02-22 09:16:41

AndroidWindows狀態欄

2014-06-06 14:03:13

iOS狀態欄提示控件原理

2012-12-24 14:42:48

iOS自定義狀態欄

2021-06-15 15:04:38

Android 12安卓通話

2013-11-20 15:08:32

iOS開發狀態欄

2021-06-15 15:28:31

谷歌Android開發

2016-11-18 08:41:23

2022-03-16 15:30:25

barAndroid開發者

2018-05-16 09:11:19

Android狀態欄移動系統

2011-06-02 09:23:58

Android 效果

2023-05-30 14:50:20

界面開發鴻蒙

2011-06-16 11:15:04

2021-10-20 07:39:12

谷歌安卓 Android 12

2020-11-23 14:29:22

Android 10窗口代碼
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 免费一区在线观看 | 日韩一区二区av | 亚洲欧美在线一区 | 99久久精品一区二区成人 | 欧美a区 | 99这里只有精品视频 | 成人久久久 | 国产成人免费视频网站视频社区 | 国产精品美女一区二区 | 天天操网| 久久精品免费 | 欧美一级片在线观看 | 国产精品久久久久久久久久东京 | 中文字幕免费视频 | 久久久av一区 | 在线高清免费观看视频 | 欧美一级免费片 | 国产精品国产a级 | 成人在线观看免费视频 | 国产一区二区三区 | 91精品在线看| 日本a v在线播放 | 国产成人精品综合 | 九九国产在线观看 | 日本在线免费视频 | 精品videossex高潮汇编 | 翔田千里一区二区 | 亚洲经典一区 | 精品免费国产一区二区三区 | www.v888av.com | 噜啊噜在线 | 久久久2o19精品 | 欧美日产国产成人免费图片 | 精产嫩模国品一二三区 | 久久久999精品 | 日韩字幕 | 亚州精品天堂中文字幕 | av av在线| 日韩欧美高清 | 国产偷录叫床高潮录音 | 午夜精品久久久久久不卡欧美一级 |