android 防微信朋友圈TopBar状态栏渐变模式

说白了没啥高深的东西简单的一笔。。。。

首先呢弄一个ScrollView并并重写方法 onScrollChanged 监听滑动的状态,然后根据自己需求改变其状态。

其次,看代码:

public class ObservableScrollView extends ScrollView {
    //底部图片
    private View viewHeigh;
    private View viewTopBar;
    private View decor;
    float f = -1;
    int height;

    private ScrollViewListener scrollViewListener = null;


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


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


    public ObservableScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
//        decor = this.getWindow().getDecorView();
    }


    public void setScrollViewListener(ScrollViewListener listener) {
        this.scrollViewListener = listener;
    }


    @Override
    protected void onScrollChanged(int x, int y, int oldx, int oldy) {
        super.onScrollChanged(x, y, oldx, oldy);
        setalph(y);
//        if(scrollViewListener != null) {
//            scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
//        }
    }

    private void setalph(int y){
        if (height == 0) {
            height = viewHeigh.getMeasuredHeight()-viewTopBar.getMeasuredHeight();
        } else if (y <= height*2/3) {
            setTopBar(0);
        } else if (y <= height) {
            if (y < 0) {
                y = 0;
            }
            f = (float) (y-height*2/3) / (float) (height/3);
            if(f<0.2){
                f = 0;
            }
            if (f > 1) {
                f = 1;
            }
            setTopBar(f);
        }else if(f<1){
            f = 1;
            setTopBar(f);
        }
    }

    private void setTopBar(float f) {
        viewTopBar.getBackground().setAlpha((int) (f * 255));
        // 通知标题栏刷新显示
        viewTopBar.invalidate();
        if (decor == null) {
            return;
        }
        //设置状态栏字体颜色
        if (f > 0.5) {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        } else {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }

    public void setViewBase(View viewHeigh,View viewTopBar,View window){
        this.viewHeigh = viewHeigh;
        this.viewTopBar = viewTopBar;
        this.decor = window;
    }


    public interface ScrollViewListener {
        void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);
    }
}

Activity代码:

public class FillScreenActivity extends BaseTwoActivity {
    ObservableScrollView scrollView;
    LinearLayout topBar;
    ImageView view;

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

    private void innitview() {
        scrollView = findViewById(R.id.myscrollview);
        topBar = findViewById(R.id.tipbar_linea);
        view = findViewById(R.id.toolbar_image);
        topBar.getBackground().setAlpha(0);
        topBar.setPadding(0,getStatusBarHeigh(),0,0);
        scrollView.setViewBase(view,topBar,getWindow().getDecorView());
    }


}
public class BaseTwoActivity extends AppCompatActivity {
    private static String TAG = "BaseActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//| Window.FEATURE_SWIPE_TO_DISMISS
        setStatusBarTranslucent();
        //一个创建、一个销毁时 exitAnim:预销毁的activity的动画
    }



/**
     * 设置透明状态栏
     *
     */
    private void setStatusBarTranslucent() {
        /**
         * 设置状态栏透明后: {windowIsTranslucent=(Manifest activity Theme中
         * android:windowIsTranslucent=true/false)}
         * 1:windowIsTranslucent对其不能产生任何影响 2:windowIsTranslucent
         * 可以解决弹出键盘时暂时出现的黑屏问题 3:xml中 fitsSystemWindows 能产生效果(状态栏空出自然色)
         */
     
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
            // 透明状态栏
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

            // // 透明导航栏
            // getWindow().addFlags(
            // WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            // 让虚拟导航栏一直不显示
            // Window window = getWindow();
            // WindowManager.LayoutParams params = window.getAttributes();
            // params.systemUiVisibility =
            // View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_IMMERSIVE;
            // window.setAttributes(params);
        }

    }

 /**
     * 获取状态栏高度
     *
     * @return
     */
    public int getStatusBarHeigh() {
        /**
         * 获取状态栏高度——方法1
         * */
        int statusBarHeight1 = -1;
        // 获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height",
                "dimen", "android");
        if (resourceId > 0) {
            // 根据资源ID获取响应的尺寸值
            statusBarHeight1 = getResources().getDimensionPixelSize(resourceId);
        }

        if (statusBarHeight1 != -1) {
            return statusBarHeight1;
        } else {
            /**
             * 获取状态栏高度——方法3 应用区的顶端位置即状态栏的高度 *注意*该方法不能在初始化的时候用
             * */
            Rect rectangle = new Rect();
            getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);

            return rectangle.top;
        }

    }


}

大功告成。

你可能感兴趣的:(android 防微信朋友圈TopBar状态栏渐变模式)