Android詳情頁結合ScrollView完成Title顏色漸變效果

啊啊啊,我又來更新了。java

哈哈哈,分享最近有趣小點。動畫

最近在作詳情頁滑動漸變顯示與淡入淡出效果,我也是費盡腦筋查了又查,搜了又搜。code

最後仍是用最原生的方式實現了,多了不說少了不嘮,仔細閱讀如下文字。get

note:接下來這個方法是獲取view的原始有顏色,經過RGB計算將色值一點點調透明度。animation

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);
    }
 }

附贈:漸漸隱藏和漸漸顯示的動畫it

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);
  }

調用方式:io

//漸漸顯示
  fadeInVisible(mDetailTitle, 0f, 1f);
  //漸漸隱藏
  fadeOutGone(mDetailTitle, 1f, 0f);
相關文章
相關標籤/搜索