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

/**
 *
 *页面
 *
 */
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<ProductBean> mData = new ArrayList<>();


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


     initListeners();
    setData();

    CommonlyAdapter adapter = new CommonlyAdapter<ProductBean>(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);
        }
    }
}

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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="@color/commonBg"
    android:orientation="vertical">
    <include layout="@layout/layout_common_throw"></include>

    <com.whd.demo.widget.view.ObservableScrollView
        android:id="@+id/personScrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/caddie_vis"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_weight="1"
        android:scrollbars="none">

        <LinearLayout
            android:background="@color/white"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white">

                <ImageView
                    android:id="@+id/img_bg"
                    android:layout_width="match_parent"
                    android:layout_height="160dp"
                    android:layout_gravity="center"
                    android:scaleType="centerCrop"
                    android:src="@mipmap/img_person_zhuye" />

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="135dp">

                    <ImageView
                        android:id="@+id/iv_cirlce"
                        android:layout_width="70dp"
                        android:layout_height="70dp"
                        android:layout_marginStart="20dp"
                        android:scaleType="centerCrop"
                        android:src="@mipmap/ic_head" />

                    <RelativeLayout
                        android:id="@+id/rl_ti"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:layout_marginLeft="10dp"
                        android:layout_toEndOf="@+id/iv_cirlce">

                        <LinearLayout
                            android:id="@+id/ll_n"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal">

                            <TextView
                                android:id="@+id/name"
                                style="@style/form_key"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center"
                                android:layout_marginRight="5dp"
                                android:layout_weight="1" />

                            <TextView
                                android:id="@+id/iv_caddie"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_below="@id/name"
                                android:layout_gravity="center_vertical"
                                android:layout_marginRight="10dp"
                                android:background="@drawable/bg_btn_white_border_normal"
                                android:padding="3dp"
                                android:text="教"
                                android:textSize="10sp" />
                        </LinearLayout>

                        <TextView
                            android:id="@+id/tv_caddie_ma"
                            style="@style/form_value"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@id/ll_n"
                            android:layout_marginLeft="0dp"
                            android:layout_marginTop="5dp"
                            android:gravity="left"
                            android:text="888888"
                            tools:ignore="UnknownId" />
                    </RelativeLayout>
                </RelativeLayout>
            </RelativeLayout>

            <com.whd.demo.widget.NoScrollListView
                android:id="@+id/dt_listView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scrollbars="none"></com.whd.demo.widget.widget.NoScrollListView>

        </LinearLayout>
    </com.whd.demo.view.ObservableScrollView>
 
  
    <RelativeLayout
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/iv_back"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:padding="10dp"
            android:src="@mipmap/ic_back_white" />

        <TextView
            android:id="@+id/ttTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:background="#00000000"
            android:gravity="center"
            android:text=""
            android:textColor="@android:color/white"
            android:textSize="18sp" />

        <ImageView
            android:id="@+id/iv_dian"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="12dp"
            android:padding="5dp"
            android:src="@mipmap/icon_title_point" />

        <ImageView
            android:id="@+id/guanfangrenzheng"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/iv_dian"
            android:gravity="center"
            android:src="@mipmap/ic_renzh"
            android:visibility="visible" />
    </RelativeLayout>
</RelativeLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值