Android 技巧之巧用Range注解(IntRange,FloatRange) 和自定义Range

5 篇文章 0 订阅
2 篇文章 0 订阅
在Android开发中,自定义公共方法或工具类时,使用Range注解可以确保参数值在预期范围内,避免错误。例如在弹框背景透明度设置中,通过@FloatRange限制0.0f到1.0f之间的值,防止超出范围导致的问题。本文介绍了Range的使用方法和自定义Range注解的步骤,强调了其在代码健壮性上的作用。
摘要由CSDN通过智能技术生成

 

在我们Android开发应用程式时,会经常写一些自定义的公共方法或者工具类

比如弹框的时候,将Activity背景设置暗一些,弹框消失,恢复到原状,比如自定义View时,Color的区间取值

示例Code:

 fun alpUpdate(context: Activity,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)
    }

在这个方法中,bgAlpha 是一个float的参数,区间在0.0f - 1.0f。ps:为什么后面加f呢,这涉及到基本类型的默认类型,浮点型默认为double

如果将这个方法写成一个工具类或者在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 (C) 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-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package androidx.annotation;

import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.CLASS;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

/**
 * Denotes that the annotated element should be a float or double in the given range
 * <p>
 * Example:
 * <pre><code>
 *  &#64;FloatRange(from=0.0,to=1.0)
 *  public float getAlpha() {
 *      ...
 *  }
 * </code></pre>
 */
@Retention(CLASS)
@Target({METHOD,PARAMETER,FIELD,LOCAL_VARIABLE,ANNOTATION_TYPE})
public @interface FloatRange {
    /** Smallest value. Whether it is inclusive or not is determined
     * by {@link #fromInclusive} */
    double from() default Double.NEGATIVE_INFINITY;
    /** Largest value. Whether it is inclusive or not is determined
     * by {@link #toInclusive} */
    double to() default Double.POSITIVE_INFINITY;

    /** Whether the from value is included in the range */
    boolean fromInclusive() default true;

    /** Whether the to value is included in the range */
    boolean toInclusive() default true;
}

可见它是一个注解,可用于方法/参数/字段/局部变量

from 也就是最小值,to是最大值,这两个配合使用就是控制的一个范围

如果只是from 那么就表示最小的值从x 起

Code:

fun floatRange1(@FloatRange(from = 1.0) params : Float){
        
    }

如果只写to 那么就表示当前允许传入的最大值为x

Code:

 fun floatRange1(@FloatRange(to = 1.0) params : Float){

    }

IntRange 同理

 

如何自定义Range?

这个需要我们要有一些自定义注解的基本知识(后续会写如何自定义注解)

Retention 类型  常用的 RunTime/CLASS/SOURCE

target 作用域

这里我们自定义注解需要使用CLSS

@Retention(RetentionPolicy.CLASS)
@Target({ElementType.METHOD,ElementType.ANNOTATION_TYPE,ElementType.FIELD,ElementType.LOCAL_VARIABLE,ElementType.PARAMETER})

完整代码:

package com.luxiaofeng.utils.range;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @author lxf
 * @description
 * @date 2021/3/31.
 */

@Retention(RetentionPolicy.CLASS)
@Target({ElementType.METHOD,ElementType.ANNOTATION_TYPE,ElementType.FIELD,ElementType.LOCAL_VARIABLE,ElementType.PARAMETER})
public @interface DoubleRange {
    //min value is double min_value
    double form() default Double.MIN_VALUE;
    //max value default is double.max
    double to()   default Double.MAX_VALUE;
    
}

除此之外 我们还可以定义Byte  Short 以及其他类型的Range 比如枚举 Color等,也可在自定义View中使用

如何使用?

  fun doubleRange1(@DoubleRange(form = 1.0) params : Float){

    }

    fun doubleRange2(@DoubleRange(to = 1.0) params : Float){

    }

    fun doubleRange3(@DoubleRange(form = 0.0,to = 1.0) params : Float){

    }

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值