From 2a5aec98a9d2b1305806c38055362ba087312f77 Mon Sep 17 00:00:00 2001 From: Jens Driller Date: Fri, 21 Mar 2014 12:28:42 +0000 Subject: [PATCH] Add support for dynamically changing the background drawable --- .../FadingActionBarHelperBase.java | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/library/src/com/manuelpeinado/fadingactionbar/FadingActionBarHelperBase.java b/library/src/com/manuelpeinado/fadingactionbar/FadingActionBarHelperBase.java index 0721a66..1f109a4 100644 --- a/library/src/com/manuelpeinado/fadingactionbar/FadingActionBarHelperBase.java +++ b/library/src/com/manuelpeinado/fadingactionbar/FadingActionBarHelperBase.java @@ -170,12 +170,22 @@ public void initActionBar(Activity activity) { if (mActionBarBackgroundDrawable == null) { mActionBarBackgroundDrawable = activity.getResources().getDrawable(mActionBarBackgroundResId); } - setActionBarBackgroundDrawable(mActionBarBackgroundDrawable); + applyBackgroundDrawable(); + mActionBarBackgroundDrawable.setAlpha(0); + } + + public void setActionBarBackground(Drawable background) { + mActionBarBackgroundDrawable = background; + applyBackgroundDrawable(); + setAlpha(mLastScrollPosition); + } + + private void applyBackgroundDrawable() { + setActionBarBackgroundDrawable(mActionBarBackgroundDrawable); if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN) { mActionBarBackgroundDrawable.setCallback(mDrawableCallback); } - mActionBarBackgroundDrawable.setAlpha(0); - } + } protected abstract int getActionBarHeight(); protected abstract boolean isActionBarNull(); @@ -305,8 +315,13 @@ private void onNewScroll(int scrollPosition) { if (isActionBarNull()) { return; } + + setAlpha(scrollPosition); + addParallaxEffect(scrollPosition); + } - int currentHeaderHeight = mHeaderContainer.getHeight(); + private void setAlpha(int scrollPosition) { + int currentHeaderHeight = mHeaderContainer.getHeight(); if (currentHeaderHeight != mLastHeaderHeight) { updateHeaderHeight(currentHeaderHeight); } @@ -315,9 +330,7 @@ private void onNewScroll(int scrollPosition) { float ratio = (float) Math.min(Math.max(scrollPosition, 0), headerHeight) / headerHeight; int newAlpha = (int) (ratio * 255); mActionBarBackgroundDrawable.setAlpha(newAlpha); - - addParallaxEffect(scrollPosition); - } + } private void addParallaxEffect(int scrollPosition) { float damping = mUseParallax ? 0.5f : 1.0f;