Android takes you to analyze ScrollView-imitation QQ space title bar gradient

Android takes you to analyze ScrollView-imitation QQ space title bar gradient

[[190235]]

introduction

Today we are going to study ScrollView, which is divided into horizontal scroll view (HorizontalScrollView) and vertical scroll view (ScrollView). Today we will mainly study the vertical scroll view. I believe that everyone often uses it in development. The function of ScrollView is already very powerful, but it still cannot satisfy our UI designers with wild imagination, so we need to customize it... This article mainly talks about monitoring the sliding of ScrollView to achieve the gradient of the QQ space title bar. Let's take a look at the effect picture first:

Okay, let’s get to the point.

ScrollView properties you may not know

  • android:scrollbars

Set the scroll bar display. None (hide), horizontal (horizontal), vertical (vertical)

  • android:scrollbarStyle

Set the scroll bar style and position. Setting value: insideOverlay, insideInset, outsideOverlay, outsideInset

  • android:scrollbarThumbHorizontal

Sets the drawable for the horizontal scroll bar.

  • android:soundEffectsEnabled

Set whether there is a sound effect when clicking or touching

  • android:fadingEdge

Set the direction of the border gradient when the scroll bar is pulled. None (border color remains unchanged), horizontal (color fades horizontally), vertical (color fades vertically). Refer to the fadingEdgeLength effect diagram android:fadingEdgeLength to set the length of the border gradient

  • android:scrollX

Set the horizontal scroll offset in pixels. This effect can be seen in the GridView.

  • android:scrollY

Set the vertical scroll offset in pixels.

  • android:scrollbarAlwaysDrawHorizontalTrack

Set whether to always display the vertical scroll bar

  • android:scrollbarDefaultDelayBeforeFade

Sets the time in milliseconds after which the fade will start.

If you are interested in the above properties, you can study them. I won’t go into detail here. Many properties are not commonly used. Let’s talk about the ones we often use. How to monitor the sliding of ScrollView and realize the gradient of the title bar?

ScrollView sliding monitoring:

Google does not provide us with a method to determine the sliding distance of ScrollView or whether to slide to the bottom or top of the layout, but it does provide an onScrollChanged method:

  1. @Override
  2. protected void onScrollChanged( int x, int y, int oldx, int oldy) {
  3. super.onScrollChanged(x, y, oldx, oldy);
  4. //todo:
  5. }
  6. }

By viewing the source code comments,

  1. /**
  2. * This is called in response to an internal scroll   in this view (ie, the
  3. * view scrolled its own contents). This is typically as a result of  
  4. * {@link #scrollBy( int , int )} or {@link #scrollTo( int , int )} having been
  5. * called.
  6. *
  7. * @param l Current horizontal scroll origin.
  8. * @param t Current vertical scroll origin.
  9. * @param oldl Previous horizontal scroll origin.
  10. * @param oldt Previous vertical scroll origin.
  11. */

We can know that the parameters of this method are:

l: Current horizontal sliding distance

t: current vertical sliding distance

oldl: previous horizontal sliding distance

oldt: previous vertical sliding distance

But we cannot call this method. We can rewrite the interface or rewrite ScrollView to expose this method:

  1. package com.hankkin.gradationscroll;
  2.  
  3. import android.content.Context;
  4. import android.util.AttributeSet;
  5. import android.widget.ScrollView;
  6. /**
  7. * Scrollview with scroll monitoring
  8. *
  9. */
  10. public class GradationScrollView extends ScrollView {
  11.  
  12. public interface ScrollViewListener {
  13.  
  14. void onScrollChanged(GradationScrollView scrollView, int x, int y,
  15. int oldx, int oldy);
  16.  
  17. }
  18.  
  19. private ScrollViewListener scrollViewListener = null ;
  20.  
  21. public GradationScrollView(Context context) {
  22. super(context);
  23. }
  24.  
  25. public GradationScrollView(Context context, AttributeSet attrs,
  26. int defStyle) {
  27. super(context, attrs, defStyle);
  28. }
  29.  
  30. public GradationScrollView(Context context, AttributeSet attrs) {
  31. super(context, attrs);
  32. }
  33.  
  34. public void setScrollViewListener(ScrollViewListener scrollViewListener) {
  35. this.scrollViewListener = scrollViewListener;
  36. }
  37.  
  38. @Override
  39. protected void onScrollChanged( int x, int y, int oldx, int oldy) {
  40. super.onScrollChanged(x, y, oldx, oldy);
  41. if (scrollViewListener != null ) {
  42. scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
  43. }
  44. }
  45.  
  46. }

Set title gradient

Now that the scroll listener is exposed, we should set the title bar to change the transparency of the title bar to achieve a gradual change as the ScrollView slides:

Let's take a look at the layout first:

  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= "com.hankkin.gradationtitlebar.QQSpeakActivity" >
  7.  
  8. <com.hankkin.gradationsscroll.GradationScrollView
  9. android:id= "@+id/scrollview"  
  10. android:layout_width= "match_parent"  
  11. android:layout_height= "match_parent"  
  12. android:scrollbars= "none" >
  13. <LinearLayout
  14. android:layout_width= "match_parent"  
  15. android:layout_height= "wrap_content"  
  16. android:orientation= "vertical" >
  17. <ImageView
  18. android:id= "@+id/iv_banner"  
  19. android:scaleType= "fitXY"  
  20. android:src= "@drawable/banner3"  
  21. android:layout_width= "match_parent"  
  22. android:layout_height= "200dp" />
  23. <com.hankkin.gradationscroll.NoScrollListview
  24. android:id= "@+id/listview"  
  25. android:layout_width= "match_parent"  
  26. android:layout_height= "wrap_content" >
  27. </com.hankkin.gradationscroll.NoScrollListview>
  28. </LinearLayout>
  29. </com.hankkin.gradationsscroll.GradationScrollView>
  30. <TextView
  31. android:paddingBottom= "10dp"  
  32. android:id= "@+id/textview"  
  33. android:layout_width= "match_parent"  
  34. android:layout_height= "55dp"  
  35. android:gravity= "center|bottom"  
  36. android:text= "I am the title"  
  37. android:textSize= "18sp"  
  38. android:textColor= "@color/transparent"  
  39. android:background= "#00000000" />
  40. </RelativeLayout>

The outermost layer is our custom ScrollView, which wraps a background image and a ListView (ListView is rewritten to not be able to slide), and then there is a TextView on the layout as the title bar. You can also use the layout.

Then we need to get the height of the image and set the scroll listener to set the color transparency of the title bar and the transparency of the font color according to the scroll distance.

  1. /**
  2. * After getting the top image height, set the scroll monitor
  3. */
  4. private void initListeners() {
  5.  
  6. ViewTreeObserver vto = ivBanner.getViewTreeObserver();
  7. vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  8. @Override
  9. public void onGlobalLayout() {
  10. textView.getViewTreeObserver().removeGlobalOnLayoutListener(
  11. this);
  12. height = ivBanner.getHeight();
  13.  
  14. scrollView.setScrollViewListener(QQSpeakActivity.this);
  15. }
  16. });
  17. } /**
  18. * Slide monitoring
  19. * @param scrollView
  20. * @param x
  21. * @param y
  22. * @param oldx
  23. * @param oldy
  24. */
  25. @Override
  26. public void onScrollChanged(GradationScrollView scrollView, int x, int y,
  27. int oldx, int oldy) {
  28. // TODO Auto-generated method stub
  29. if (y <= 0) { //Set the background color of the title
  30. textView.setBackgroundColor(Color.argb(( int ) 0, 144,151,166));
  31. } else if (y > 0 && y <= height) { //When the sliding distance is less than the height of the banner image, set the background and font color transparency gradient
  32. float scale = ( float ) y / height;
  33. float alpha = (255 * scale);
  34. textView.setTextColor(Color.argb(( int ) alpha, 255,255,255));
  35. textView.setBackgroundColor(Color.argb(( int ) alpha, 144,151,166));
  36. } else { //Slide to the bottom of the banner to set the normal color
  37. textView.setBackgroundColor(Color.argb(( int ) 255, 144,151,166));
  38. }
  39. }

OK, this achieves the effect you see at the top.

In fact, it is not difficult, it’s just that we haven’t implemented it ourselves. I believe that if we do it more ourselves, we can achieve what the UI wants.

<<:  Computing power on data platforms: Which GPUs are better suited for deep learning and databases?

>>:  Android image zooming animation is so simple

Recommend

This boy band disbanded, not because of their mediocre singing, but because...

There are few insects whose names are often unkno...

DeepMind's latest research: AI beats humans and designs better economic mechanisms

“Many of the problems facing humanity are not sim...

How to quickly remove oil stains from mobile phone screens using invoices

The biggest advantage of Android phones is that th...

Talk about iOS identification virtual positioning research

[[415572]] This article is reprinted from the WeC...

To Mi Meng, free poison, paid aphrodisiac

Some people say that content monetization is noth...

Analysis: What’s so clever about Qutoutiao’s fission strategy?

In 2017, a dark horse, Qutoutiao, emerged in the ...

How to prevent chronic diseases that kill 41 million people a year?

Author: Zhang Fengguang, epidemiology expert, cha...

Are the products on Taobao’s daily specials genuine? How can I participate?

The prices on Taobao’s daily specials are very ch...

Mini Program Global Configuration

The app.json file in the root directory of the mi...

How to deal with space junk? Does it need to be sorted?

Before we knew it, the Shenzhou 14 crew has been ...

The oldest forest fossils push the forest's "birthday" forward millions of years!

□ Feng Weimin Recently, the international geologi...