<!-- Base application theme. -->
<!-- Status bar color. -->
<!-- Customize your theme here. -->
package com.tool.id;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.appcompat.widget.Toolbar;
import com.manuelpeinado.fadingactionbar.view.ObservableScrollView;
import com.manuelpeinado.fadingactionbar.view.ObservableScrollable;
import com.manuelpeinado.fadingactionbar.view.OnScrollChangedCallback;
import com.readystatesoftware.systembartint.SystemBarTintManager;
public class MainActivity extends AppCompatActivity implements OnScrollChangedCallback {
private Toolbar mToolbar;
private Drawable mDrawable;
private View mHeader;
private int mDampedScroll;
private int mInitialStatusBarColor;
private int mFinalStatusBarColor;
private SystemBarTintManager mStatusbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = findViewById(R.id.toolbar);
mDrawable = mToolbar.getBackground();
setSupportActionBar(mToolbar);
mStatusbar = new SystemBarTintManager(this);
mStatusbar.setStatusBarTintEnabled(true);
mInitialStatusBarColor = Color.WHITE;
mFinalStatusBarColor = getResources().getColor(R.color.colorPrimary);
mHeader = findViewById(R.id.header);
ObservableScrollable scrollView = (ObservableScrollable) findViewById(R.id.scrollview);
scrollView.setOnScrollChangedCallback(this);
onScroll(-1, 0);
}
@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);
mDrawable.setAlpha(newAlpha);
mToolbar.setBackground(mDrawable);
}
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);
mStatusbar.setTintColor(Color.rgb(r, g, b));
}
private void updateParallaxEffect(int scrollPosition) {
float damping = 0.5f;
int dampedScroll = (int) (scrollPosition * damping);
int offset = mDampedScroll - dampedScroll;
mHeader.offsetTopAndBottom(-offset);
mDampedScroll = dampedScroll;
}
private int interpolate(int from, int to, float param) {
return (int) (from * param + to * (1 - param));
}
}