android标题栏滑动渐变效果

这篇博客展示了如何在Android应用中实现标题栏随着滚动而渐变的效果,包括颜色透明度和字体颜色的变化。通过获取图片高度并设置滚动监听,动态调整标题栏样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先看一下效果图:

Gif

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?xml version= "1.0"  encoding= "utf-8" ?>
<RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     xmlns:tools= "http://schemas.android.com/tools"
     android:layout_width= "match_parent"
     android:layout_height= "match_parent"
     tools:context= "com.hankkin.gradationtitlebar.QQSpeakActivity" >
 
     <com.hankkin.gradationscroll.GradationScrollView
         android:id= "@+id/scrollview"
         android:layout_width= "match_parent"
         android:layout_height= "match_parent"
         android:scrollbars= "none" >
         <LinearLayout
             android:layout_width= "match_parent"
             android:layout_height= "wrap_content"
             android:orientation= "vertical"  >
             <ImageView
                 android:id= "@+id/iv_banner"
                 android:scaleType= "fitXY"
                 android:src= "@drawable/banner3"
                 android:layout_width= "match_parent"
                 android:layout_height= "200dp"  />
             <com.hankkin.gradationscroll.NoScrollListview
                 android:id= "@+id/listview"
                 android:layout_width= "match_parent"
                 android:layout_height= "wrap_content"  >
             </com.hankkin.gradationscroll.NoScrollListview>
         </LinearLayout>
     </com.hankkin.gradationscroll.GradationScrollView>
     <TextView
         android:paddingBottom= "10dp"
         android:id= "@+id/textview"
         android:layout_width= "match_parent"
         android:layout_height= "55dp"
         android:gravity= "center|bottom"
         android:text= "我是标题"
         android:textSize= "18sp"
         android:textColor= "@color/transparent"
         android:background= "#00000000"  />
</RelativeLayout>

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public  class  GradationScrollView  extends  ScrollView {
 
     public  interface  ScrollViewListener {
         void  onScrollChanged(GradationScrollView scrollView,  int  x,  int  y,
                              int  oldx,  int  oldy);
     }
 
     private  ScrollViewListener scrollViewListener =  null ;
 
     public  GradationScrollView(Context context) {
         super (context);
     }
 
     public  GradationScrollView(Context context, AttributeSet attrs,
                                int  defStyle) {
         super (context, attrs, defStyle);
     }
 
     public  GradationScrollView(Context context, AttributeSet attrs) {
         super (context, attrs);
     }
 
     public  void  setScrollViewListener(ScrollViewListener scrollViewListener) {
         this .scrollViewListener = scrollViewListener;
     }
 
     @Override
     protected  void  onScrollChanged( int  x,  int  y,  int  oldx,  int  oldy) {
         super .onScrollChanged(x, y, oldx, oldy);
         if  (scrollViewListener !=  null ) {
             scrollViewListener.onScrollChanged( this , x, y, oldx, oldy);
         }
     }
}

我们需要获取图片的高度,并且设置滚动监听,随着滚动的距离来设置标题栏的颜色透明度和字体颜色的透明度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
   * 获取顶部图片高度后,设置滚动监听
*/
private  void  initListeners() {
 
         ViewTreeObserver vto = ivBanner.getViewTreeObserver();
         vto.addOnGlobalLayoutListener( new  ViewTreeObserver.OnGlobalLayoutListener() {
             @Override
             public  void  onGlobalLayout() {
                 textView.getViewTreeObserver().removeGlobalOnLayoutListener(
                         this );
                 height = ivBanner.getHeight();
 
                 scrollView.setScrollViewListener(QQSpeakActivity. this );
             }
         });
  }

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
      * 滑动监听
      * @param scrollView
      * @param x
      * @param y
      * @param oldx
      * @param oldy
*/
@Override
public  void  onScrollChanged(GradationScrollView scrollView,  int  x,  int  y,
                                 int  oldx,  int  oldy) {
         // TODO Auto-generated method stub
         if  (y <=  0 ) {    //设置标题的背景颜色
             textView.setBackgroundColor(Color.argb(( int 0 144 , 151 , 166 ));
         else  if  (y >  0  && y <= height) {  //滑动距离小于banner图的高度时,设置背景和字体颜色颜色透明度渐变
             float  scale = ( float ) y / height;
             float  alpha = ( 255  * scale);
             textView.setTextColor(Color.argb(( int ) alpha,  255 , 255 , 255 ));
             textView.setBackgroundColor(Color.argb(( int ) alpha,  144 , 151 , 166 ));
         else  {     //滑动到banner下面设置普通颜色
             textView.setBackgroundColor(Color.argb(( int 255 144 , 151 , 166 ));
         }
     }
github地址:https://github.com/a4575169/GradationTitleBar.git
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值