Created
February 20, 2015 22:50
-
-
Save anonymous/856dec9d460015968351 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void setupFadingToolbarScrollView() { | |
final ImageView iv = (ImageView) findViewById(R.id.toolbar_anchored_image); | |
if(iv == null) | |
throw new NullPointerException("You must declare an imageview named 'toolbar_anchored_image' to use fading toolbar"); | |
int imageHeight = iv.getLayoutParams().height; | |
int toolbarHeight = mToolbar.getHeight(); | |
int actionBarFullAlphaAt = imageHeight - toolbarHeight; | |
final float alphaScale = 255f / actionBarFullAlphaAt; | |
final Drawable drawable = getResources().getDrawable(R.drawable.actionbar_background); | |
//Use existing alpha which will either be 0, or passed from savedInstanceState | |
drawable.setAlpha((int) mToolbarAlpha); | |
mToolbar.setBackground(drawable); | |
//TODO do the commented out section below when you have time | |
//If they have a title declared, set it up for falling into the toolbar | |
final TextView title = (TextView) findViewById(R.id.toolbar_anchored_title); | |
final ViewGroup root = (ViewGroup) findViewById(R.id.root); | |
int finalTitleY = 120; //TODO replace with correct dimen based on whatever | |
int finalTitleX = 180; //TODO replace with correct dimen based on whatever | |
int[] startingPosition = new int[2]; | |
title.getLocationInWindow(startingPosition); | |
final int startingX = startingPosition[0]; | |
final int startingY = startingPosition[1]; | |
//Start moving title as soon as title hits bottom of toolbar | |
final int moveTitleStartAt = startingY - toolbarHeight; | |
final float moveTitleFinishedAt = actionBarFullAlphaAt; //TODO just for now | |
final float yScale = finalTitleY / moveTitleFinishedAt; | |
final float xScale = finalTitleX / moveTitleFinishedAt; | |
ObservableScrollView scrollView = (ObservableScrollView) findViewById(R.id.toolbar_anchored_scrollview); | |
if(scrollView == null) | |
throw new NullPointerException("Your must declare an ObservableScrollView named 'toolbar_anchored_scrollview' to use fading toolbar"); | |
scrollView.setOnScrollChangedListener(new ObservableScrollView.OnScrollListener() { | |
private TextView aNewTitle; | |
@Override | |
public void onScroll(int l, int t) { | |
//Get alpha for toolbar background | |
mToolbarAlpha = alphaScale * t; | |
mToolbarAlpha = Math.min(mToolbarAlpha, 255f); | |
mToolbarAlpha = Math.max(mToolbarAlpha, 0); | |
drawable.setAlpha((int) mToolbarAlpha); | |
//Only apply when between the move sections, this might be done cleaner with min and max like above | |
if(t >= moveTitleStartAt && t <= moveTitleFinishedAt) { | |
Print.log("in the title move zone", t, moveTitleStartAt); | |
//This makes the 0 point the point at which we start moving, not 0 itself | |
int pixelsFromStartPoint = t - moveTitleStartAt; | |
//At 0, the view should be at its starting position, so we add them to the formula | |
float newX = xScale * pixelsFromStartPoint + startingX; | |
float newY = yScale * pixelsFromStartPoint + startingY; | |
Print.log("new coords", newX, newY); | |
if(aNewTitle == null) { | |
aNewTitle = new TextView(BaseActivity.this); | |
aNewTitle.setTextAppearance(BaseActivity.this, R.style.Widget_Bandsintown_ImageOverlayTextAppearance); | |
aNewTitle.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 24); //TODO make a dimen resource | |
aNewTitle.setText(title.getText()); | |
root.addView(aNewTitle); | |
} | |
aNewTitle.setX(newX); | |
aNewTitle.setY(newY); | |
if(pixelsFromStartPoint > 0) { | |
title.setVisibility(View.GONE); | |
aNewTitle.setVisibility(View.VISIBLE); | |
} | |
else { | |
title.setVisibility(View.VISIBLE); | |
aNewTitle.setVisibility(View.GONE); | |
} | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment