android.graphics.Color#valueOf ( )源码实例Demo

下面列出了android.graphics.Color#valueOf ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: android_9.0.0_r45   文件: WallpaperColors.java
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();
}
 
@TargetApi(Build.VERSION_CODES.O_MR1)
@Override public WallpaperColors onComputeColors ()
{
    Color color = Color.valueOf(Color.BLACK);
    return new WallpaperColors(color, color, color);
}
 
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"));
}
 
源代码4 项目: android_9.0.0_r45   文件: WallpaperColors.java
/**
 * 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);
}