android 仿QQ好友动态 title滑动渐变

   最近QQ更新了qq空间消息列表。天天动听也使用了那效果。而且越来越多的android应用都在使用,咱们不能落后啊!

   得紧跟时代的步伐,不然就out了。 看起来不错分享下咯!

   首先贴一下效果图,因为gif动态图片不懂怎么搞。下次一定改进。


android 仿QQ好友动态 title滑动渐变_第1张图片


android 仿QQ好友动态 title滑动渐变_第2张图片


贴下代码先:

自定义了滑动时背景颜色渐变

  1. package com.seven.fadingdemo;


  2. import com.seven.fadingactionbardemo.R;


  3. import android.annotation.SuppressLint;
  4. import android.content.Context;
  5. import android.graphics.drawable.Drawable;
  6. import android.util.AttributeSet;
  7. import android.view.MotionEvent;
  8. import android.widget.ImageView;
  9. import android.widget.LinearLayout;


  10. /**
  11.  * 定义的控件布局
  12.  * 
  13.  * @author seven2729
  14.  */
  15. public class FadingScrollView extends LinearLayout {


  16. private LinearLayout mActionBar;


  17. private Drawable mBgDrawable;


  18. private ImageView fadingBar;// title渐变的控件


  19. private int fadingHeight; // 可隐藏的控件高度
  20. private int oldY;
  21. private int fadingOffset;


  22. public static final int ALPHA_START = 20;
  23. public static final int ALPHA_END = 255;


  24. public FadingScrollView(Context context) {
  25. this(context, null);
  26. }


  27. public FadingScrollView(Context context, AttributeSet attrs) {
  28. this(context, attrs, 0);
  29. }


  30. @SuppressLint("NewApi")
  31. public FadingScrollView(Context context, AttributeSet attrs,
  32. int defStyleAttr) {
  33. super(context, attrs, defStyleAttr);


  34. setOrientation(VERTICAL);
  35. }


  36. @Override
  37. protected void onFinishInflate() {
  38. super.onFinishInflate();


  39. fadingBar = (ImageView) findViewById(R.id.fading_bar);
  40. }


  41. @Override
  42. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  43. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  44. fadingHeight = fadingBar.getMeasuredHeight() - fadingOffset;// title渐变的控件距离
  45. }


  46. @SuppressLint("ClickableViewAccessibility")
  47. @Override
  48. public boolean onTouchEvent(MotionEvent ev) {


  49. switch (ev.getAction()) {
  50. case MotionEvent.ACTION_DOWN:
  51. oldY = (int) ev.getY();
  52. break;
  53. case MotionEvent.ACTION_MOVE:
  54. int scrollY = getScrollY();
  55. int y = (int) ev.getY();
  56. int deltaY = y - oldY;


  57. int willScrollY = scrollY - deltaY;


  58. if (willScrollY > fadingHeight) 
  59. {// 不做操作颜色保持不变
  60. // willScrollY = fadingHeight;
  61. }
  62. else 
  63. {// 颜色渐变
  64. updateActionBarAlpha(willScrollY * (ALPHA_END - ALPHA_START)
  65. / fadingHeight + ALPHA_START);
  66. }


  67. if (willScrollY < 0) {
  68. willScrollY = 0;
  69. }


  70. scrollTo(0, willScrollY);
  71. oldY = y;


  72. break;
  73. case MotionEvent.ACTION_UP:
  74. break;
  75. }


  76. return true;
  77. }


  78. public void bindingActionBar(LinearLayout actionBar) {
  79. mActionBar = actionBar;
  80. }


  81. @SuppressWarnings("deprecation")
  82. public void setActionBarBgDrawable(Drawable bgDrawable) {
  83. if (mActionBar == null) {
  84. try {
  85. throw new Exception(
  86. "Please try to binding the actionBar before set it's background.");
  87. } catch (Exception e) {
  88. e.printStackTrace();
  89. }
  90. }
  91. mBgDrawable = bgDrawable;
  92. mBgDrawable.setAlpha(ALPHA_START);
  93. mActionBar.setBackgroundDrawable(mBgDrawable);
  94. }


  95. @SuppressWarnings("deprecation")
  96. public void setActionBarAlpha(int alpha) throws Exception {
  97. if (mActionBar == null || mBgDrawable == null) {
  98. throw new Exception(
  99. "acitonBar is not binding or bgDrawable is not set.");
  100. }
  101. mBgDrawable.setAlpha(alpha);
  102. mActionBar.setBackgroundDrawable(mBgDrawable);
  103. }


  104. void updateActionBarAlpha(int alpha) {
  105. try {
  106. setActionBarAlpha(alpha);
  107. } catch (Exception e) {
  108. e.printStackTrace();
  109. }
  110. }


  111. public void setFadingOffset(int height) {
  112. fadingOffset = height;
  113. }
  114. }


然后就是代码的使用了,根据自己的需要设置大小或者颜色。

  1. package com.seven.fadingdemo;


  2. import com.seven.fadingactionbardemo.R;


  3. import android.annotation.SuppressLint;
  4. import android.app.Activity;
  5. import android.graphics.drawable.ColorDrawable;
  6. import android.os.Bundle;
  7. import android.widget.LinearLayout;


  8. /**
  9.  * 主页展示页面
  10.  * 
  11.  * @author seven2729
  12.  */
  13. public class MainActivity extends Activity {
  14. private LinearLayout mLayout;
  15. private FadingScrollView fadingScrollView;// 自定义滑动布局


  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. // requestWindowFeature(Window.FEATURE_NO_TITLE);
  20. setContentView(R.layout.activity_main);


  21. fadingScrollView = (FadingScrollView) findViewById(R.id.root);
  22. initLinearLayout();
  23. }


  24. /**
  25. * 布局初始化
  26. */
  27. @SuppressLint({ "Recycle", "InlinedApi" })
  28. void initLinearLayout() {
  29. //初始化fadingScrollView的一些信息
  30. mLayout = (LinearLayout) findViewById(R.id.lyout_title);
  31. ColorDrawable bgDrawable = new ColorDrawable(getResources().getColor(
  32. R.color.color_blue));
  33. fadingScrollView.bindingActionBar(mLayout);// 设置渐变布局
  34. fadingScrollView.setActionBarBgDrawable(bgDrawable);// 设置渐变颜色
  35. }

  36. }
颜色值的代码:
  1.     #FFFFFF
  2.     #000000
  3.     #00000000
  4.     #50BDFB
  5.     


因为最近有在研究,所以找点资料自己修改了。改成自己需要的

如有需要可以  下载源码

如源码有偏差请根据博文修改!

欢迎转载,转载请注明出处:http://blog.csdn.net/seven2729/article/details/48372851


你可能感兴趣的:(原创)