use of android.annotation.NonNull in project platform_frameworks_base by android.
the class ImageUtils method scale.
/**
* Resize the given image
*
* @param source the image to be scaled
* @param xScale x scale
* @param yScale y scale
* @return the scaled image
*/
@NonNull
public static BufferedImage scale(@NonNull BufferedImage source, double xScale, double yScale) {
int sourceWidth = source.getWidth();
int sourceHeight = source.getHeight();
int destWidth = Math.max(1, (int) (xScale * sourceWidth));
int destHeight = Math.max(1, (int) (yScale * sourceHeight));
int imageType = source.getType();
if (imageType == BufferedImage.TYPE_CUSTOM) {
imageType = BufferedImage.TYPE_INT_ARGB;
}
if (xScale > 0.5 && yScale > 0.5) {
BufferedImage scaled = new BufferedImage(destWidth, destHeight, imageType);
Graphics2D g2 = scaled.createGraphics();
g2.setComposite(AlphaComposite.Src);
g2.setColor(new Color(0, true));
g2.fillRect(0, 0, destWidth, destHeight);
if (xScale == 1 && yScale == 1) {
g2.drawImage(source, 0, 0, null);
} else {
setRenderingHints(g2);
g2.drawImage(source, 0, 0, destWidth, destHeight, 0, 0, sourceWidth, sourceHeight, null);
}
g2.dispose();
return scaled;
} else {
// When creating a thumbnail, using the above code doesn't work very well;
// you get some visible artifacts, especially for text. Instead use the
// technique of repeatedly scaling the image into half; this will cause
// proper averaging of neighboring pixels, and will typically (for the kinds
// of screen sizes used by this utility method in the layout editor) take
// about 3-4 iterations to get the result since we are logarithmically reducing
// the size. Besides, each successive pass in operating on much fewer pixels
// (a reduction of 4 in each pass).
//
// However, we may not be resizing to a size that can be reached exactly by
// successively diving in half. Therefore, once we're within a factor of 2 of
// the final size, we can do a resize to the exact target size.
// However, we can get even better results if we perform this final resize
// up front. Let's say we're going from width 1000 to a destination width of 85.
// The first approach would cause a resize from 1000 to 500 to 250 to 125, and
// then a resize from 125 to 85. That last resize can distort/blur a lot.
// Instead, we can start with the destination width, 85, and double it
// successfully until we're close to the initial size: 85, then 170,
// then 340, and finally 680. (The next one, 1360, is larger than 1000).
// So, now we *start* the thumbnail operation by resizing from width 1000 to
// width 680, which will preserve a lot of visual details such as text.
// Then we can successively resize the image in half, 680 to 340 to 170 to 85.
// We end up with the expected final size, but we've been doing an exact
// divide-in-half resizing operation at the end so there is less distortion.
// Number of halving operations to perform after the initial resize
int iterations = 0;
// Width closest to source width that = 2^x, x is integer
int nearestWidth = destWidth;
int nearestHeight = destHeight;
while (nearestWidth < sourceWidth / 2) {
nearestWidth *= 2;
nearestHeight *= 2;
iterations++;
}
BufferedImage scaled = new BufferedImage(nearestWidth, nearestHeight, imageType);
Graphics2D g2 = scaled.createGraphics();
setRenderingHints(g2);
g2.drawImage(source, 0, 0, nearestWidth, nearestHeight, 0, 0, sourceWidth, sourceHeight, null);
g2.dispose();
sourceWidth = nearestWidth;
sourceHeight = nearestHeight;
source = scaled;
for (int iteration = iterations - 1; iteration >= 0; iteration--) {
int halfWidth = sourceWidth / 2;
int halfHeight = sourceHeight / 2;
scaled = new BufferedImage(halfWidth, halfHeight, imageType);
g2 = scaled.createGraphics();
setRenderingHints(g2);
g2.drawImage(source, 0, 0, halfWidth, halfHeight, 0, 0, sourceWidth, sourceHeight, null);
g2.dispose();
sourceWidth = halfWidth;
sourceHeight = halfHeight;
source = scaled;
iterations--;
}
return scaled;
}
}
use of android.annotation.NonNull in project platform_frameworks_base by android.
the class ParserFactory method create.
@NonNull
private static XmlPullParser create(@NonNull InputStream stream, @Nullable String name, long size, boolean isLayout) throws XmlPullParserException {
XmlPullParser parser = instantiateParser(name);
stream = readAndClose(stream, name, size);
parser.setInput(stream, ENCODING);
if (isLayout) {
try {
return new LayoutParserWrapper(parser).peekTillLayoutStart();
} catch (IOException e) {
throw new XmlPullParserException(null, parser, e);
}
}
return parser;
}
use of android.annotation.NonNull in project platform_frameworks_base by android.
the class ParserFactory method readAndClose.
@NonNull
private static InputStream readAndClose(@NonNull InputStream stream, @Nullable String name, long size) throws XmlPullParserException {
// just a sanity check. It's doubtful we'll have such big files!
if (size > Integer.MAX_VALUE) {
throw new XmlPullParserException("File " + name + " is too big to be parsed");
}
int intSize = (int) size;
// create a buffered reader to facilitate reading.
BufferedInputStream bufferedStream = new BufferedInputStream(stream);
try {
int avail;
if (intSize != -1) {
avail = intSize;
} else {
// get the size to read.
avail = bufferedStream.available();
}
// create the initial buffer and read it.
byte[] buffer = new byte[avail];
int read = stream.read(buffer);
// this is the easy case.
if (read == intSize) {
return new ByteArrayInputStream(buffer);
}
// available() returned!)
while ((avail = bufferedStream.available()) > 0) {
if (read + avail > buffer.length) {
// just allocate what is needed. We're mostly reading small files
// so it shouldn't be too problematic.
byte[] moreBuffer = new byte[read + avail];
System.arraycopy(buffer, 0, moreBuffer, 0, read);
buffer = moreBuffer;
}
read += stream.read(buffer, read, avail);
}
// return a new stream encapsulating this buffer.
return new ByteArrayInputStream(buffer);
} catch (IOException e) {
throw new XmlPullParserException("Failed to read " + name, null, e);
} finally {
try {
bufferedStream.close();
} catch (IOException ignored) {
}
}
}
use of android.annotation.NonNull in project platform_frameworks_base by android.
the class Layout method createContentFrame.
@NonNull
private FrameLayout createContentFrame() {
FrameLayout contentRoot = new FrameLayout(getContext());
LayoutParams params = createLayoutParams(MATCH_PARENT, MATCH_PARENT);
int rule = mBuilder.isNavBarVertical() ? START_OF : ABOVE;
if (mBuilder.hasNavBar() && mBuilder.solidBars()) {
params.addRule(rule, getId(ID_NAV_BAR));
}
int below = -1;
if (mBuilder.mActionBarSize <= 0 && mBuilder.mTitleBarSize > 0) {
below = getId(ID_TITLE_BAR);
} else if (mBuilder.hasStatusBar() && mBuilder.solidBars()) {
below = getId(ID_STATUS_BAR);
}
if (below != -1) {
params.addRule(BELOW, below);
}
contentRoot.setLayoutParams(params);
return contentRoot;
}
use of android.annotation.NonNull in project platform_frameworks_base by android.
the class Layout method createNavBar.
/**
* @param isRtl whether the current locale is an RTL locale.
* @param isRtlSupported whether the applications supports RTL (i.e. has supportsRtl=true
* in the manifest and targetSdkVersion >= 17.
*/
@NonNull
private NavigationBar createNavBar(BridgeContext context, Density density, boolean isRtl, boolean isRtlSupported, int simulatedPlatformVersion) {
int orientation = mBuilder.mNavBarOrientation;
int size = mBuilder.mNavBarSize;
NavigationBar navBar = new NavigationBar(context, density, orientation, isRtl, isRtlSupported, simulatedPlatformVersion);
boolean isVertical = mBuilder.isNavBarVertical();
int w = isVertical ? size : MATCH_PARENT;
int h = isVertical ? MATCH_PARENT : size;
LayoutParams params = createLayoutParams(w, h);
params.addRule(isVertical ? ALIGN_PARENT_END : ALIGN_PARENT_BOTTOM);
navBar.setLayoutParams(params);
navBar.setId(getId(ID_NAV_BAR));
return navBar;
}
Aggregations