Back to project page Jaguar.
The source code is released under:
Apache License
If you think the Android project Jaguar listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.richard.jaguar; /* www .j a v a 2s . c om*/ import android.graphics.Color; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.view.View; import com.readystatesoftware.systembartint.SystemBarTintManager; public class MainActivity extends ActionBarActivity implements OnScrollChangedCallback{ private Toolbar mToolbar; private Drawable mActionBarBackgroundDrawable; private View mHeader; private int mLastDampedScroll; private int mInitialStatusBarColor; private int mFinalStatusBarColor; private SystemBarTintManager mStatusBarManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mToolbar = (Toolbar) findViewById(R.id.toolbar); mActionBarBackgroundDrawable = mToolbar.getBackground(); setSupportActionBar(mToolbar); mStatusBarManager = new SystemBarTintManager(this); mStatusBarManager.setStatusBarTintEnabled(true); mInitialStatusBarColor = Color.BLACK; mFinalStatusBarColor = getResources().getColor(R.color.colorPrimaryDark); mHeader = findViewById(R.id.header); ObservableScrollable scrollView = (ObservableScrollable) findViewById(R.id.scroll_view); scrollView.setOnScrollChangedCallback(this); onScroll(-1, 0); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override public void onScroll(int l, int scrollPosition) { int headerHeight = mHeader.getHeight() - mToolbar.getHeight(); float ratio = 0; if (scrollPosition > 0 && headerHeight > 0) ratio = (float) Math.min(Math.max(scrollPosition, 0), headerHeight) / headerHeight; updateActionBarTransparency(ratio); updateStatusBarColor(ratio); updateParallaxEffect(scrollPosition); } private void updateActionBarTransparency(float scrollRatio) { int newAlpha = (int) (scrollRatio * 255); mActionBarBackgroundDrawable.setAlpha(newAlpha); mToolbar.setBackgroundDrawable(mActionBarBackgroundDrawable); } private void updateStatusBarColor(float scrollRatio) { int r = interpolate(Color.red(mInitialStatusBarColor), Color.red(mFinalStatusBarColor), 1 - scrollRatio); int g = interpolate(Color.green(mInitialStatusBarColor), Color.green(mFinalStatusBarColor), 1 - scrollRatio); int b = interpolate(Color.blue(mInitialStatusBarColor), Color.blue(mFinalStatusBarColor), 1 - scrollRatio); mStatusBarManager.setTintColor(Color.rgb(r, g, b)); } private void updateParallaxEffect(int scrollPosition) { float damping = 0.5f; int dampedScroll = (int) (scrollPosition * damping); int offset = mLastDampedScroll - dampedScroll; mHeader.offsetTopAndBottom(-offset); mLastDampedScroll = dampedScroll; } private int interpolate(int from, int to, float param) { return (int) (from * param + to * (1 - param)); } }