当前位置:   article > 正文

Android自定义ScrollView(在滑动时实现渐变标题栏背景颜色)_标题栏背景色渐变

标题栏背景色渐变

滑动前:

滑动中:

滑动后

1.自定义MyScrollView

  1. package com.jukopro.titlebarcolor;
  2. import android.content.Context;
  3. import android.util.AttributeSet;
  4. import android.widget.ScrollView;
  5. /**
  6. * 自定义带滚动监听的scrollview
  7. *
  8. */
  9. public class MyScrollView extends ScrollView {
  10. /**
  11. * 接口回调
  12. */
  13. private ScrollViewListener scrollViewListener = null;
  14. public interface ScrollViewListener {
  15. void onScrollChanged(ObservableScrollView scrollView, int x, int y,
  16. int oldx, int oldy);
  17. }
  18. public void setScrollViewListener(ScrollViewListener scrollViewListener) {
  19. this.scrollViewListener = scrollViewListener;
  20. }
  21. /**
  22. * 重写的onScrollChanged方法监听坐标
  23. * 这个监听在源码中没有写成回调的样子,
  24. * 只是写成了方法的样子,由于修饰这个方法的修饰符是protected,
  25. *(protected只能在本类,子类,同一包中调用),
  26. * 所以拿到ScrollView对象后在无法activity中调用此方法,
  27. * 所以只能重写后,子类中自动调用,
  28. * 所以要想在activity调用,
  29. * 就要写回调,
  30. * 上面就是我写的回调
  31. * 在Android源码中这种写法很多,在很多控件中都有
  32. */
  33. @Override
  34. protected void onScrollChanged(int x, int y, int oldx, int oldy) {
  35. super.onScrollChanged(x, y, oldx, oldy);
  36. if (scrollViewListener != null) {
  37. scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
  38. }
  39. }
  40. /**
  41. *构造方法(在这里没用)
  42. */
  43. public MyScrollView(Context context) {
  44. super(context);
  45. }
  46. public MyScrollView(Context context, AttributeSet attrs,
  47. int defStyle) {
  48. super(context, attrs, defStyle);
  49. }
  50. public MyScrollView(Context context, AttributeSet attrs) {
  51. super(context, attrs);
  52. }
  53. }

2.MainActivity

  1. package com.jukopro.titlebarcolor;
  2. import android.app.Activity;
  3. import android.graphics.Color;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.view.ViewTreeObserver;
  7. import android.view.ViewTreeObserver.OnGlobalLayoutListener;
  8. import android.widget.ArrayAdapter;
  9. import android.widget.ImageView;
  10. import android.widget.ListView;
  11. import android.widget.TextView;
  12. import com.jukopro.titlebarcolor.ObservableScrollView.ScrollViewListener;
  13. public class MainActivity extends Activity {
  14. private MyScrollView scrollView;
  15. private ImageView imageView;
  16. private TextView textView;
  17. private int imageHeight;
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);
  22. scrollView = (MyScrollView) findViewById(R.id.scrollview);
  23. imageView = (ImageView) findViewById(R.id.imageview);
  24. textView = (TextView) findViewById(R.id.textview);
  25. initListeners();
  26. }
  27. private void initListeners() {
  28. // 获取顶部图片高度后,设置滚动监听
  29. ViewTreeObserver vto = imageView.getViewTreeObserver();
  30. vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
  31. @Override
  32. public void onGlobalLayout() {
  33. imageView.getViewTreeObserver().removeGlobalOnLayoutListener(
  34. this);
  35. imageHeight = imageView.getHeight();
  36. scrollView.setScrollViewListener(new ScrollViewListener() {
  37. @Override
  38. public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
  39. // TODO Auto-generated method stub
  40. // Log.i("TAG", "y--->" + y + " height-->" + height);
  41. if (y <= 0) {
  42. // 设置文字背景颜色,白色
  43. textView.setBackgroundColor(Color.argb((int) 0, 255, 255, 255));//AGB由相关工具获得,或者美工提供
  44. // 设置文字颜色,黑色
  45. textView.setTextColor(Color.argb((int) 0, 255, 255, 255));
  46. Log.e("111","y <= 0");
  47. } else if (y > 0 && y <= imageHeight) {
  48. float scale = (float) y / imageHeight;
  49. float alpha = (255 * scale);
  50. // 只是layout背景透明(仿知乎滑动效果)白色透明
  51. textView.setBackgroundColor(Color.argb((int) alpha, 255, 255, 255));
  52. // 设置文字颜色,黑色,加透明度
  53. textView.setTextColor(Color.argb((int) alpha, 0, 0, 0));
  54. Log.e("111","y > 0 && y <= imageHeight");
  55. } else {
  56. // 白色不透明
  57. textView.setBackgroundColor(Color.argb((int) 255, 255, 255, 255));
  58. // 设置文字颜色
  59. //黑色
  60. textView.setTextColor(Color.argb((int) 255, 0, 0, 0));
  61. Log.e("111","else");
  62. }
  63. }
  64. });
  65. }
  66. });
  67. }
  68. }

3.activity_main布局文件
 

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context="${relativePackage}.${activityClass}" >
  6. <com.jiyaruo.titlebarcolor.MyScrollView
  7. android:id="@+id/scrollview"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. >
  11. <LinearLayout
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:orientation="vertical"
  15. >
  16. <ImageView
  17. android:id="@+id/imageview"
  18. android:layout_width="match_parent"
  19. android:layout_height="200dp"
  20. android:background="@mipmap/msvzs" />
  21. <TextView
  22. android:layout_width="match_parent"
  23. android:layout_height="50dp"
  24. android:text="话不多说我中意你"
  25. android:gravity="center"
  26. />
  27. <TextView
  28. android:layout_width="match_parent"
  29. android:layout_height="50dp"
  30. android:text="话不多说我中意你"
  31. android:gravity="center"
  32. />
  33. <TextView
  34. android:layout_width="match_parent"
  35. android:layout_height="50dp"
  36. android:text="话不多说我中意你"
  37. android:gravity="center"
  38. />
  39. <TextView
  40. android:layout_width="match_parent"
  41. android:layout_height="50dp"
  42. android:text="话不多说我中意你"
  43. android:gravity="center"
  44. />
  45. <TextView
  46. android:layout_width="match_parent"
  47. android:layout_height="50dp"
  48. android:text="话不多说我中意你"
  49. android:gravity="center"
  50. />
  51. <TextView
  52. android:layout_width="match_parent"
  53. android:layout_height="50dp"
  54. android:text="话不多说我中意你"
  55. android:gravity="center"
  56. />
  57. <TextView
  58. android:layout_width="match_parent"
  59. android:layout_height="50dp"
  60. android:text="话不多说我中意你"
  61. android:gravity="center"
  62. />
  63. <TextView
  64. android:layout_width="match_parent"
  65. android:layout_height="50dp"
  66. android:text="话不多说我中意你"
  67. android:gravity="center"
  68. />
  69. <TextView
  70. android:layout_width="match_parent"
  71. android:layout_height="50dp"
  72. android:text="话不多说我中意你"
  73. android:gravity="center"
  74. />
  75. <TextView
  76. android:layout_width="match_parent"
  77. android:layout_height="50dp"
  78. android:text="话不多说我中意你"
  79. android:gravity="center"
  80. />
  81. <TextView
  82. android:layout_width="match_parent"
  83. android:layout_height="50dp"
  84. android:text="话不多说我中意你"
  85. android:gravity="center"
  86. />
  87. <TextView
  88. android:layout_width="match_parent"
  89. android:layout_height="50dp"
  90. android:text="话不多说我中意你"
  91. android:gravity="center"
  92. />
  93. <TextView
  94. android:layout_width="match_parent"
  95. android:layout_height="50dp"
  96. android:text="话不多说我中意你"
  97. android:gravity="center"
  98. />
  99. </LinearLayout>
  100. </com.jiyaruo.titlebarcolor.MyScrollView>
  101. <TextView
  102. android:id="@+id/textview"
  103. android:layout_width="match_parent"
  104. android:layout_height="48dp"
  105. android:gravity="center"
  106. android:text="我是标题"
  107. android:textSize="18sp"
  108. android:textColor="#ffffff"
  109. android:background="#00000000" />
  110. </RelativeLayout>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/420615
推荐阅读
相关标签
  

闽ICP备14008679号