android 滑动改变状态栏,android滑动改变状态栏颜色及透明度

RecyclerView根据滑动改变状态栏

//滑动监听事件

mRv.addOnScrollListener(new RecyclerView.OnScrollListener() {

//dy:每一次竖直滑动增量 向下为正 向上为负

@Override

public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

super.onScrolled(recyclerView, dx, dy);

final float density = mContext.getResources().getDisplayMetrics().density;

//滑动的距离

mDistanceY += dy;

//toolbar的高度

int toolbarHeight = (int) (214f * density) - mSearchLinear.getHeight();

//当滑动的距离 <= toolbar高度的时候,改变Toolbar背景色的透明度,达到渐变的效果

if (mDistanceY <= toolbarHeight) {

mSearchKefu.setImageResource(R.mipmap.icon_kf_white);

mSearchSaoyisao.setImageResource(R.mipmap.icon_sys_white);

// searchLocationIma.setBackgroundResource(R.mipmap.icon_location_white);

if (isIma) {

searchLocationImaBlack.setVisibility(View.GONE);

searchLocationIma.setVisibility(View.VISIBLE);

} else {

searchLocationText.setTextColor(Color.WHITE);

searchLocationText.setCompoundDrawables(null, null, drawable2, null);

}

mSearchEdit.setTextColor(Color.WHITE);

// mSearchLinearEdit.setBackgroundResource(R.drawable.bg_search);

setStateBarTextColor(false);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!针对您的问题,您可以使用 Android 中的 Toolbar 控件来实现滑动渐变标题栏。 具体实现步骤如下: 1. 在布局文件中添加 Toolbar 控件,并设置其高度为 wrap_content。 2. 在代码中通过 findViewById 获取 Toolbar 对象,并调用 setSupportActionBar 方法将其设置为当前 Activity 的 ActionBar。 3. 在 Activity 的 onCreate 方法中,通过 getSupportActionBar().setDisplayHomeAsUpEnabled(true) 设置标题栏左侧显示返回按钮。 4. 在滑动时,通过监听 RecyclerView 的滚动事件,动态改变标题栏的背景颜色透明度,实现滑动渐变效果。 5. 在 onScrollStateChanged 方法中,根据当前滚动状态判断是否需要执行动画效果。 下面是一个简单的示例代码: ```java // 获取 Toolbar 对象 Toolbar toolbar = findViewById(R.id.toolbar); // 将 Toolbar 设置为当前 Activity 的 ActionBar setSupportActionBar(toolbar); // 显示返回按钮 getSupportActionBar().setDisplayHomeAsUpEnabled(true); // 监听 RecyclerView 的滚动事件 recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); if (newState == RecyclerView.SCROLL_STATE_IDLE) { // 滑动停止时执行动画效果 animateToolbarColor(0xFF0000FF, 0x00000000); } } @Override public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); // 获取当前 RecyclerView 的滚动位置 int scrollY = recyclerView.computeVerticalScrollOffset(); // 计算标题栏透明度 int alpha = Math.min(255, scrollY * 2); // 执行动画效果 animateToolbarColor(Color.argb(alpha, 0xFF, 0x00, 0x00), Color.argb(0, 0x00, 0x00, 0x00)); } }); // 改变标题栏背景颜色透明度的动画效果 private void animateToolbarColor(int fromColor, int toColor) { ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), fromColor, toColor); colorAnimation.setDuration(250); colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animator) { toolbar.setBackgroundColor((int) animator.getAnimatedValue()); } }); colorAnimation.start(); } ``` 希望这个示例能够对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值