Java Code Examples for android.graphics.Color#valueOf()

The following examples show how to use android.graphics.Color#valueOf() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WallpaperColors.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
public WallpaperColors(Parcel parcel) {
    mMainColors = new ArrayList<>();
    final int count = parcel.readInt();
    for (int i = 0; i < count; i++) {
        final int colorInt = parcel.readInt();
        Color color = Color.valueOf(colorInt);
        mMainColors.add(color);
    }
    mColorHints = parcel.readInt();
}
 
Example 2
Source File: WallpaperActivity.java    From Unity-Android-Live-Wallpaper with MIT License 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.O_MR1)
@Override public WallpaperColors onComputeColors ()
{
    Color color = Color.valueOf(Color.BLACK);
    return new WallpaperColors(color, color, color);
}
 
Example 3
Source File: WallpaperManagerService.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private void parseWallpaperAttributes(XmlPullParser parser, WallpaperData wallpaper,
        boolean keepDimensionHints) {
    final String idString = parser.getAttributeValue(null, "id");
    if (idString != null) {
        final int id = wallpaper.wallpaperId = Integer.parseInt(idString);
        if (id > mWallpaperId) {
            mWallpaperId = id;
        }
    } else {
        wallpaper.wallpaperId = makeWallpaperIdLocked();
    }

    if (!keepDimensionHints) {
        wallpaper.width = Integer.parseInt(parser.getAttributeValue(null, "width"));
        wallpaper.height = Integer.parseInt(parser
                .getAttributeValue(null, "height"));
    }
    wallpaper.cropHint.left = getAttributeInt(parser, "cropLeft", 0);
    wallpaper.cropHint.top = getAttributeInt(parser, "cropTop", 0);
    wallpaper.cropHint.right = getAttributeInt(parser, "cropRight", 0);
    wallpaper.cropHint.bottom = getAttributeInt(parser, "cropBottom", 0);
    wallpaper.padding.left = getAttributeInt(parser, "paddingLeft", 0);
    wallpaper.padding.top = getAttributeInt(parser, "paddingTop", 0);
    wallpaper.padding.right = getAttributeInt(parser, "paddingRight", 0);
    wallpaper.padding.bottom = getAttributeInt(parser, "paddingBottom", 0);
    int colorsCount = getAttributeInt(parser, "colorsCount", 0);
    if (colorsCount > 0) {
        Color primary = null, secondary = null, tertiary = null;
        for (int i = 0; i < colorsCount; i++) {
            Color color = Color.valueOf(getAttributeInt(parser, "colorValue" + i, 0));
            if (i == 0) {
                primary = color;
            } else if (i == 1) {
                secondary = color;
            } else if (i == 2) {
                tertiary = color;
            } else {
                break;
            }
        }
        int colorHints = getAttributeInt(parser, "colorHints", 0);
        wallpaper.primaryColors = new WallpaperColors(primary, secondary, tertiary, colorHints);
    }
    wallpaper.name = parser.getAttributeValue(null, "name");
    wallpaper.allowBackup = "true".equals(parser.getAttributeValue(null, "backup"));
}
 
Example 4
Source File: WallpaperColors.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Constructs {@link WallpaperColors} from a bitmap.
 * <p>
 * Main colors will be extracted from the bitmap.
 *
 * @param bitmap Source where to extract from.
 */
public static WallpaperColors fromBitmap(@NonNull Bitmap bitmap) {
    if (bitmap == null) {
        throw new IllegalArgumentException("Bitmap can't be null");
    }

    final int bitmapArea = bitmap.getWidth() * bitmap.getHeight();
    boolean shouldRecycle = false;
    if (bitmapArea > MAX_WALLPAPER_EXTRACTION_AREA) {
        shouldRecycle = true;
        Size optimalSize = calculateOptimalSize(bitmap.getWidth(), bitmap.getHeight());
        bitmap = Bitmap.createScaledBitmap(bitmap, optimalSize.getWidth(),
                optimalSize.getHeight(), true /* filter */);
    }

    final Palette palette = Palette
            .from(bitmap)
            .setQuantizer(new VariationalKMeansQuantizer())
            .maximumColorCount(5)
            .clearFilters()
            .resizeBitmapArea(MAX_WALLPAPER_EXTRACTION_AREA)
            .generate();

    // Remove insignificant colors and sort swatches by population
    final ArrayList<Palette.Swatch> swatches = new ArrayList<>(palette.getSwatches());
    final float minColorArea = bitmap.getWidth() * bitmap.getHeight() * MIN_COLOR_OCCURRENCE;
    swatches.removeIf(s -> s.getPopulation() < minColorArea);
    swatches.sort((a, b) -> b.getPopulation() - a.getPopulation());

    final int swatchesSize = swatches.size();
    Color primary = null, secondary = null, tertiary = null;

    swatchLoop:
    for (int i = 0; i < swatchesSize; i++) {
        Color color = Color.valueOf(swatches.get(i).getRgb());
        switch (i) {
            case 0:
                primary = color;
                break;
            case 1:
                secondary = color;
                break;
            case 2:
                tertiary = color;
                break;
            default:
                // out of bounds
                break swatchLoop;
        }
    }

    int hints = calculateDarkHints(bitmap);

    if (shouldRecycle) {
        bitmap.recycle();
    }

    return new WallpaperColors(primary, secondary, tertiary, HINT_FROM_BITMAP | hints);
}