ScrollView上下滑动修改顶部title背景色淡入淡出

来源:互联网 发布:工业机器人编程用什么 编辑:程序博客网 时间:2024/05/22 06:31

MainActivity XML:

<?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.example.scroll.ObservableScrollView        android:id="@+id/scroll"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:scrollbars="none">        <com.example.scroll.ConflictListView            android:id="@+id/lv"            android:layout_width="match_parent"            android:layout_height="match_parent" />    </com.example.scroll.ObservableScrollView>    <RelativeLayout        android:id="@+id/relative"        android:layout_width="match_parent"        android:layout_height="60dp"        android:paddingTop="20dp">        <Button            android:id="@+id/bt1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="点我" />        <Button            android:id="@+id/bt2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentRight="true"            android:text="点我" />    </RelativeLayout></RelativeLayout>


MainActivity:

public class MainActivity extends AppCompatActivity implements ObservableScrollView.ScrollViewListener {    private ObservableScrollView mScrollView;    private ConflictListView mListView;    private RelativeLayout mTextView;    private int imageHight;    private ImageView mImageView;    private View headerView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        WindowUtils.setTraspartenStatusBar(this);        mListView = (ConflictListView) findViewById(R.id.lv);        mTextView = (RelativeLayout) findViewById(R.id.relative);        mScrollView = (ObservableScrollView) findViewById(R.id.scroll);        headerView = View.inflate(this, R.layout.header, null);        mImageView = (ImageView) headerView.findViewById(R.id.imageview);        Button bt1 = (Button) findViewById(R.id.bt1);        Button bt2 = (Button) findViewById(R.id.bt2);        findViewById(R.id.bt1).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Toast.makeText(MainActivity.this, "点我1", Toast.LENGTH_SHORT).show();            }        });        findViewById(R.id.bt2).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Toast.makeText(MainActivity.this, "点我2", Toast.LENGTH_SHORT).show();            }        });        initListeners();        initData();    }    private void initData() {        mListView.addHeaderView(headerView);        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.data));        mListView.setAdapter(adapter);    }    private void initListeners() {        // 获取顶部图片高度后,设置滚动监听        ViewTreeObserver vto = mImageView.getViewTreeObserver();        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {            @Override            public void onGlobalLayout() {                mImageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);                imageHight = mImageView.getHeight();                mScrollView.setScrollViewListener(MainActivity.this);            }        });    }    @Override    public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {        Log.i("TAG", "y--->" + y);        if (y <= 0) {            mTextView.setBackgroundColor(Color.argb((int) 0, 227, 29, 26));//AGB由相关工具获得,或者美工提供        } else if (y > 0 && y <= imageHight) {            float scale = (float) y / imageHight;            float alpha = (255 * scale);            // 只是layout背景透明(仿知乎滑动效果)            mTextView.setBackgroundColor(Color.argb((int) alpha, 227, 29, 26));        } else {            mTextView.setBackgroundColor(Color.argb((int) 255, 227, 29, 26));        }    }}

自定义的ScrollView:

public class ObservableScrollView extends ScrollView {public interface ScrollViewListener {void onScrollChanged(ObservableScrollView scrollView, int x, int y,                             int oldx, int oldy);}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;}@Overrideprotected 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);}}}



0 0
原创粉丝点击