Sabtu, 06 Agustus 2022

Fade Toolbar

Fade Toolbar

build.gradle
implementation 'com.github.manuelpeinado.fadingactionbar:fadingactionbar:3.1.2'
implementation 'com.readystatesoftware.systembartint:systembartint:1.0.3'
activity_main
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffffff"
    android:clipToPadding="false"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity" >

    <com.manuelpeinado.fadingactionbar.view.ObservableScrollView
        android:id="@+id/scrollview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false"
        android:fillViewport="true"
        android:text="hello_world" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/header"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:src="@drawable/ic_launcher_background" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?android:colorBackground"
                android:padding="16dp"
                android:text="@string/teks_panjang" />
        </LinearLayout>
    </com.manuelpeinado.fadingactionbar.view.ObservableScrollView>

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top|center_vertical"
        android:background="?attr/colorPrimary"
        android:clipToPadding="false"
        android:minHeight="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

</FrameLayout>

themes
<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
      <item name="colorPrimary">@color/colorPrimary</item>
  <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
  
        <!-- Status bar color. -->
        <item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:fitsSystemWindows">true</item>
        <!-- Customize your theme here. -->
    </style>
</resources>

MainActivity
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));
      }
}