日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
Android沉浸式狀態(tài)欄及懸浮效果

一、概述

網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)公司!專(zhuān)注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、微信小程序開(kāi)發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶(hù)創(chuàng)新互聯(lián)還提供了麗江免費(fèi)建站歡迎大家使用!

現(xiàn)在大多數(shù)的電商APP的詳情頁(yè)長(zhǎng)得幾乎都差不多,幾乎都是上面一個(gè)商品的圖片,當(dāng)你滑動(dòng)的時(shí)候,會(huì)有Tab懸浮在上面,這樣做用戶(hù)體驗(yàn)確實(shí)不錯(cuò),如果Tab滑上去,用戶(hù)可能還需要滑下來(lái),在來(lái)點(diǎn)擊Tab,這樣確實(shí)很麻煩。沉浸式狀態(tài)欄那,郭霖說(shuō)過(guò)谷歌并沒(méi)有給出沉浸式狀態(tài)欄這個(gè)明白,谷歌只說(shuō)了沉浸式模式(Immersive Mode)。不過(guò)沉浸式狀態(tài)欄這個(gè)名字其實(shí)聽(tīng)不粗,隨大眾吧,但是Android的環(huán)境并沒(méi)有IOS環(huán)境一樣特別統(tǒng)一,比如華為rom的跟小米rom的虛擬按鍵完全不一樣,所有Android開(kāi)發(fā)者不容易。。。。。

二、淘寶的效果 

三、我們的效果 

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

四、實(shí)現(xiàn)類(lèi)

自定義ScrollView (StickyScrollView)

StatusBarUtil //非常不錯(cuò)的狀態(tài)欄工具

五、布局 

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

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

六、實(shí)現(xiàn)代碼

 
 
 
 
  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.     //默認(rèn)設(shè)置一個(gè)Frg 
  40.     getSupportFragmentManager().beginTransaction().replace(R.id.tabMainContainer, Fragment.newInstance()).commit(); 
  41.  
  42. /** 
  43.  * 獲取狀態(tài)欄高度 
  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.     //獲取內(nèi)容總高度 
  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();//計(jì)算滑動(dòng)的總距離 
  92.             stickyScrollView.setStickTop(llTitle.getHeight() + getStatusHeight());//設(shè)置距離多少懸浮 
  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));//設(shè)置標(biāo)題欄的透明度及顏色 
  109.         StatusBarUtil.setTranslucentForImageView(MainActivity.this, alpha, title);//設(shè)置狀態(tài)欄的透明度 
  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)我們通過(guò)這個(gè)方法可以設(shè)置Tab距離多高開(kāi)始懸浮

我們通過(guò)監(jiān)聽(tīng)ScrollView滑動(dòng)距離來(lái)不斷改變我們標(biāo)題欄跟狀態(tài)欄的透明度來(lái)達(dá)到效果,在這里我們計(jì)算了幾個(gè)高度(滑動(dòng)距離)。最后來(lái)算出滑動(dòng)總距離,根據(jù)滑動(dòng)的距離跟滑動(dòng)的總距離來(lái)算出透明度的數(shù)值。

StatusBarUtil.setTranslucentForImageView(MainActivity.this, 0, title);我們通過(guò)工具來(lái)實(shí)現(xiàn)圖片深入狀態(tài)欄。里面的傳的View是圖片下面的View。

六、總結(jié)

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


文章名稱(chēng):Android沉浸式狀態(tài)欄及懸浮效果
URL地址:http://www.dlmjj.cn/article/ccoihgc.html