android仿QQ空间滚动ActionBar透明度变化Demo(带源码)

QQ空间5.0的ActionBar会随着滚动的距离让ActionBar的透明度发生变化,效果还是非常cool,自己实现了一个小demo,基本实现了上面的所说的效果,但是其他控件的事件监听以及一系列较为复杂的控件的冲突还没考虑,暂时来总结下。


1,实现思路:

在最外层包裹一个自定义的FadingScrollView,监听滑动事件,然后去滚动控件,不过滚动范围只是最上面的fadingView的高度减去actionBar的高度,然后根据已经滚动的距离和fadingView的height来改变actionbar的透明度。还有一点注意让actionBar悬停在contentView之上。

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package huwei.com.fadingactionbardemo;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.drawable.Drawable;  
  5. import android.support.v7.app.ActionBar;  
  6. import android.util.AttributeSet;  
  7. import android.util.Log;  
  8. import android.view.MotionEvent;  
  9. import android.widget.ImageView;  
  10. import android.widget.LinearLayout;  
  11.   
  12.   
  13. /**  
  14.  * Created by huwei on 15-1-31.  
  15.  */  
  16. public class FadingScrollView extends LinearLayout {  
  17.     private static String TAG = "FadingScrollView";  
  18.   
  19.     private ActionBar mActionBar;  
  20.     private Drawable mBgDrawable;  
  21.   
  22.     private ImageView fadingBar;  
  23.   
  24.     private int fadingHeight;   //可隐藏的控件高度  
  25.     private int oldY;  
  26.     private int fadingOffset;  
  27.   
  28.     public static final int ALPHA_START=20;  
  29.     public static final int ALPHA_END=255;  
  30.   
  31.     public FadingScrollView(Context context) {  
  32.         this(context, null);  
  33.     }  
  34.   
  35.     public FadingScrollView(Context context, AttributeSet attrs) {  
  36.         this(context, attrs, 0);  
  37.     }  
  38.   
  39.     public FadingScrollView(Context context, AttributeSet attrs, int defStyleAttr) {  
  40.         super(context, attrs, defStyleAttr);  
  41.   
  42.         setOrientation(VERTICAL);  
  43.     }  
  44.   
  45.     @Override  
  46.     protected void onFinishInflate() {  
  47.         super.onFinishInflate();  
  48.   
  49.         fadingBar = (ImageView) findViewById(R.id.fading_bar);  
  50.     }  
  51.   
  52.     @Override  
  53.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  54.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  55.         fadingHeight = fadingBar.getMeasuredHeight()-fadingOffset;  
  56.     }  
  57.   
  58.     @Override  
  59.     public boolean onTouchEvent(MotionEvent ev) {  
  60.   
  61.         switch (ev.getAction()) {  
  62.             case MotionEvent.ACTION_DOWN:  
  63.                 oldY = (int) ev.getY();  
  64.                 break;  
  65.             case MotionEvent.ACTION_MOVE:  
  66.                 int scrollY = getScrollY();  
  67.   
  68.                 Log.i(TAG, "scrollY:" + scrollY + " ;-fadingHeight" + fadingHeight);  
  69.                 int y = (int) ev.getY();  
  70.                 int deltaY = y - oldY;  
  71.   
  72.                 int willScrollY = scrollY - deltaY;  
  73.   
  74.                 if (willScrollY > fadingHeight) {  
  75.                     willScrollY = fadingHeight;  
  76.                 }  
  77.   
  78.                 if (willScrollY < 0) {  
  79.                     willScrollY = 0;  
  80.                 }  
  81.   
  82.                 scrollTo(0, willScrollY);  
  83.                 updateActionBarAlpha(willScrollY*(ALPHA_END-ALPHA_START)/fadingHeight+ALPHA_START);  
  84.                 oldY = y;  
  85.   
  86.                 break;  
  87.             case MotionEvent.ACTION_UP:  
  88.                 break;  
  89.         }  
  90.   
  91.         return true;  
  92.     }  
  93.   
  94.     public void bindingActionBar(ActionBar actionBar) {  
  95.         mActionBar = actionBar;  
  96.     }  
  97.   
  98.     public void setActionBarBgDrawable(Drawable bgDrawable) throws Exception{  
  99.         if(mActionBar==null){  
  100.             throw new Exception("Please try to binding the actionBar before set it's background.");  
  101.         }  
  102.   
  103.         mBgDrawable = bgDrawable;  
  104.         mBgDrawable.setAlpha(ALPHA_START);  
  105.         mActionBar.setBackgroundDrawable(mBgDrawable);  
  106.     }  
  107.       
  108.     public void setActionBarAlpha(int alpha) throws Exception{  
  109.         if(mActionBar==null||mBgDrawable==null){  
  110.             throw new Exception("acitonBar is not binding or bgDrawable is not set.");  
  111.         }  
  112.         mBgDrawable.setAlpha(alpha);  
  113.         mActionBar.setBackgroundDrawable(mBgDrawable);  
  114.     }  
  115.       
  116.     void updateActionBarAlpha(int alpha){  
  117.         try {  
  118.             setActionBarAlpha(alpha);  
  119.         } catch (Exception e) {  
  120.             e.printStackTrace();  
  121.         }  
  122.           
  123.     }  
  124.       
  125.     public void setFadingOffset(int height){  
  126.         fadingOffset=height;  
  127.     }  
  128. }  


2,让ActionBar悬停。

1)代码实现:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. RequestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);  

2)自定义样式

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <!-- Base application theme. -->  
  2. <style name="AppTheme" parent="Theme.AppCompat.Light">  
  3.     <!-- Customize your theme here. -->  
  4.     <item name="windowActionBarOverlay">true</item>  
  5. </style>  

3,改变透明度

设置背景的Drawable的透明度即可,比如:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. mBgDrawable.setAlpha(ALPHA_START);  

  1. mActionBar.setBackgroundDrawable(mBgDrawable);  

       下载地址:http://download.csdn.net/detail/huweigoodboy/8415533

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值