Search in sources :

Example 41 with NonNull

use of android.annotation.NonNull in project android_frameworks_base by DirtyUnicorns.

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;
    }
}
Also used : Color(java.awt.Color) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) NonNull(android.annotation.NonNull)

Example 42 with NonNull

use of android.annotation.NonNull in project android_frameworks_base by DirtyUnicorns.

the class Layout method createStatusBar.

/**
     * @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 StatusBar createStatusBar(BridgeContext context, Density density, boolean isRtl, boolean isRtlSupported, int simulatedPlatformVersion) {
    StatusBar statusBar = new StatusBar(context, density, isRtl, isRtlSupported, simulatedPlatformVersion);
    LayoutParams params = createLayoutParams(MATCH_PARENT, mBuilder.mStatusBarSize);
    if (mBuilder.isNavBarVertical()) {
        params.addRule(START_OF, getId(ID_NAV_BAR));
    }
    statusBar.setLayoutParams(params);
    statusBar.setId(getId(ID_STATUS_BAR));
    return statusBar;
}
Also used : StatusBar(com.android.layoutlib.bridge.bars.StatusBar) NonNull(android.annotation.NonNull)

Example 43 with NonNull

use of android.annotation.NonNull in project android_frameworks_base by DirtyUnicorns.

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;
}
Also used : NavigationBar(com.android.layoutlib.bridge.bars.NavigationBar) NonNull(android.annotation.NonNull)

Example 44 with NonNull

use of android.annotation.NonNull in project android_frameworks_base by DirtyUnicorns.

the class Main method setUp.

/**
     * Initialize the bridge and the resource maps.
     */
@BeforeClass
public static void setUp() {
    File data_dir = new File(PLATFORM_DIR, "data");
    File res = new File(data_dir, "res");
    sFrameworkRepo = new FrameworkResources(new FolderWrapper(res));
    sFrameworkRepo.loadResources();
    sFrameworkRepo.loadPublicResources(getLogger());
    sProjectResources = new ResourceRepository(new FolderWrapper(TEST_RES_DIR + APP_TEST_RES), false) {

        @NonNull
        @Override
        protected ResourceItem createResourceItem(@NonNull String name) {
            return new ResourceItem(name);
        }
    };
    sProjectResources.loadResources();
    File fontLocation = new File(data_dir, "fonts");
    File buildProp = new File(PLATFORM_DIR, "build.prop");
    File attrs = new File(res, "values" + File.separator + "attrs.xml");
    sBridge = new Bridge();
    sBridge.init(ConfigGenerator.loadProperties(buildProp), fontLocation, ConfigGenerator.getEnumMap(attrs), getLayoutLog());
}
Also used : FrameworkResources(com.android.ide.common.resources.FrameworkResources) NonNull(android.annotation.NonNull) FolderWrapper(com.android.io.FolderWrapper) ResourceRepository(com.android.ide.common.resources.ResourceRepository) ResourceItem(com.android.ide.common.resources.ResourceItem) File(java.io.File) Bridge(com.android.layoutlib.bridge.Bridge) BeforeClass(org.junit.BeforeClass)

Example 45 with NonNull

use of android.annotation.NonNull in project android_frameworks_base by DirtyUnicorns.

the class StaticLayout_Delegate method computePrimitives.

/**
     * Compute metadata each character - things which help in deciding if it's possible to break
     * at a point or not.
     */
@NonNull
private static List<Primitive> computePrimitives(@NonNull char[] text, @NonNull float[] widths, int length, @NonNull List<Integer> breaks) {
    // Initialize the list with a guess of the number of primitives:
    // 2 Primitives per non-whitespace char and approx 5 chars per word (i.e. 83% chars)
    List<Primitive> primitives = new ArrayList<Primitive>(((int) Math.ceil(length * 1.833)));
    int breaksSize = breaks.size();
    int breakIndex = 0;
    for (int i = 0; i < length; i++) {
        char c = text[i];
        if (c == CHAR_SPACE || c == CHAR_ZWSP) {
            primitives.add(PrimitiveType.GLUE.getNewPrimitive(i, widths[i]));
        } else if (c == CHAR_TAB) {
            primitives.add(PrimitiveType.VARIABLE.getNewPrimitive(i));
        } else if (c != CHAR_NEWLINE) {
            while (breakIndex < breaksSize && breaks.get(breakIndex) < i) {
                breakIndex++;
            }
            Primitive p;
            if (widths[i] != 0) {
                if (breakIndex < breaksSize && breaks.get(breakIndex) == i) {
                    p = PrimitiveType.PENALTY.getNewPrimitive(i, 0, 0);
                } else {
                    p = PrimitiveType.WORD_BREAK.getNewPrimitive(i, 0);
                }
                primitives.add(p);
            }
            primitives.add(PrimitiveType.BOX.getNewPrimitive(i, widths[i]));
        }
    }
    // final break at end of everything
    primitives.add(PrimitiveType.PENALTY.getNewPrimitive(length, 0, -PrimitiveType.PENALTY_INFINITY));
    return primitives;
}
Also used : ArrayList(java.util.ArrayList) Paint(android.graphics.Paint) NonNull(android.annotation.NonNull)

Aggregations

NonNull (android.annotation.NonNull)322 ArrayList (java.util.ArrayList)46 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)45 IOException (java.io.IOException)35 ComponentName (android.content.ComponentName)25 File (java.io.File)22 XmlPullParser (org.xmlpull.v1.XmlPullParser)20 Intent (android.content.Intent)18 EphemeralResolveInfo (android.content.pm.EphemeralResolveInfo)16 ResolveInfo (android.content.pm.ResolveInfo)16 Bundle (android.os.Bundle)15 RemoteException (android.os.RemoteException)15 FileNotFoundException (java.io.FileNotFoundException)15 Paint (android.graphics.Paint)14 PackageParser (android.content.pm.PackageParser)12 ContentResolver (android.content.ContentResolver)10 UserInfo (android.content.pm.UserInfo)10 StorageManager (android.os.storage.StorageManager)10 VolumeInfo (android.os.storage.VolumeInfo)10 KeyCharacteristics (android.security.keymaster.KeyCharacteristics)10