下面列出了怎么用android.support.annotation.FloatRange的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 设置状态栏darkMode,字体颜色及icon变黑(目前支持MIUI6以上,Flyme4以上,Android M以上)
*/
public static void darkMode(Window window, int color, @FloatRange(from = 0.0, to = 1.0) float alpha) {
if (isFlyme4Later()) {
darkModeForFlyme4(window, true);
immersive(window,color,alpha);
} else if (isMIUI6Later()) {
darkModeForMIUI6(window, true);
immersive(window,color,alpha);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
darkModeForM(window, true);
immersive(window, color, alpha);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
setTranslucentView((ViewGroup) window.getDecorView(), color, alpha);
} else {
immersive(window, color, alpha);
}
}
@Override
protected void computeUpdateValue(ValueAnimator animation, @FloatRange(from = 0.0, to = 1.0) float animatedValue)
{
mRotateAngle = 360 * animatedValue;
mAntiRotateAngle = 360 * (1 - animatedValue);
switch (mCurrAnimatorState)
{
case 0:
resetDrawPath();
mPathMeasure.setPath(mPath, false);
float stop = mPathMeasure.getLength() * animatedValue;
float start = 0;
mPathMeasure.getSegment(start, stop, mDrawPath, true);
break;
case 1:
resetDrawPath();
mPathMeasure.setPath(mPath, false);
stop = mPathMeasure.getLength();
start = mPathMeasure.getLength() * animatedValue;
mPathMeasure.getSegment(start, stop, mDrawPath, true);
break;
default:
break;
}
}
@Override
protected void drawProgress(Canvas canvas, @FloatRange(from=0.0F, to=1.0F) float progress){
path.reset();
//add outer rect
path.addRect(mOuterBound, Path.Direction.CCW);
//punch a hole
path.addCircle(mCenterX, mCenterY, mRadius, Path.Direction.CW);
canvas.drawPath(path, mPaint);
if(progress >= 0){
float angle = (1.0F - progress) * 360;
float startAngle = -90 + (360 - angle);
canvas.drawArc(mOval, startAngle, angle, true, mPaint);
}
}
/**
* Calculate a variant of the color to make it more suitable for overlaying information. Light
* colors will be lightened and dark colors will be darkened
*
* @param color the color to adjust
* @param isDark whether {@code color} is light or dark
* @param lightnessMultiplier the amount to modify the color e.g. 0.1f will alter it by 10%
* @return the adjusted color
*/
public static
@ColorInt
int scrimify(@ColorInt int color,
boolean isDark,
@FloatRange(from = 0f, to = 1f) float lightnessMultiplier) {
float[] hsl = new float[3];
android.support.v4.graphics.ColorUtils.colorToHSL(color, hsl);
if (!isDark) {
lightnessMultiplier += 1f;
} else {
lightnessMultiplier = 1f - lightnessMultiplier;
}
hsl[2] = MathUtils.constrain(0f, 1f, hsl[2] * lightnessMultiplier);
return android.support.v4.graphics.ColorUtils.HSLToColor(hsl);
}
/**
* 根据比例,在两个 color 值之间计算出一个 color 值
* <b>注意该方法是 ARGB 通道分开计算比例的</b>
*
* @param fromColor 开始的 color 值
* @param toColor 最终的 color 值
* @param fraction 比例,取值为 [0,1],为 0 时返回 fromColor, 为 1 时返回 toColor
* @return 计算出的 color 值
*/
public static int computeColor(@ColorInt int fromColor, @ColorInt int toColor, @FloatRange(from = 0, to = 1) float fraction) {
fraction = Math.max(Math.min(fraction, 1), 0);
int minColorA = Color.alpha(fromColor);
int maxColorA = Color.alpha(toColor);
int resultA = (int) ((maxColorA - minColorA) * fraction) + minColorA;
int minColorR = Color.red(fromColor);
int maxColorR = Color.red(toColor);
int resultR = (int) ((maxColorR - minColorR) * fraction) + minColorR;
int minColorG = Color.green(fromColor);
int maxColorG = Color.green(toColor);
int resultG = (int) ((maxColorG - minColorG) * fraction) + minColorG;
int minColorB = Color.blue(fromColor);
int maxColorB = Color.blue(toColor);
int resultB = (int) ((maxColorB - minColorB) * fraction) + minColorB;
return Color.argb(resultA, resultR, resultG, resultB);
}
public static void immersive(Window window, int color, @FloatRange(from = 0.0, to = 1.0) float alpha) {
if (Build.VERSION.SDK_INT >= 21) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(mixtureColor(color, alpha));
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
} else if (Build.VERSION.SDK_INT >= 19) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
setTranslucentView((ViewGroup) window.getDecorView(), color, alpha);
} else if (Build.VERSION.SDK_INT >= MIN_API && Build.VERSION.SDK_INT > 16) {
int systemUiVisibility = window.getDecorView().getSystemUiVisibility();
systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
window.getDecorView().setSystemUiVisibility(systemUiVisibility);
}
}
/**
* Calculate a variant of the color to make it more suitable for overlaying information. Light
* colors will be lightened and dark colors will be darkened
*
* @param color the color to adjust
* @param isDark whether {@code color} is light or dark
* @param lightnessMultiplier the amount to modify the color e.g. 0.1f will alter it by 10%
* @return the adjusted color
*/
public static
@ColorInt
int scrimify(@ColorInt int color,
boolean isDark,
@FloatRange(from = 0f, to = 1f) float lightnessMultiplier) {
float[] hsl = new float[3];
android.support.v4.graphics.ColorUtils.colorToHSL(color, hsl);
if (!isDark) {
lightnessMultiplier += 1f;
} else {
lightnessMultiplier = 1f - lightnessMultiplier;
}
hsl[2] = MathUtils.constrain(0f, 1f, hsl[2] * lightnessMultiplier);
return android.support.v4.graphics.ColorUtils.HSLToColor(hsl);
}
@FloatRange(from = 0, to = 1)
float getPercent() {
if (mAlpha <= 0f && mDAlpha <= 0f) {
return 1.f;
}
if (mRadius <= 0f && mDRadius <= 0f) {
return 1.f;
}
float percent;
if (mDAlpha >= 0f) {
percent = mAlpha / 255.f;
} else {
percent = 1.f - mAlpha / mInitialState.mAlpha;
}
//noinspection StatementWithEmptyBody
if (mDRadius >= 0f) {
//incalculable, do nothing
} else {
percent = Math.max(1.f - mRadius / mInitialState.mRadius, percent);
}
return percent;
}
/** 设置状态栏darkMode,字体颜色及icon变黑(目前支持MIUI6以上,Flyme4以上,Android M以上) */
public static void darkMode(Window window, int color, @FloatRange(from = 0.0, to = 1.0) float alpha) {
if (isFlyme4Later()) {
immersive(window,color,alpha);
darkModeForFlyme4(window, true);
} else if (isMIUI6Later()) {
immersive(window,color,alpha);
darkModeForMIUI6(window, true);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
immersive(window, color, alpha);
darkModeForM(window, true);
} else if (Build.VERSION.SDK_INT >= 19) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
setTranslucentView((ViewGroup) window.getDecorView(), color, alpha);
}
// if (Build.VERSION.SDK_INT >= 21) {
// window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// window.setStatusBarColor(Color.TRANSPARENT);
// } else if (Build.VERSION.SDK_INT >= 19) {
// window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// }
// setTranslucentView((ViewGroup) window.getDecorView(), color, alpha);
}
/**
* Android4.4以上的状态栏着色
*
* @param window 一般都是用于Activity的window,也可以是其他的例如Dialog,DialogFragment
* @param statusBarColor 状态栏颜色
* @param alpha 透明栏透明度[0.0-1.0]
*/
public static void tintStatusBar(Window window, @ColorInt int statusBarColor, @FloatRange(from = 0.0, to = 1.0) float alpha) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
} else {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
ViewGroup decorView = (ViewGroup) window.getDecorView();
ViewGroup contentView = (ViewGroup) window.getDecorView().findViewById(Window.ID_ANDROID_CONTENT);
View rootView = contentView.getChildAt(0);
if (rootView != null) {
ViewCompat.setFitsSystemWindows(rootView, true);
}
setStatusBar(decorView, statusBarColor, true);
setTranslucentView(decorView, alpha);
}
public void setScrimAlpha(@FloatRange(from = 0f, to = 1f) float alpha) {
if (scrimAlpha != alpha) {
scrimAlpha = alpha;
scrimPaint.setColor(PresentationUtils.modifyAlpha(scrimColor, scrimAlpha));
postInvalidateOnAnimation();
}
}
/**
* 设置沉浸式状态栏
*/
public static void immersive(Window window, int color, @FloatRange(from = 0.0, to = 1.0) float alpha) {
if(Build.VERSION.SDK_INT < MIN_API) return;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setStatusColor(window, color, alpha);
}else{
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
setTranslucentView((ViewGroup)window.getDecorView(), color, alpha);
}
}
public static void XYZToLAB(@FloatRange(from = 0.0d, to = 95.047d) double x, @FloatRange(from = 0.0d, to = 100.0d) double y, @FloatRange(from = 0.0d, to = 108.883d) double z, @NonNull double[] outLab) {
if (outLab.length != 3) {
throw new IllegalArgumentException("outLab must have a length of 3.");
}
x = pivotXyzComponent(x / XYZ_WHITE_REFERENCE_X);
y = pivotXyzComponent(y / XYZ_WHITE_REFERENCE_Y);
z = pivotXyzComponent(z / XYZ_WHITE_REFERENCE_Z);
outLab[0] = Math.max(0.0d, (116.0d * y) - 16.0d);
outLab[1] = 500.0d * (x - y);
outLab[2] = 200.0d * (y - z);
}
/**
* @see android.text.style.ScaleXSpan#ScaleXSpan(float)
*/
public static Span scaleXSize(@FloatRange(from = 0.0) final float proportion) {
return new Span(new SpanBuilder() {
@Override
public Object build() {
return new ScaleXSpan(proportion);
}
});
}
@Override
public void setOffset(@FloatRange(from = -1.0, to = 1.0) float offset) {
for (int i = getChildCount() - 1; i >= 0; i--) {
View child = getChildAt(i);
ParallaxLinearLayout.LayoutParams p = (LayoutParams) child.getLayoutParams();
if (p.parallaxFactor == 0)
continue;
child.setTranslationX(getWidth() * -offset * p.parallaxFactor);
}
}
/**
* 状态栏和导航栏颜色
*
* @param barColor the bar color
* @param barAlpha the bar alpha
* @return the immersion bar
*/
public ImmersionBar barColorInt(@ColorInt int barColor, @FloatRange(from = 0f, to = 1f) float barAlpha) {
mBarParams.statusBarColor = barColor;
mBarParams.navigationBarColor = barColor;
mBarParams.statusBarAlpha = barAlpha;
mBarParams.navigationBarAlpha = barAlpha;
return this;
}
/**
* 使用一个 Drawable 资源生成一个具有按下效果的 StateListDrawable
*
* @param context context
* @param res drawable resource
* @param mode mode for press
* @param alpha value
* @return a stateListDrawable
*/
private static Drawable createBgDrawable(@NonNull Context context, @DrawableRes int res, @PressedMode.Mode int mode, @FloatRange(from = 0.0f, to = 1.0f) float alpha) {
Drawable normal = context.getResources().getDrawable(res);
Drawable pressed = context.getResources().getDrawable(res);
Drawable unable = context.getResources().getDrawable(res);
pressed.mutate();
unable.mutate();
pressed = getPressedStateDrawable(context, mode, alpha, pressed);
unable = getUnableStateDrawable(context, unable);
return createStateListDrawable(normal, pressed, unable);
}
static boolean setAnimatorScale(
@NonNull Context context,
@FloatRange(from = 0.0, to = 10.0) float scale) {
try {
Settings.Global.putFloat(
context.getContentResolver(), Settings.Global.ANIMATOR_DURATION_SCALE, scale);
return true;
} catch (SecurityException se) {
String message = context.getString(R.string.permission_required_toast);
Toast.makeText(context.getApplicationContext(), message, Toast.LENGTH_LONG).show();
Log.d(TAG, message);
return false;
}
}
/**
* Set the distance between updates of the location
*
* @param metersBetweenUpdates the distance between the updates
* @return the instance of TrackerSettings
*/
public TrackerSettings setMetersBetweenUpdates(@FloatRange(from = 1) float metersBetweenUpdates) {
if (metersBetweenUpdates > 0) {
mMetersBetweenUpdates = metersBetweenUpdates;
}
return this;
}
/**
* Return the blur bitmap using render script.
*
* @param src The source of bitmap.
* @param radius The radius(0...25).
* @return the blur bitmap
*/
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap renderScriptBlur(final Bitmap src,
@FloatRange(
from = 0, to = 25, fromInclusive = false
) final float radius) {
return renderScriptBlur(src, radius, false);
}
/******************** radius *******************************/
public void setRadius(@FloatRange(from = 0) float radius) {
this.mRadius = radius;
mNormalBackground.setCornerRadius(mRadius);
mPressedBackground.setCornerRadius(mRadius);
mUnableBackground.setCornerRadius(mRadius);
}
private void updateVolume(@FloatRange(from = 0.0, to = 1.0) float volume) {
Log.d("AAAAAAAAA", "::" + String.valueOf(volume));
if (stepVolume > 1) {
stepVolume = 1;
} else if (stepVolume < 0) {
stepVolume = 0;
}
setVolume(stepVolume);
if (videoControls != null) {
videoControls.showVolumeLightView(stepVolume, ExoVideoControls.VOLUME_MODEL);
}
}
/**
* Set the span of shadow.
*
* @param radius The radius of shadow.
* @param dx X-axis offset, in pixel.
* @param dy Y-axis offset, in pixel.
* @param shadowColor The color of shadow.
* @return the single {@link SpanUtils} instance
*/
public SpanUtils setShadow(@FloatRange(from = 0, fromInclusive = false) final float radius,
final float dx,
final float dy,
final int shadowColor) {
this.shadowRadius = radius;
this.shadowDx = dx;
this.shadowDy = dy;
this.shadowColor = shadowColor;
return this;
}
public static void blendLAB(@NonNull double[] lab1, @NonNull double[] lab2, @FloatRange(from = 0.0d, to = 1.0d) double ratio, @NonNull double[] outResult) {
if (outResult.length != 3) {
throw new IllegalArgumentException("outResult must have a length of 3.");
}
double inverseRatio = 1.0d - ratio;
outResult[0] = (lab1[0] * inverseRatio) + (lab2[0] * ratio);
outResult[1] = (lab1[1] * inverseRatio) + (lab2[1] * ratio);
outResult[2] = (lab1[2] * inverseRatio) + (lab2[2] * ratio);
}
@Override
public void setOffset(@FloatRange(from = -1.0, to = 1.0) float offset) {
for (int i = getChildCount() - 1; i >= 0; i--) {
View child = getChildAt(i);
ParallaxRelativeLayout.LayoutParams p = (LayoutParams) child.getLayoutParams();
if (p.parallaxFactor == 0)
continue;
child.setTranslationX(getWidth() * -offset * p.parallaxFactor);
}
}
void setProgress(@FloatRange(from = 0f, to = 1f) float progress) {
if (matteLayer != null) {
matteLayer.setProgress(progress);
}
for (int i = 0; i < animations.size(); i++) {
animations.get(i).setProgress(progress);
}
}
public static RippleDrawable createRipple(@ColorInt int color,
@FloatRange(from = 0f, to = 1f) float alpha,
boolean bounded) {
color = ColorUtils.modifyAlpha(color, alpha);
return new RippleDrawable(ColorStateList.valueOf(color), null,
bounded ? new ColorDrawable(Color.WHITE) : null);
}
/**
* Create a decoration that draws a line in the given color and width between the items in the view.
*
* @param context a context to access the resources.
* @param color the color of the separator to draw.
* @param heightDp the height of the separator in dp.
*/
public SeparatorDecoration(@NonNull Context context, @ColorInt int color,
@FloatRange(from = 0, fromInclusive = false) float heightDp) {
mPaint = new Paint();
mPaint.setColor(color);
final float thickness = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
heightDp, context.getResources().getDisplayMetrics());
mPaint.setStrokeWidth(thickness);
}
/**
* 状态栏和导航栏颜色
*
* @param barColor the bar color
* @param barColorTransform the bar color transform
* @param barAlpha the bar alpha
* @return the immersion bar
*/
public ImmersionBar barColorInt(@ColorInt int barColor,
@ColorInt int barColorTransform,
@FloatRange(from = 0f, to = 1f) float barAlpha) {
mBarParams.statusBarColor = barColor;
mBarParams.navigationBarColor = barColor;
mBarParams.navigationBarColorTemp = mBarParams.navigationBarColor;
mBarParams.statusBarColorTransform = barColorTransform;
mBarParams.navigationBarColorTransform = barColorTransform;
mBarParams.statusBarAlpha = barAlpha;
mBarParams.navigationBarAlpha = barAlpha;
return this;
}
@Override
protected void setMaxCompressRatio(@FloatRange(from = 0.0, to = 1.0) float compressRatio) {
this.compressRatio = compressRatio;
}