android 上下滑動標題欄和狀態欄改變顏色實現


import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;

public class MyScrollView extends ScrollView {

    private OnScrollistener onScrollistener;

    public OnScrollistener getOnScrollistener() {
        return onScrollistener;
    }

    public void setOnScrollistener(OnScrollistener onScrollistener) {
        this.onScrollistener = onScrollistener;
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyScrollView(Context context) {
        super(context);
    }

    public interface OnScrollistener {

        void onScroll(int startY, int endY);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        onScrollistener.onScroll(oldt, t);
        super.onScrollChanged(l, t, oldl, oldt);
    }
}
import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {
    private View titleLine;
    private View titleLine1;
    private LinearLayout title;
    private LinearLayout top;
    private MyScrollView scrollView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        setTranslucentStatus(this, true);

        title = (LinearLayout) findViewById(R.id.ll_title);
        title.getBackground().mutate().setAlpha(0);
        top = (LinearLayout) findViewById(R.id.ll_title_top);
        titleLine1 = findViewById(R.id.v_title_line_1);
        titleLine = findViewById(R.id.v_title_line);
        scrollView = (MyScrollView) findViewById(R.id.sv_content);

        // 設置狀態欄高度
        int statusBarHeight = this.getResources().getDimensionPixelSize(this.getResources().getIdentifier("status_bar_height", "dimen", "android"));
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight);
        titleLine.setLayoutParams(params);
        titleLine1.setLayoutParams(params);

        // 設置滑動
        scrollView.setOnScrollistener(new MyScrollView.OnScrollistener() {

            @Override
            public void onScroll(int startY, int endY) {
                //根據scrollview滑動更改標題欄透明度
                changeAphla(startY, endY);
            }
        });
    }

    /**
     * 根據內容窗體的移動改變標題欄背景透明度
     *
     * @param startY scrollview開始滑動的y坐標(相對值)
     * @param endY   scrollview結束滑動的y坐標(相對值)
     */
    private void changeAphla(int startY, int endY) {
        //獲取標題高度
        int titleHeight = title.getMeasuredHeight();
        //獲取背景高度
        int backHeight = top.getMeasuredHeight();

        //獲取控件的絕對位置坐標
        int[] location = new int[2];
        top.getLocationInWindow(location);
        //從屏幕頂部到控件頂部的坐標位置Y
        int currentY = location[1];
        //表示回到原位(滑動到頂部)
        if (currentY >= 0) {
            title.getBackground().mutate().setAlpha(0);
        }

        Log.e("zpan", "=titleHeight=" + titleHeight + "=backHeight=" + backHeight + "=currentY=" + currentY);
        //顏色透明度改變
        if (currentY < titleHeight && currentY >= -(backHeight - titleHeight)) {
            //計算出滾動過程中改變的透明值
            double y = Math.abs(currentY) * 1.0;
            double height = (backHeight - titleHeight) * 1.0;
            int changeValue = (int) (y / height * 255);

            Log.e("zpan", "changeValue=" + changeValue);
            //判斷是向上還是向下
            if (endY > startY) {    //向上;透明度值增加,實際透明度減小
                title.getBackground().mutate().setAlpha(changeValue);
            } else if (endY < startY) {     //向下;透明度值減小,實際透明度增加
                title.getBackground().mutate().setAlpha(changeValue);
            }
        }
        //紅色背景移除屏幕
        if (currentY < -(backHeight - titleHeight)) {
            title.getBackground().mutate().setAlpha(255);
        }
    }

    /**
     * 設置狀態欄透明
     *
     * @param activity
     * @param on
     */
    public void setTranslucentStatus(Activity activity, boolean on) {
        Window win = activity.getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            win.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            win.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION //保證華為虛擬鍵盤能顯示
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            win.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            win.setStatusBarColor(Color.TRANSPARENT);
//            win.setNavigationBarColor(Color.TRANSPARENT); //保證華為虛擬鍵盤是系統色
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            WindowManager.LayoutParams winParams = win.getAttributes();
            final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            if (on) {
                winParams.flags |= bits;
            } else {
                winParams.flags &= ~bits;
            }
            win.setAttributes(winParams);
        }
    }

}

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.loaderman.flutterdemo.MyScrollView
        android:id="@+id/sv_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

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

            <LinearLayout
                android:id="@+id/ll_title_top"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:background="#2c5aff">

                <View
                    android:id="@+id/v_title_line"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"/>

                <View
                    android:layout_width="match_parent"
                    android:layout_height="50dp"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="150dp"
                    android:gravity="center"
                    android:text="狀\n態\n改\n變"
                    android:textColor="#ffffff"
                    android:textSize="24sp"/>

            </LinearLayout>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="1600dp"
                android:gravity="center"
                android:background="#b5f581"
                android:text="內\n容\n區\n域"
                android:textColor="#ffffff"
                android:textSize="24sp"/>

        </LinearLayout>
    </com.loaderman.flutterdemo.MyScrollView>

    <LinearLayout
        android:id="@+id/ll_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="#f10303">

        <View
            android:id="@+id/v_title_line_1"
            android:layout_width="match_parent"
            android:layout_height="0dp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center"
            android:text="頭部"
            android:textColor="#ffffff"
            android:textSize="18sp"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="#fff"/>

    </LinearLayout>
</RelativeLayout>

效果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM