下面列出了怎么用androidx.annotation.FloatRange的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 创建一张渐变图片,支持韵脚。
*
* @param startColor 渐变开始色
* @param endColor 渐变结束色
* @param radius 圆角大小
* @param centerX 渐变中心点 X 轴坐标
* @param centerY 渐变中心点 Y 轴坐标
* @return 返回所创建的渐变图片。
*/
@TargetApi(16)
public static GradientDrawable getCircleGradientDrawable(@ColorInt int startColor,
@ColorInt int endColor, int radius,
@FloatRange(from = 0f, to = 1f) float centerX,
@FloatRange(from = 0f, to = 1f) float centerY) {
GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setColors(new int[]{
startColor,
endColor
});
gradientDrawable.setGradientType(GradientDrawable.RADIAL_GRADIENT);
gradientDrawable.setGradientRadius(radius);
gradientDrawable.setGradientCenter(centerX, centerY);
return gradientDrawable;
}
/**
* Calculates the amount of the visibility of the {@link com.google.android.exoplayer2.ui.PlayerView}
* on the screen.
* Used to determine the visibility area ratio (a value between 0.0 and 1.0), so that further
* playback management related actions can be taken by the host {@link com.arthurivanets.arvi.widget.PlayableItemsContainer}.
*
* @param playable the playable
* @return the visibility are offset (a value between 0.0 and 1.0)
*/
@FloatRange(from = 0.0, to = 1.0)
public static float getVisibleAreaOffset(@NonNull Playable playable) {
Preconditions.nonNull(playable);
if(playable.getParent() == null) {
return 0f;
}
final View playerView = playable.getPlayerView();
final Rect drawRect = new Rect();
final Rect playerRect = new Rect();
playerView.getDrawingRect(drawRect);
final int drawArea = (drawRect.width() * drawRect.height());
final boolean isVisible = playerView.getGlobalVisibleRect(playerRect, new Point());
if(isVisible && (drawArea > 0)) {
final int visibleArea = (playerRect.height() * playerRect.width());
return (visibleArea / (float) drawArea);
}
return 0f;
}
public static Data setCalibrationValue(@FloatRange(from = 0) final float glucoseConcentrationOfCalibration,
@GlucoseSampleType final int sampleType,
@GlucoseSampleLocation final int sampleLocation,
@IntRange(from = 0, to = 65535) final int calibrationTime,
@IntRange(from = 0, to = 65535) final int nextCalibrationTime,
final boolean secure) {
final MutableData data = new MutableData(new byte[11 + (secure ? 2 : 0)]);
data.setByte(OP_CODE_SET_CALIBRATION_VALUE, 0);
data.setValue(glucoseConcentrationOfCalibration, Data.FORMAT_SFLOAT, 1);
data.setValue(calibrationTime, Data.FORMAT_UINT16, 3);
final int typeAndSampleLocation = ((sampleLocation & 0xF) << 8) | (sampleType & 0xF);
data.setValue(typeAndSampleLocation, Data.FORMAT_UINT8, 5);
data.setValue(nextCalibrationTime, Data.FORMAT_UINT16, 6);
data.setValue(0, Data.FORMAT_UINT16, 8); // ignored: calibration data record number
data.setValue(0, Data.FORMAT_UINT8, 10); // ignored: calibration status
return appendCrc(data, secure);
}
/**
* Set the alpha component of {@code color} to be {@code alpha}.
*/
public static @CheckResult
@ColorInt
int modifyAlpha(@ColorInt int color,
@FloatRange(from = 0f, to = 1f) float alpha) {
return modifyAlpha(color, (int) (255f * alpha));
}
public void setTrackWidth(@FloatRange(from = Float.MIN_VALUE) float trackWidth) {
if (trackWidth <= 0) {
throw new IllegalArgumentException("Track width must be a positive number.");
}
mTrackWidth = trackWidth;
generateInactiveTrackPath();
invalidate();
}
public void setThumbRadius(@FloatRange(from = Float.MIN_VALUE) float radius) {
if (radius <= 0) {
throw new IllegalArgumentException("Thumb radius must be a positive number.");
}
mRadius = radius;
generateInactiveTrackPath();
invalidate();
}
/**
* 设置状态栏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);
}
// 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);
}
public TransformationEvent(@NonNull String id,
@EventType int type,
@FloatRange(from = 0, to = 1) float progress,
@Nullable Throwable cause) {
this.id = id;
this.type = type;
this.progress = progress;
this.cause = cause;
}
/******************** radius *******************************/
public void setRadius(@FloatRange(from = 0) float radius) {
this.mRadius = radius;
mNormalBackground.setCornerRadius(mRadius);
mPressedBackground.setCornerRadius(mRadius);
mUnableBackground.setCornerRadius(mRadius);
}
@ColorInt
public static int shiftColor(@ColorInt int color, @FloatRange(from = 0.0f, to = 2.0f) float by) {
if (by == 1f) return color;
int alpha = Color.alpha(color);
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
hsv[2] *= by; // value component
return (alpha << 24) + (0x00ffffff & Color.HSVToColor(hsv));
}
@ColorInt
public static int adjustAlpha(@ColorInt int color, @FloatRange(from = 0.0, to = 1.0) float factor) {
int alpha = Math.round(Color.alpha(color) * factor);
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
return Color.argb(alpha, red, green, blue);
}
/**
* 导航栏颜色
*
* @param navigationBarColor the navigation bar color 导航栏颜色
* @param navigationAlpha the navigation alpha 透明度
* @return the immersion bar
*/
public ImmersionBar navigationBarColorInt(@ColorInt int navigationBarColor,
@FloatRange(from = 0f, to = 1f) float navigationAlpha) {
mBarParams.navigationBarColor = navigationBarColor;
mBarParams.navigationBarAlpha = navigationAlpha;
mBarParams.navigationBarColorTemp = mBarParams.navigationBarColor;
return this;
}
/**
* Adjusts the alpha channel of the specified color.
*
* @param color The color to adjust alpha of
* @param alpha The alpha value
*
* @return The color with the adjusted alpha channel
*/
public static int adjustColorAlpha(int color, @FloatRange(from = 0.0, to = 1.0) float alpha) {
final int alphaChannel = (int) (255 * alpha);
final int redChannel = Color.red(color);
final int greenChannel = Color.green(color);
final int blueChannel = Color.blue(color);
return Color.argb(alphaChannel, redChannel, greenChannel, blueChannel);
}
/**
* 状态栏和导航栏颜色
*
* @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.navigationBarColorTemp = mBarParams.navigationBarColor;
mBarParams.statusBarAlpha = barAlpha;
mBarParams.navigationBarAlpha = barAlpha;
return this;
}
/**
* 解决布局与状态栏重叠问题,支持侧滑返回
* Fits system windows immersion bar.
*
* @param fits the fits
* @param statusBarColorContentView the status bar color content view 状态栏颜色
* @param statusBarColorContentViewTransform the status bar color content view transform 状态栏变色后的颜色
* @param statusBarContentViewAlpha the status bar content view alpha 透明度
* @return the immersion bar
*/
public ImmersionBar fitsSystemWindows(boolean fits, @ColorRes int statusBarColorContentView
, @ColorRes int statusBarColorContentViewTransform, @FloatRange(from = 0f, to = 1f) float statusBarContentViewAlpha) {
mBarParams.fits = fits;
mBarParams.statusBarColorContentView = ContextCompat.getColor(mActivity, statusBarColorContentView);
mBarParams.statusBarColorContentViewTransform = ContextCompat.getColor(mActivity, statusBarColorContentViewTransform);
mBarParams.statusBarContentViewAlpha = statusBarContentViewAlpha;
mBarParams.statusBarColorContentView = ContextCompat.getColor(mActivity, statusBarColorContentView);
mContentView.setBackgroundColor(ColorUtils.blendARGB(mBarParams.statusBarColorContentView,
mBarParams.statusBarColorContentViewTransform, mBarParams.statusBarContentViewAlpha));
return this;
}
/**
* 导航栏字体深色或亮色,判断设备支不支持状态栏变色来设置状态栏透明度
* Status bar dark font immersion bar.
*
* @param isDarkFont the is dark font
* @param statusAlpha the status alpha 如果不支持状态栏字体变色可以使用statusAlpha来指定状态栏透明度,比如白色状态栏的时候可以用到
* @return the immersion bar
*/
public ImmersionBar navigationBarDarkFont(boolean isDarkFont, @FloatRange(from = 0f, to = 1f) float statusAlpha) {
mBarParams.navigationBarDarkFont = isDarkFont;
if (canNavigationBarDarkFont()) {
mBarParams.navigationBarAlpha = 0;
} else {
mBarParams.navigationBarAlpha = statusAlpha;
}
return this;
}
/**
* Sets a new progress value. Passing true in animate will cause an animated progress update.
*
* @param progress New progress float value to set.
* @param animate Animation boolean value to set whether to animate progress change or not.
* @throws IllegalArgumentException if the given progress is negative, or, less or equal to 100.
*/
public void setProgress(@FloatRange(from = 0f, to = 100f) float progress, boolean animate) {
if (progress < 0 || progress > 100) {
throw new IllegalArgumentException("Progress value must be positive and less or equal to 100.");
}
renderer.setProgress(progress, animate);
}
/**
* Sets the background bar thickness in pixels. Works only if chart mode is set to ring.
*
* @param thickness non-negative thickness value in pixels.
* @throws IllegalArgumentException if the given value is negative, or, background bar thickness is not supported by the current used chart mode.
*/
public PercentageChartView backgroundBarThickness(@FloatRange(from = 0) float thickness) {
if (thickness < 0) {
throw new IllegalArgumentException("Background bar thickness must be a positive value.");
}
try {
((RingModeRenderer) renderer).setBackgroundBarThickness(thickness);
} catch (ClassCastException e) {
throw new IllegalArgumentException("Background bar thickness is not support by the used percentage chart mode.");
}
return this;
}
/**
* Sets the progress bar thickness in pixels. Works only if chart mode is set to ring.
*
* @param thickness non-negative thickness value in pixels.
* @throws IllegalArgumentException if the given value is negative, or, progress bar thickness is not supported by the current used chart mode.
*/
public PercentageChartView progressBarThickness(@FloatRange(from = 0) float thickness) {
if (thickness < 0) {
throw new IllegalArgumentException("Progress bar thickness must be a positive value.");
}
try {
((RingModeRenderer) renderer).setProgressBarThickness(thickness);
} catch (ClassCastException e) {
throw new IllegalArgumentException("Progress bar thickness is not support by the used percentage chart mode.");
}
return this;
}
/**
* 设置 View 透明度
* @param view View
* @param alpha 透明度
* @return {@link View}
*/
public static View setAlpha(final View view, @FloatRange(from = 0.0, to = 1.0) final float alpha) {
if (view != null) {
view.setAlpha(alpha);
}
return view;
}
/**
* 为活动中的Fragment的设置沉浸式状态栏
*/
public static void immersiveInFragments(Activity activity, 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) {
activity.getWindow().setStatusBarColor(color);
activity.getWindow().getDecorView()
.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
} else{
activity.getWindow()
.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
setTranslucentView((ViewGroup)activity.getWindow().getDecorView(), color, alpha);
}
}
public MenuItemBuilder pausedProgress(@FloatRange(from = 0, to = 1) float progress) {
if (progress <= 0) progress = 0;
else if (progress >= 1) progress = 1;
menuItem.lottieProgress = progress;
return this;
}
/**
* <br>
* Sets the audio volume to be used during the playback of the video associated with this {@link AdapsterPlayableItemViewHolder}.
* <br>
* If the playback is the active state, then the volume will be adjusted directly on the corresponding {@link Player} instance.
*
* @param audioVolume the exact audio volume (a value between <strong>0.0</strong> and <strong>1.0</strong>).
*/
protected final void setVolume(@FloatRange(from = 0.0, to = 1.0) float audioVolume) {
// creating/updating the corresponding Playback Info
final PlaybackInfo playbackInfo = getPlaybackInfo();
playbackInfo.getVolumeInfo().setVolume(audioVolume);
setPlaybackInfo(playbackInfo);
// updating the Player-related state (if necessary)
final Player player = getPlayer();
if(player != null) {
player.getVolumeController().setVolume(audioVolume);
}
}
/**
* Retrieves the audio volume that's associated with the current instance of the {@link AdapsterPlayableItemViewHolder}.
*
* @return an audio volume ratio (a value between <strong>0.0</strong> and <strong>1.0</strong>).
*/
@FloatRange(from = 0.0, to = 1.0)
protected final float getVolume() {
final PlaybackInfo playbackInfo = getPlaybackInfo();
final Player player = getPlayer();
return ((player != null) ? player.getVolumeController().getVolume() : playbackInfo.getVolumeInfo().getVolume());
}
/**
* 导航栏颜色
*
* @param navigationBarColor the navigation bar color 导航栏颜色
* @param navigationBarColorTransform the navigation bar color transform 导航栏变色后的颜色
* @param navigationAlpha the navigation alpha 透明度
* @return the immersion bar
*/
public ImmersionBar navigationBarColorInt(@ColorInt int navigationBarColor,
@ColorInt int navigationBarColorTransform,
@FloatRange(from = 0f, to = 1f) float navigationAlpha) {
mBarParams.navigationBarColor = navigationBarColor;
mBarParams.navigationBarColorTransform = navigationBarColorTransform;
mBarParams.navigationBarAlpha = navigationAlpha;
mBarParams.navigationBarColorTemp = mBarParams.navigationBarColor;
return this;
}
/**
* <br>
* Sets the audio volume to be used during the playback of the video associated with this {@link PlayableItemViewHolder}.
* <br>
* If the playback is the active state, then the volume will be adjusted directly on the corresponding {@link Player} instance.
*
* @param audioVolume the exact audio volume (a value between <strong>0.0</strong> and <strong>1.0</strong>).
*/
protected final void setVolume(@FloatRange(from = 0.0, to = 1.0) float audioVolume) {
// creating/updating the corresponding Playback Info
final PlaybackInfo playbackInfo = getPlaybackInfo();
playbackInfo.getVolumeInfo().setVolume(audioVolume);
setPlaybackInfo(playbackInfo);
// updating the Player-related state (if necessary)
final Player player = getPlayer();
if(player != null) {
player.getVolumeController().setVolume(audioVolume);
}
}
/**
* 设置沉浸式状态栏
*/
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 void setBackgroundDimAmount(@FloatRange(from = 0, to = 1) float dimAmount) {
float alpha = 1 - dimAmount;
if (isShowing()) {
setActivityAlpha(alpha);
}
if (mPopupBackground == null && alpha != 1) {
mPopupBackground = new PopupBackground();
addOnShowListener(mPopupBackground);
addOnDismissListener(mPopupBackground);
}
if (mPopupBackground != null) {
mPopupBackground.setAlpha(alpha);
}
}
/**
* 设置背景遮盖层的透明度
*/
public B setBackgroundDimAmount(@FloatRange(from = 0, to = 1) float dimAmount) {
mBackgroundDimAmount = dimAmount;
if (isShowing()) {
mPopupWindow.setBackgroundDimAmount(dimAmount);
}
return (B) this;
}
/**
* 设置背景遮盖层的透明度(前提条件是背景遮盖层开关必须是为开启状态)
*/
public void setBackgroundDimAmount(@FloatRange(from = 0, to = 1) float dimAmount) {
Window window = getWindow();
if (window != null) {
window.setDimAmount(dimAmount);
}
}