当前位置:主页 > 移动开发 > Android代码 >

Android利用动画实现背景逐渐变暗

时间:2020-11-30 13:51:12 | 栏目:Android代码 | 点击:

前言

之前写了一篇Android-实现底部弹出PopupWindow并让背景逐渐变暗,介绍利用Handler动态改变背景透明度从而达到变暗的效果。现在补充一种方法,使用动画来实现相同的效果。

ValueAnimator Interpolator

今天的主角就是这俩,关于ValueAnimator和Interpolator(插值器)的概念请各位自行补充,这里主要讲述怎么用到我们这里来(因为我也不很懂(捂脸))。

效果

跟之前没有太大区别,只是为了演示变暗、变亮的过程 ↓

代码

AnimUtil.java

/**
 * 动画工具类
 * UpdateListener: 动画过程中通过添加此监听来回调数据
 * EndListener: 动画结束的时候通过此监听器来做一些处理
 */
public class AnimUtil {

  private ValueAnimator valueAnimator;
  private UpdateListener updateListener;
  private EndListener endListener;
  private long duration;
  private float start;
  private float end;
  private Interpolator interpolator = new LinearInterpolator();

  public AnimUtil() {
    duration = 1000; //默认动画时常1s
    start = 0.0f;
    end = 1.0f;
    interpolator = new LinearInterpolator();// 匀速的插值器
  }


  public void setDuration(int timeLength) {
    duration = timeLength;
  }

  public void setValueAnimator(float start, float end, long duration) {

    this.start = start;
    this.end = end;
    this.duration = duration;

  }

  public void setInterpolator(Interpolator interpolator) {
    this.interpolator = interpolator;
  }

  public void startAnimator() {
    if (valueAnimator != null){
      valueAnimator = null;
    }
    valueAnimator = ValueAnimator.ofFloat(start, end);
    valueAnimator.setDuration(duration);
    valueAnimator.setInterpolator(interpolator);
    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
      @Override
      public void onAnimationUpdate(ValueAnimator valueAnimator) {

        if (updateListener == null) {
          return;
        }

        float cur = (float) valueAnimator.getAnimatedValue();
        updateListener.progress(cur);
      }
    });
    valueAnimator.addListener(new Animator.AnimatorListener() {
      @Override
      public void onAnimationStart(Animator animator) {}
      @Override
      public void onAnimationEnd(Animator animator) {
        if(endListener == null){
          return;
        }
        endListener.endUpdate(animator);
      }
      @Override
      public void onAnimationCancel(Animator animator) {}

      @Override
      public void onAnimationRepeat(Animator animator) {}
    });
    valueAnimator.start();
  }

  public void addUpdateListener(UpdateListener updateListener) {

    this.updateListener = updateListener;
  }

  public void addEndListner(EndListener endListener){
    this.endListener = endListener;
  }

  public interface EndListener {
    void endUpdate(Animator animator);
  }

  public interface UpdateListener {

    void progress(float progress);
  }

}

MainActivity.java

public class MainActivity extends AppCompatActivity {
  private AnimUtil animUtil;
  private float bgAlpha = 1f;
  private boolean bright = false;
  PopupWindow popupWindow;
  Button button;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    animUtil = new AnimUtil();
    button = (Button) findViewById(R.id.btn);
    button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        bottomwindow(button);
        toggleBright();
      }
    });

  }

  private void toggleBright() {
    //三个参数分别为: 起始值 结束值 时长 那么整个动画回调过来的值就是从0.5f--1f的
    animUtil.setValueAnimator(0.5f, 1f, 350);
    animUtil.addUpdateListener(new AnimUtil.UpdateListener() {
      @Override
      public void progress(float progress) {
        //此处系统会根据上述三个值,计算每次回调的值是多少,我们根据这个值来改变透明度
        bgAlpha = bright ? progress : (1.5f - progress);//三目运算,应该挺好懂的。
        backgroundAlpha(bgAlpha);//在此处改变背景,这样就不用通过Handler去刷新了。
      }
    });
    animUtil.addEndListner(new AnimUtil.EndListener() {
      @Override
      public void endUpdate(Animator animator) {
        //在一次动画结束的时候,翻转状态
        bright = !bright;
      }
    });
    animUtil.startAnimator();
  }

  /***
   * 此方法用于改变背景的透明度,从而达到“变暗”的效果
   */
  private void backgroundAlpha(float bgAlpha) {
    WindowManager.LayoutParams lp = getWindow().getAttributes();
    lp.alpha = bgAlpha; //0.0-1.0
    getWindow().setAttributes(lp);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
  }


  void bottomwindow(View view) {
    if (popupWindow != null && popupWindow.isShowing()) {
      return;
    }
    LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(R.layout.window_popup, null);
    popupWindow = new PopupWindow(layout,
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.WRAP_CONTENT);
    //点击空白处时,隐藏掉pop窗口
    popupWindow.setFocusable(true);
    popupWindow.setBackgroundDrawable(new BitmapDrawable());

    popupWindow.setAnimationStyle(R.style.Popupwindow);
    int[] location = new int[2];
    view.getLocationOnScreen(location);
    popupWindow.showAtLocation(view, Gravity.LEFT | Gravity.BOTTOM, 0, -location[1]);
    popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
      @Override
      public void onDismiss() {
        toggleBright();
      }
    });
  }
}

代码很简单,注释都写了。

小结:

对比之前的那种用Handler的方法写的,这种感觉代码更简洁,更容易在多处使用,也算是填坑吧,哈哈。 如果本文埋下了另一个坑,请大家指正,谢谢!

您可能感兴趣的文章:

相关文章