亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android利用動(dòng)畫實(shí)現(xiàn)背景逐漸變暗

 更新時(shí)間:2016年12月15日 10:38:56   作者:Arbo_Xjb  
這篇文章主要為大家詳細(xì)介紹了Android利用動(dòng)畫實(shí)現(xiàn)背景逐漸變暗的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前言

之前寫了一篇Android-實(shí)現(xiàn)底部彈出PopupWindow并讓背景逐漸變暗,介紹利用Handler動(dòng)態(tài)改變背景透明度從而達(dá)到變暗的效果?,F(xiàn)在補(bǔ)充一種方法,使用動(dòng)畫來(lái)實(shí)現(xiàn)相同的效果。

ValueAnimator Interpolator

今天的主角就是這倆,關(guān)于ValueAnimator和Interpolator(插值器)的概念請(qǐng)各位自行補(bǔ)充,這里主要講述怎么用到我們這里來(lái)(因?yàn)槲乙膊缓芏?捂臉))。

效果

跟之前沒(méi)有太大區(qū)別,只是為了演示變暗、變亮的過(guò)程 ↓

代碼

AnimUtil.java

/**
 * 動(dòng)畫工具類
 * UpdateListener: 動(dòng)畫過(guò)程中通過(guò)添加此監(jiān)聽(tīng)來(lái)回調(diào)數(shù)據(jù)
 * EndListener: 動(dòng)畫結(jié)束的時(shí)候通過(guò)此監(jiān)聽(tīng)器來(lái)做一些處理
 */
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; //默認(rèn)動(dòng)畫時(shí)常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() {
    //三個(gè)參數(shù)分別為: 起始值 結(jié)束值 時(shí)長(zhǎng) 那么整個(gè)動(dòng)畫回調(diào)過(guò)來(lái)的值就是從0.5f--1f的
    animUtil.setValueAnimator(0.5f, 1f, 350);
    animUtil.addUpdateListener(new AnimUtil.UpdateListener() {
      @Override
      public void progress(float progress) {
        //此處系統(tǒng)會(huì)根據(jù)上述三個(gè)值,計(jì)算每次回調(diào)的值是多少,我們根據(jù)這個(gè)值來(lái)改變透明度
        bgAlpha = bright ? progress : (1.5f - progress);//三目運(yùn)算,應(yīng)該挺好懂的。
        backgroundAlpha(bgAlpha);//在此處改變背景,這樣就不用通過(guò)Handler去刷新了。
      }
    });
    animUtil.addEndListner(new AnimUtil.EndListener() {
      @Override
      public void endUpdate(Animator animator) {
        //在一次動(dòng)畫結(jié)束的時(shí)候,翻轉(zhuǎn)狀態(tài)
        bright = !bright;
      }
    });
    animUtil.startAnimator();
  }

  /***
   * 此方法用于改變背景的透明度,從而達(dá)到“變暗”的效果
   */
  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);
    //點(diǎn)擊空白處時(shí),隱藏掉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();
      }
    });
  }
}

代碼很簡(jiǎn)單,注釋都寫了。

小結(jié):

對(duì)比之前的那種用Handler的方法寫的,這種感覺(jué)代碼更簡(jiǎn)潔,更容易在多處使用,也算是填坑吧,哈哈。 如果本文埋下了另一個(gè)坑,請(qǐng)大家指正,謝謝!

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論