下面列出了java.lang.Math#round ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private BufferedImage produceRoundedCornerIcon(BufferedImage icon) {
int imageWidth = icon.getWidth();
// Ratio of icon size to png image size for roundRect icon is 0.93
double iconWidth = imageWidth * 0.93;
// Round iconWidth value to even int for a centered png
int intIconWidth = ((int)Math.round(iconWidth / 2) * 2);
Image tmp = icon.getScaledInstance(intIconWidth, intIconWidth, Image.SCALE_SMOOTH);
int marginWidth = ((imageWidth - intIconWidth) / 2);
// Corner radius of roundedCornerIcon needs to be 1/12 of width according to Android material guidelines
float cornerRadius = intIconWidth / 12;
BufferedImage roundedCornerIcon = new BufferedImage(imageWidth, imageWidth, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = roundedCornerIcon.createGraphics();
g2.setClip(new RoundRectangle2D.Float(marginWidth, marginWidth, intIconWidth, intIconWidth, cornerRadius, cornerRadius));
g2.drawImage(tmp, marginWidth, marginWidth, null);
return roundedCornerIcon;
}
void _doStateChangeV1(boolean force) {
// compile our state value
short s = _buttonStateV1; // buttons
s |= (_dPadStateH > 0 ? 1 : 0) << 5; // sign bit
s |= ((int) (Math.round(Math.min(1.0, Math.abs(_dPadStateH)) * 15.0))) <<
6; // mag
s |= (_dPadStateV > 0 ? 1 : 0) << 10; // sign bit
s |= ((int) (Math.round(Math.min(1.0, Math.abs(_dPadStateV)) * 15.0))) <<
11; // mag
// if our compiled state value hasn't changed, don't send.
// (analog joystick noise can send a bunch of redundant states through here)
// The exception is if forced is true, which is the case with packets that
// double as keepalives.
if ((s == _lastSentState) && (!force)) {
return;
}
_stateBirthTimes[_nextState] = SystemClock.uptimeMillis();
_stateLastSentTimes[_nextState] = 0;
if (debug) {
Log.v(TAG, "STORING NEXT STATE: " + _nextState);
}
_statesV1[_nextState] = s;
_nextState = (_nextState + 1) % 256;
_lastSentState = s;
// if we're pretty up to date as far as state acks, lets go ahead
// and send out this state immediately..
// (keeps us nice and responsive on low latency networks)
int unackedCount = (_nextState - _requestedState) & 0xFF; // upcast to
// get
// unsigned
if (unackedCount < 3) {
_shipUnAckedStatesV1();
}
}
private BufferedImage produceRoundIcon(BufferedImage icon) {
int imageWidth = icon.getWidth();
// Ratio of icon size to png image size for round icon is 0.80
double iconWidth = imageWidth * 0.80;
// Round iconWidth value to even int for a centered png
int intIconWidth = ((int)Math.round(iconWidth / 2) * 2);
Image tmp = icon.getScaledInstance(intIconWidth, intIconWidth, Image.SCALE_SMOOTH);
int marginWidth = ((imageWidth - intIconWidth) / 2);
BufferedImage roundIcon = new BufferedImage(imageWidth, imageWidth, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = roundIcon.createGraphics();
g2.setClip(new Ellipse2D.Float(marginWidth, marginWidth, intIconWidth, intIconWidth));
g2.drawImage(tmp, marginWidth, marginWidth, null);
return roundIcon;
}
private BufferedImage produceForegroundImageIcon(BufferedImage icon) {
int imageWidth = icon.getWidth();
// According to the adaptive icon documentation, both layers are 108x108dp but only the inner
// 72x72dp appears in the masked viewport, so we shrink down the size of the image accordingly.
double iconWidth = imageWidth * 72.0 / 108.0;
// Round iconWidth value to even int for a centered png
int intIconWidth = ((int)Math.round(iconWidth / 2) * 2);
Image tmp = icon.getScaledInstance(intIconWidth, intIconWidth, Image.SCALE_SMOOTH);
int marginWidth = ((imageWidth - intIconWidth) / 2);
BufferedImage foregroundImageIcon = new BufferedImage(imageWidth, imageWidth, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = foregroundImageIcon.createGraphics();
g2.drawImage(tmp, marginWidth, marginWidth, null);
return foregroundImageIcon;
}
private int ceil(double v) {
int valor;
valor = (int) Math.round(v);
if ((double) valor < v) {
valor++;
}
return (valor);
}
private int ceil(double v) {
int valor;
valor = (int) Math.round(v);
if ((double) valor < v) {
valor++;
}
return (valor);
}
private int ceil (double v) {
int valor;
valor = (int) Math.round(v);
if ((double)valor < v) valor++;
return (valor);
}
private int ceil (double v) {
int valor;
valor = (int) Math.round(v);
if ((double)valor < v) valor++;
return (valor);
}
private int ceil (double v) {
int valor;
valor = (int) Math.round(v);
if ((double)valor < v) valor++;
return (valor);
}
private int ceil(double v) {
int valor;
valor = (int) Math.round(v);
if ( (double) valor < v) {
valor++;
}
return (valor);
}
public static int convertDpToPixel(Context ctx, int dp) {
float density = ctx.getResources().getDisplayMetrics().density;
return Math.round((float) dp * density);
}
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
selectTimedMetadataTrack(mp);
mVideoBufferedDuration = (int) Math.round((double) (mVideoDuration * percent) / 100.0);
}
/**
* Rounds a double to the given number of decimal places.
*
* @param value the double value
* @param afterDecimalPoint the number of digits after the decimal point
* @return the double rounded to the given precision
*/
public static double roundDouble(double value, int afterDecimalPoint) {
double mask = Math.pow(10.0, (double) afterDecimalPoint);
return (double) (Math.round(value * mask)) / mask;
}
/**
* Rounds a double to the given number of decimal places.
*
* @param value the double value
* @param afterDecimalPoint the number of digits after the decimal point
* @return the double rounded to the given precision
*/
public static /*@[email protected]*/ double roundDouble(double value,int afterDecimalPoint) {
double mask = Math.pow(10.0, (double)afterDecimalPoint);
return (double)(Math.round(value * mask)) / mask;
}
/**
* Rounds a double to the given number of decimal places.
*
* @param value the double value
* @param afterDecimalPoint the number of digits after the decimal point
* @return the double rounded to the given precision
*/
public static /*@[email protected]*/ double roundDouble(double value,int afterDecimalPoint) {
double mask = Math.pow(10.0, (double)afterDecimalPoint);
return (double)(Math.round(value * mask)) / mask;
}
/**
* Rounds a double to the given number of decimal places.
*
* @param value the double value
* @param afterDecimalPoint the number of digits after the decimal point
* @return the double rounded to the given precision
*/
public static /*@[email protected]*/ double roundDouble(double value,int afterDecimalPoint) {
double mask = Math.pow(10.0, (double)afterDecimalPoint);
return (double)(Math.round(value * mask)) / mask;
}
public static MR_int round ( MR_float x ) { return new MR_int((int)Math.round(x.get())); }
public static MR_long round ( MR_double x ) { return new MR_long(Math.round(x.get())); }