Android详情页结合ScrollView完成Title颜色渐变效果

啊啊啊,我又来更新了。

哈哈哈,分享最近有趣小点。

最近在做详情页滑动渐变显示与淡入淡出效果,我也是费尽脑筋查了又查,搜了又搜。

最后还是用最原生的方式实现了,多了不说少了不唠,仔细阅读以下文字。

note:接下来这个方法是获取view的原始有颜色,通过RGB计算将色值一点点调透明度。

private void setDetailTitleColor(int scrollY) {
    if (mDetailTitle == null) {
      return;
    }
    if (scrollY == 0) {
      mDetailTitle.setTextColor(Colo.White);
    } else {
      int color = mDetailTitle.getTextColors().getDefaultColor();
      int r = Color.red(color);
      int g = Color.green(color);
      int b = Color.blue(color);
      int changeToColor = Color.argb((255 * (scrollY - 1)), r, g, b);
      mDetailTitle.setTextColor(changeToColor);
    }
 }

附赠:渐渐隐藏和渐渐显示的动画

  public static void fadeInVisible(@NonNull View view, float startAlpha, float endAlpha) {
    if (view.getVisibility() == View.VISIBLE) {
      return;
    }
    view.setVisibility(View.VISIBLE);
    Animation animation = new AlphaAnimation(startAlpha, endAlpha);
    animation.setDuration(500);
    view.startAnimation(animation);
  }
  public static void fadeOutGone(@NonNull View view, float startAlpha, float endAlpha) {
    if (view.getVisibility() == View.GONE) {
      return;
    }
    view.setVisibility(View.GONE);
    Animation animation = new AlphaAnimation(startAlpha, endAlpha);
    animation.setDuration(500);
    view.startAnimation(animation);
  }

调用方式:

  //渐渐显示
  fadeInVisible(mDetailTitle, 0f, 1f);
  //渐渐隐藏
  fadeOutGone(mDetailTitle, 1f, 0f);

 

猜你喜欢

转载自blog.csdn.net/weixin_43917449/article/details/107862535
今日推荐