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

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

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

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






贴下代码先:

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

  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. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.     <color name="white">#FFFFFF</color>
  4.     <color name="black">#000000</color>
  5.     <color name="transparent">#00000000</color>
  6.     <color name="color_blue">#50BDFB</color>
  7.     
  8. </resources>


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

如有需要可以  下载源码

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

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


  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值