个人中心滚动标题渐变颜色

/**
 *
 *页面
 *
 */
public class ShowWebImageActivity extends Activity implements ObservableScrollView.ScrollViewListener {

    @BindView(R.id.personScrollView)
    ObservableScrollView scrollView;
    @BindView(R.id.dt_listView)
    NoScrollListView listView;
    @BindView(R.id.img_bg)
    ImageView img_bg;


    @BindView(R.id.ttTitle)
    TextView textTitle;
    private int imageHeight;
    @BindView(R.id.title)
    RelativeLayout layout;
    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.iv_dian)
    ImageView iv_dian;


    List mData = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


     initListeners();
    setData();

    CommonlyAdapter adapter = new CommonlyAdapter(mData, this, R.layout.item) {
        @Override
        public void convert(ViewHolderHelper viewHolderHelper, ProductBean item, int position) {
            viewHolderHelper.setText(R.id.grid_item_label, item.getName());
        }


    };
        listView.setAdapter(adapter);
}

    private void setData() {
        ProductBean productBean;
        for (int i = 0; i < 10; i++) {
            productBean = new ProductBean();
            productBean.setName("第" + i + "条");
            mData.add(productBean);
        }
    }


    private void initListeners() {
        // 获取顶部图片高度后,设置滚动监听
        ViewTreeObserver vto = img_bg.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                img_bg.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                imageHeight = img_bg.getHeight();

                scrollView.setScrollViewListener(ShowWebImageActivity.this);
            }
        });
    }

    /**
     * 将dip或dp值转换为px值,保证尺寸大小不变
     *
     * @param dipValue (DisplayMetrics类中属性density)
     * @return
     */
    public static int dp2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_show_web_image;
    }

    @Override
    public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
        if (y <= 0) {
            layout.setBackgroundColor(Color.argb((int) 0, 227, 29, 26));//AGB由相关工具获得,或者美工提供
        } else if (y > 0 && y <= imageHeight) {
            float scale = (float) y / imageHeight;
            float alpha = (255 * scale);
            // 只是layout背景透明(仿知乎滑动效果)
            layout.setBackgroundColor(Color.argb((int) alpha, 255, 255, 255));
            textTitle.setTextColor(Color.rgb(255, 255, 255));
            textTitle.setText("");
            iconColorFilter(Color.parseColor("#ffffff"));
        } else {
            textTitle.setText("王妃");
            textTitle.setTextColor(Color.rgb(0, 0, 0));
            iconColorFilter(Color.parseColor("#000000"));
            layout.setBackgroundColor(Color.argb((int) 255, 255, 255, 255));
        }
    }
    /**
     * 标题栏/导航栏icon 颜色改变
     *
     * @param color
     */
    private void iconColorFilter(int color) {
        PorterDuffColorFilter colorFilter = new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP);
        iv_back.setColorFilter(colorFilter);
        iv_dian.setColorFilter(colorFilter);
    }

    @Override
    public void onScrollStop(boolean isScrollStop) {

    }
}
/**
 * 自定义 view 标题栏显示和隐藏
 */
public class ObservableScrollView extends ScrollView {
    public interface ScrollViewListener {

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

        void onScrollStop(boolean isScrollStop);
    }

    private ScrollViewListener scrollViewListener = null;

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

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

    public ObservableScrollView(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);
        }
    }
}

/*****布局******/



    

    

        

            

                

                

                    

                    

                        

                            

                            
                        

                        
                    
                
            

            

        
    
 
  
    

        

        

        

        
    

你可能感兴趣的:(个人中心滚动标题渐变颜色)