轉載:http://blog.csdn.net/jan_s/article/details/43772697
我看到越來越多的應用使用這樣的效果,如QQ空間5.0的主界面,確實很好看!大概就搜了一下相關的實現方式,發現早就有了相關的方案:
仿QQ空間滾動ActionBar透明度變化Demo
還有我在github上看到就有這樣的實現方式,這也是本博文的主要核心內容:
具體請查看:https://github.com/AChep/Header2ActionBar
效果如下:
這是Demo結構:
1.FadingActionBarHelper.java 這個類是處理Actionbar的核心類,處理對scroll事件對actionbar背景色alpha的處理。
[java]?view plain
?copy ? public?class?FadingActionBarHelper?{?? ?? ????private?static?final?String?TAG?=?"FadingActionBarHelper";?? ?? ????private?int?mAlpha?=?255;?? ????private?Drawable?mDrawable;?? ????private?boolean?isAlphaLocked;?? ?? ????private?final?ActionBar?mActionBar;?? ?? ????public?FadingActionBarHelper(final?ActionBar?actionBar)?{?? ????????mActionBar?=?actionBar;?? ????}?? ?? ????public?FadingActionBarHelper(final?ActionBar?actionBar,?final?Drawable?drawable)?{?? ????????mActionBar?=?actionBar;?? ????????setActionBarBackgroundDrawable(drawable);?? ????}?? ?? ????public?void?setActionBarBackgroundDrawable(Drawable?drawable)?{?? ????????setActionBarBackgroundDrawable(drawable,?true);?? ????}?? ?? ????@TargetApi(Build.VERSION_CODES.KITKAT)?? ????public?void?setActionBarBackgroundDrawable(Drawable?drawable,?boolean?mutate)?{?? ????????mDrawable?=?mutate???drawable.mutate()?:?drawable;?? ????????mActionBar.setBackgroundDrawable(mDrawable);?? ????????if?(mAlpha?==?255)?{?? ????????????if?(Build.VERSION.SDK_INT?>=?Build.VERSION_CODES.KITKAT)?? ????????????????mAlpha?=?mDrawable.getAlpha();?? ????????}?else?{?? ????????????setActionBarAlpha(mAlpha);?? ????????}?? ????}?? ?? ????? ? ? ? ? ?? ????public?Drawable?getActionBarBackgroundDrawable()?{?? ????????return?mDrawable;?? ????}?? ?? ????? ? ? ? ? ? ? ? ? ? ?? ????public?void?setActionBarAlpha(int?alpha)?{?? ????????if?(mDrawable?==?null)?{?? ????????????Log.w(TAG,?"Set?action?bar?background?before?setting?the?alpha?level!");?? ????????????return;?? ????????}?? ????????if?(!isAlphaLocked)?{?? ????????????mDrawable.setAlpha(alpha);?? ????????????View?view?=?mActionBar.getCustomView();?? ????????????if(view!=null){?? ?????????????????? ????????????????if(alpha>=55){?? ????????????????????view.findViewById(R.id.search_button).setBackgroundResource(R.drawable.search);?? ????????????????????view.findViewById(R.id.refresh_button).setBackgroundResource(R.drawable.refresh);?? ????????????????}else{?? ????????????????????view.findViewById(R.id.search_button).setBackgroundResource(R.drawable.skin_nav_icon_l_search_rev);?? ????????????????????view.findViewById(R.id.refresh_button).setBackgroundResource(R.drawable.skin_nav_icon_r_refresh_rev);?? ????????????????}?? ????????????????Log.i(TAG,?"search_button.alpha=>"+alpha);?? ????????????}?? ????????}?? ????????mAlpha?=?alpha;?? ????}?? ?? ????public?int?getActionBarAlpha()?{?? ????????return?mAlpha;?? ????}?? ?? ????? ? ? ? ? ?? ????public?void?setActionBarAlphaLocked(boolean?lock)?{?? ?? ?????????? ????????if?(isAlphaLocked?!=?(isAlphaLocked?=?lock)?&&?!isAlphaLocked)?{?? ????????????setActionBarAlpha(mAlpha);?? ????????}?? ????}?? ?? ????public?boolean?isActionBarAlphaLocked()?{?? ????????return?isAlphaLocked;?? ????}?? }?? 2.其他的組件類我這不copy了,有興趣的朋友自行下載github上的項目吧o(∩_∩)o。
[java]?view plain
?copy ? public?class?NotifyingScrollView?extends?ScrollView?{?? ?????? ????private?boolean?mDisableEdgeEffects?=?true;?? ?? ????? ? ?? ????public?interface?OnScrollChangedListener?{?? ????????void?onScrollChanged(ScrollView?who,?int?l,?int?t,?int?oldl,?int?oldt);?? ????}?? ?? ????private?OnScrollChangedListener?mOnScrollChangedListener;?? ?? ????public?NotifyingScrollView(Context?context)?{?? ????????super(context);?? ????}?? ?? ????public?NotifyingScrollView(Context?context,?AttributeSet?attrs)?{?? ????????super(context,?attrs);?? ????}?? ?? ????public?NotifyingScrollView(Context?context,?AttributeSet?attrs,?int?defStyle)?{?? ????????super(context,?attrs,?defStyle);?? ????}?? ?? ????@Override?? ????protected?void?onScrollChanged(int?l,?int?t,?int?oldl,?int?oldt)?{?? ????????super.onScrollChanged(l,?t,?oldl,?oldt);?? ????????if?(mOnScrollChangedListener?!=?null)?{?? ????????????mOnScrollChangedListener.onScrollChanged(this,?l,?t,?oldl,?oldt);?? ????????}?? ????}?? ?? ????public?void?setOnScrollChangedListener(OnScrollChangedListener?listener)?{?? ????????mOnScrollChangedListener?=?listener;?? ????}?? ?? ????@Override?? ????protected?float?getTopFadingEdgeStrength()?{?? ?????????? ????????if?(mDisableEdgeEffects?&&?Build.VERSION.SDK_INT?<?Build.VERSION_CODES.HONEYCOMB)?{?? ????????????return?0.0f;?? ????????}?? ????????return?super.getTopFadingEdgeStrength();?? ????}?? ?? ????@Override?? ????protected?float?getBottomFadingEdgeStrength()?{?? ?????????? ????????if?(mDisableEdgeEffects?&&?Build.VERSION.SDK_INT?<?Build.VERSION_CODES.HONEYCOMB)?{?? ????????????return?0.0f;?? ????????}?? ????????return?super.getBottomFadingEdgeStrength();?? ????}?? }??
3.對于普通的java程序員來說,你可以不知道怎么個原理,但是你一定要知道怎么運用它,如何運用到我們的實際項目中并且改造它,這是demo的主界面,用起來就幾行代碼,是不是很叼?
[java]?view plain
?copy ? public?class?MainActivity?extends?Activity?{?? ?? ????private?FadingActionBarHelper?mFadingActionBarHelper;?? ????private?ActionBar?mActionBar;?? ?? ????@Override?? ????protected?void?onCreate(Bundle?savedInstanceState)?{?? ????????super.onCreate(savedInstanceState);?? ????????setContentView(R.layout.activity_main);?? ????????mActionBar?=?getActionBar();?? ?????????? ????????mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);?? ????????mActionBar.setCustomView(R.layout.my_actionbar);?? ?????????? ????????mFadingActionBarHelper?=?new?FadingActionBarHelper(getActionBar(),?? ????????????????getResources().getDrawable(R.drawable.actionbar_bg));?? ?? ????????if?(savedInstanceState?==?null)?{?? ????????????getFragmentManager().beginTransaction()?? ????????????????????.add(R.id.container,?new?ListViewFragment())?? ????????????????????.commit();?? ????????}?? ????}?? ?? ?? ????@Override?? ????public?boolean?onCreateOptionsMenu(Menu?menu)?{?? ?????????? ?????????? ?????????? ?? ????????return?true;?? ????}?? ?? ????@Override?? ????public?boolean?onOptionsItemSelected(MenuItem?item)?{?? ?????????? ?????????? ?????????? ????????return?super.onOptionsItemSelected(item);?? ????}?? ?????? ?????? ????public?FadingActionBarHelper?getFadingActionBarHelper()?{?? ????????return?mFadingActionBarHelper;?? ????}?? }??
貼了主要的代碼,其實這個實現方式還是有點復雜的,我想應該還有更簡潔的方式吧,有的話請留言分享!分享代碼是技術圈進步的一種有效方式哦!
----------------------------------------------------------
最后是Demo的下載鏈接,有興趣的請自行下載:http://download.csdn.net/download/sunjundelove/8444749
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
總結
以上是生活随笔為你收集整理的Android ActionBar随ScorllView上下拖动而透明度渐变效果的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。