Android 渐变式状态栏实现

先看效果图

《Android 渐变式状态栏实现》 ezgif-3-72b34af0d0.gif

实现步骤

1.xml文件布局
<com.wocus.wine.baseview.GradationScrollView
        android:id="@+id/house_details_scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none"
        android:layout_marginBottom="50dp
      //内容
</com.wocus.wine.baseview.GradationScrollView>

如下图

《Android 渐变式状态栏实现》 image.png

2.自定义View提供
package com.wocus.wine.baseview;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;
/**
 * 带滚动监听的scrollview
 *
 */
public class GradationScrollView extends ScrollView {

    public interface ScrollViewListener {

        void onScrollChanged(GradationScrollView scrollView, int x, int y,
                             int oldx, int oldy);

    }

    private ScrollViewListener scrollViewListener = null;

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

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

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

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

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

}
3.Activity中的使用

以哪个View为对象,当View可见的高度百分比去实现标题栏的透明度,我这里以Banner为对象

val vto = house_details_img_banner.viewTreeObserver
        vto.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                house_details_txt_title.viewTreeObserver.removeGlobalOnLayoutListener(this)
                height = house_details_img_banner.height
                house_details_scrollView.setScrollViewListener(this@HouseDetailsActivity)
            }
        })

监听方法:GradationScrollView.ScrollViewListener

override fun onScrollChanged(scrollView: GradationScrollView?, x: Int, y: Int, oldx: Int, oldy: Int) =
            when {
                y<=0 -> {
                    house_details_view_top.setBackgroundColor(Color.argb(0,18,150,219))
                    house_details_txt_title.setTextColor(Color.argb(0,255,255,255))
                    house_details_layout_title.setBackgroundColor(Color.argb(0,18,150,219))
                }
                y in 1..height -> {
                    val bai= 100*y/height
                    val alpha=255*bai/100
                    house_details_txt_title.setTextColor(Color.argb(alpha,255,255,255))
                    house_details_layout_title.setBackgroundColor(Color.argb(alpha, 18,150,219))
                    house_details_view_top.setBackgroundColor(Color.argb(alpha, 18,150,219))
                }
                else -> {
                    house_details_view_top.setBackgroundColor(Color.argb(255,18,150,219))
                    house_details_txt_title.setTextColor(Color.argb(255,255,255,255))
                    house_details_layout_title.setBackgroundColor(Color.argb(255,18,150,219))
                }
            }

简单吧?,有任何疑问Q我752422962

    原文作者:Wocus
    原文地址: https://www.jianshu.com/p/7c090da2a408
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞