如果将这个方法写成一个工具类或者在BaseActivity or BaseFragment中调用,直接传俩参数就行
但是,如果,我们在开发过程中一不小心,将第二个参数写成了10f,那么可导致不可预期或者Error的发生
这样,Range就应运而生了
加入Range后的Code:
fun alpUpdate(context: Activity, @FloatRange(from = 0.0, to = 1.0) bgAlpha: Float) {
val lp = context.window.attributes
lp.alpha = bgAlpha //0.0-1.0
context.window.attributes = lp
context.window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
}
一旦传入参数小于或者大于range就会编译错误
先看下FloatRange源码
/*
-
Copyright © 2015 The Android Open Source Project
-
Licensed under the Apache License, Version 2.0 (the “License”);
-
you may not use this file except in compliance with the License.
-
You may obtain a copy of the License at
-
http://www.apache.org/licenses/LICENSE