1.问题原因分析
在Android SDK更新至23以上时,我们会发现之前在某些地方因计算需要使用到的FloatMath类中的方法如FloatMath.ceil()
与FloatMath.sin()
等都报错并提示不可用了,FloatMath源码如下:
package android.util;
/**
* Math routines similar to those found in {@link java.lang.Math}.
* 数学例程类似于{@link java.lang.Math}.
* <p>Historically these methods were faster than the equivalent double-based
* {@link java.lang.Math} methods. On versions of Android with a JIT they
* became slower and have since been re-implemented to wrap calls to
* {@link java.lang.Math}. {@link java.lang.Math} should be used in
* preference.
*
* <p>All methods were removed from the public API in version 23.
* 所有方法都从版本23的公共API中删除.
* @deprecated Use {@link java.lang.Math} instead.
* 方法过时,应使用java.lang.Math代替.
*/
@Deprecated
public class FloatMath {
/** Prevents instantiation. */
private FloatMath() {}
/**
* Returns the float conversion of the most positive (i.e. closest to
* positive infinity) integer value which is less than the argument.
*
* @param value to be converted
* @return the floor of value
* @removed
*/
public static float floor(float value) {
return (float) Math.floor(value);
}
/**
* Returns the float conversion of the most negative (i.e. closest to
* negative infinity) integer value which is greater than the argument.
*
* @param value to be converted
* @return the ceiling of value
* @removed
*/
public static float ceil(float value) {
return (float) Math.ceil(value);
}
/**
* Returns the closest float approximation of the sine of the argument.
*
* @param angle to compute the cosine of, in radians
* @return the sine of angle
* @removed
*/
public static float sin(float angle) {
return (float) Math.sin(angle);
}
/**
* Returns the closest float approximation of the cosine of the argument.
*
* @param angle to compute the cosine of, in radians
* @return the cosine of angle
* @removed
*/
public static float cos(float angle) {
return (float) Math.cos(angle);
}
/**
* Returns the closest float approximation of the square root of the
* argument.
*
* @param value to compute sqrt of
* @return the square root of value
* @removed
*/
public static float sqrt(float value) {
return (float) Math.sqrt(value);
}
/**
* Returns the closest float approximation of the raising "e" to the power
* of the argument.
*
* @param value to compute the exponential of
* @return the exponential of value
* @removed
*/
public static float exp(float value) {
return (float) Math.exp(value);
}
/**
* Returns the closest float approximation of the result of raising {@code
* x} to the power of {@code y}.
*
* @param x the base of the operation.
* @param y the exponent of the operation.
* @return {@code x} to the power of {@code y}.
* @removed
*/
public static float pow(float x, float y) {
return (float) Math.pow(x, y);
}
/**
* Returns {@code sqrt(}<i>{@code x}</i><sup>{@code 2}</sup>{@code +} <i>
* {@code y}</i><sup>{@code 2}</sup>{@code )}.
*
* @param x a float number
* @param y a float number
* @return the hypotenuse
* @removed
*/
public static float hypot(float x, float y) {
return (float) Math.hypot(x, y);
}
}
通过观察类说明可知,FloatMath的所有方法都已经从版本23的公共API中删除,官方提示我们可以使用java.lang.Math类来代替使用。
2.解决方法
- 使用23以下版本的SDK进行编译,即修改gradle.build文件里的compileSdkVersion为23以下版本即可。
- 使用Math类中的方法进行代替。
网友评论