Android immersive status bar and suspension effect

Android immersive status bar and suspension effect

  [[177375]]

1. Overview

Now, the detail pages of most e-commerce apps look almost the same. They are almost all pictures of a product. When you slide, a tab will float on top. This is a good user experience. If the tab slides up, the user may need to slide down and click the tab again, which is really troublesome. As for the immersive status bar, Guo Lin said that Google did not give a clear explanation of the immersive status bar. Google only said immersive mode. However, the name of the immersive status bar is not bad. Just follow the crowd. However, the Android environment is not as unified as the IOS environment. For example, the virtual buttons of Huawei ROM and Xiaomi ROM are completely different. It is not easy for all Android developers. ...

2. The effect of Taobao

3. Our results

It can only transfer 2M, and my beauties are distorted. . . . .

4. Implementation Class

Custom ScrollView (StickyScrollView)

StatusBarUtil // Very good status bar tool

5. Layout

  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= "beautiful" />
  35.  
  36. <TextView
  37. android:layout_width= "match_parent"  
  38. android:layout_height= "50dip"  
  39. android:gravity= "center"  
  40. android:text= "female" />
  41.  
  42. <TextView
  43. android:layout_width= "match_parent"  
  44. android:layout_height= "50dip"  
  45. android:gravity= "center"  
  46. android:text= "beautiful" />
  47. <TextView
  48. android:layout_width= "match_parent"  
  49. android:layout_height= "50dip"  
  50. android:gravity= "center"  
  51. android:text= "No" />
  52. <TextView
  53. android:layout_width= "match_parent"  
  54. android:layout_height= "50dip"  
  55. android:gravity= "center"  
  56. android:text= "beautiful" />
  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= "Beauty information"  
  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= "Beauty introduction"  
  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= "Return" />
  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= "Beauty" />
  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= "Share" />
  136.  
  137. </RelativeLayout>
  138.  
  139. </FrameLayout>
  140.  
  141. </RelativeLayout>

Note: We set the attribute android:tag="sticky" for the Tab to be suspended

6. Implementation Code

  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. /**
  21. * Initialize View  
  22. */
  23. private void initView() {
  24. stickyScrollView = (StickyScrollView) findViewById(R.id.scrollView);
  25. frameLayout = (FrameLayout) findViewById(R.id.tabMainContainer);
  26. title = (TextView) findViewById(R.id.title);
  27. oneTextView = (TextView) findViewById(R.id.infoText);
  28. llContent = (LinearLayout) findViewById(R.id.ll_content);
  29. llTitle = (RelativeLayout) findViewById(R.id.ll_good_detail);
  30. oneTextView.setOnClickListener(this);
  31. twoTextView = (TextView) findViewById(R.id.secondText);
  32. twoTextView.setOnClickListener(this);
  33.  
  34. stickyScrollView.setOnScrollListener(this);
  35. StatusBarUtil.setTranslucentForImageView(MainActivity.this, 0, title);
  36. FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) llTitle.getLayoutParams();
  37. params.setMargins(0, getStatusHeight(), 0, 0);
  38. llTitle.setLayoutParams(params);
  39.  
  40. //Set a Frg by default
  41. getSupportFragmentManager().beginTransaction(). replace (R.id.tabMainContainer, Fragment.newInstance()). commit ();
  42. }
  43.  
  44. /**
  45. * Get the status bar height
  46. * @return  
  47. */
  48. private int getStatusHeight() {
  49. int resourceId = MainActivity.this.getResources().getIdentifier( "status_bar_height" , "dimen" , "android" );
  50. return getResources().getDimensionPixelSize(resourceId);
  51.  
  52. }
  53.  
  54. @Override
  55. public void onClick( View v) {
  56. if (v.getId() == R.id.infoText) {
  57. getSupportFragmentManager().beginTransaction(). replace (R.id.tabMainContainer, Fragment.newInstance()). commit ();
  58. } else if (v.getId() == R.id.secondText) {
  59. getSupportFragmentManager().beginTransaction(). replace (R.id.tabMainContainer, Fragment1.newInstance()). commit ();
  60.  
  61. }
  62. }
  63.  
  64. private void initListeners() {
  65. //Get the total height of the content
  66. final ViewTreeObserver vto = llContent.getViewTreeObserver();
  67. vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  68. @Override
  69. public void onGlobalLayout() {
  70. height = llContent.getHeight();
  71. //Note to remove
  72. llContent.getViewTreeObserver()
  73. .removeGlobalOnLayoutListener(this);
  74.  
  75. }
  76. });
  77.  
  78. //Get the Fragment height
  79. ViewTreeObserver viewTreeObserver = frameLayout.getViewTreeObserver();
  80. viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  81. @Override
  82. public void onGlobalLayout() {
  83. height = height - frameLayout.getHeight();
  84. //Note to remove
  85. frameLayout.getViewTreeObserver()
  86. .removeGlobalOnLayoutListener(this);
  87. }
  88. });
  89.  
  90. //Get the title height
  91. ViewTreeObserver viewTreeObserver1 = llTitle.getViewTreeObserver();
  92. viewTreeObserver1.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  93. @Override
  94. public void onGlobalLayout() {
  95. height = height - llTitle.getHeight() - getStatusHeight(); //Calculate the total sliding distance
  96. stickyScrollView.setStickTop(llTitle.getHeight() + getStatusHeight()); //Set the suspension distance
  97. //Note to remove
  98. llTitle.getViewTreeObserver()
  99. .removeGlobalOnLayoutListener(this);
  100. }
  101. });
  102.  
  103. }
  104.  
  105. @Override
  106. public void onScrollChanged( int l, int t, int oldl, int oldt) {
  107. if (t <= 0) {
  108. llTitle.setBackgroundColor(Color.argb(( int ) 0, 255, 255, 255));
  109. StatusBarUtil.setTranslucentForImageView(MainActivity.this, 0, title);
  110. } else if (t > 0 && t <= height) {
  111. float scale = ( float ) t / height;
  112. int alpha = ( int ) (255 * scale);
  113. llTitle.setBackgroundColor(Color.argb(( int ) alpha, 227, 29, 26)); //Set the transparency and color of the title bar
  114. StatusBarUtil.setTranslucentForImageView(MainActivity.this, alpha, title); //Set the transparency of the status bar
  115. } else {
  116. llTitle.setBackgroundColor(Color.argb(( int ) 255, 227, 29, 26));
  117. StatusBarUtil.setTranslucentForImageView(MainActivity.this, 255, title);
  118. }
  119. }
  120. }

Note: stickyScrollView.setStickTop(int height) We can use this method to set the height of the Tab to start floating

We achieve this effect by monitoring the sliding distance of ScrollView to continuously change the transparency of our title bar and status bar. Here we calculate several heights (sliding distances) and finally calculate the total sliding distance, and calculate the transparency value based on the sliding distance and the total sliding distance.

StatusBarUtil.setTranslucentForImageView(MainActivity.this, 0, title); We use the tool to make the image go deep into the status bar. The View that is transmitted is the View under the image.

VI. Conclusion

The effect is good, and the beauties are also good, but there was no such thing as immersion before Android 4.4. You can download the source code to study it. I will remember it more clearly if I implement it myself. I am working. I am too busy. Finally, I would like to thank Uncle Gao (I don’t know the blog address) in the dota group for providing ideas.

<<:  HandyJSON: Swift language JSON to Model tool library

>>:  20 common usage examples of time and date libraries in Java 8

Recommend

September marketing hotspot calendar, recommended to save!

August is about to pass We are about to usher in ...

Elements and channels of product promotion plan!

The general promotion process is as follows: 1. M...

iPad Pro keyboard disassembly: Fully conductive, repairable

The foreign website ifixit disassembled the iPad ...

What amazing skills do sea snails, known as “global travelers,” have?

Author: Liu Yi Source: Knowledge is Power Magazin...

APP promotion: the details of Xiaomi promotion that you don’t know!

After Xiaomi became famous, the term "Weibo ...

For promotion on Xiaohongshu, just read this article.

This article will try to answer your questions: 1...

Zhou Lvwen-Learn mathematical modeling and MATLAB programming in 7 days

This course requires a certain foundation, otherw...