Android 利用属性动画实现PopupWindow背景逐渐变暗
程序员文章站
2022-06-27 19:33:40
...
今天,简单讲讲android如何使用属性动画实现PopupWindow弹出后背景逐渐变暗。
昨天,记得自己讲了如何使用线程使PopupWindow弹出后背景逐渐变暗,那个其实很简单,其实还有一种代码也可以实现,就是使用属性动画来实现。其实也很简单。
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();
}
});
}
}
这个是在网上搜索了资料,其实就是在PopupWindow弹出会关闭的时候使用属性动画,ValueAnimator会根据起始值和结束值进行变化,使用interpolator =
new
LinearInterpolator();新建了一个匀速插值器,可以使属性动画匀速变化。然后设置AnimatorUpdateListener获取每一时刻的动画进度,设置背景的明暗。设置AnimatorListener监听动画结束事件。
Android 利用属性动画实现PopupWindow背景逐渐变暗就讲完了。
就这么简单。