Android Palette

Palette是什么?

Palette是Google提供的一个类库,它能让你从图像中提取突出的颜色。这个类能提取以下几种颜色:

  • Vibrant (有活力的)
  • Vibrant dark(有活力的 暗色)
  • Vibrant light(有活力的 亮色)
  • Muted (柔和的)
  • Muted dark(柔和的 暗色)
  • Muted light(柔和的 亮色)

使用方式:

  • 引入依赖
implementation 'com.android.support:palette-v7:28.0.0'
     **
     * 根据Url获取Bitmap
     * img_url 图片的网址
     */
    public void initNetWorkImage(final String imgUrl, final Context context) {

        new AsyncTask<Void, Void, Bitmap>() {
            @Override
            protected Bitmap doInBackground(Void... params) {
                Bitmap bitmap = null;
                try {
                    bitmap = Glide.with(context)
                            .asBitmap()
                            .load(imgUrl)
                            //360*480,原始大小设置为Target.SIZE_ORIGINAL
                            .submit(360, 480).get();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return bitmap;
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                Palette.Builder builder = Palette.from(bitmap);
                //异步任务---可能分析的图片会比较大或者颜色分布比较复杂,会耗时比较久,防止卡死主线程。
                builder.generate(new Palette.PaletteAsyncListener() {
                    @Override
                    public void onGenerated(Palette palette) {
                        //暗、柔和的颜色
                        int darkMutedColor = palette.getDarkMutedColor(mThemeDefaultColor);//如果分析不出来,则返回默认颜色
                        //暗、柔和
                        int lightMutedColor = palette.getLightMutedColor(mThemeDefaultColor);
                        //暗、鲜艳
                        int darkVibrantColor = palette.getDarkVibrantColor(mThemeDefaultColor);
                        //亮、鲜艳
                        int lightVibrantColor = palette.getLightVibrantColor(mThemeDefaultColor);
                        //柔和
                        int mutedColor = palette.getMutedColor(mThemeDefaultColor);
                        //柔和
                        int vibrantColor = palette.getVibrantColor(mThemeDefaultColor);
                        //获取主颜色
                        int dominantColor = palette.getDominantColor(mThemeDefaultColor);
                        //获取某种特性颜色的样品
                        Palette.Swatch lightVibrantSwatch = palette.getVibrantSwatch();
                        //谷歌推荐的:图片的整体的颜色rgb的混合值---主色调
                        int rgb = lightVibrantSwatch.getRgb();
                        //谷歌推荐:图片中间的文字颜色
                        int bodyTextColor = lightVibrantSwatch.getBodyTextColor();
                        //谷歌推荐:作为标题的颜色(有一定的和图片的对比度的颜色值)
                        int titleTextColor = lightVibrantSwatch.getTitleTextColor();
                        //颜色向量
                        float[] hsl = lightVibrantSwatch.getHsl();
                        //分析该颜色在图片中所占的像素多少值
                        int population = lightVibrantSwatch.getPopulation();
                    }
                });
            }
        }.execute();

扩展方法
    /**
     * 当前颜色是否接近黑色
     *
     * @param rgb 颜色值
     * @return
     */
    public boolean isLightColor(int rgb) {
        int blue = Color.blue(rgb);
        int green = Color.green(rgb);
        int red = Color.red(rgb);

        double luma = red * 0.299 + green * 0.587 + blue * 0.114; // per ITU-R BT.709

        if (luma < 50) {
            // pick a different colour
            Log.i("isLightColor", "深色,当前的色值总合为:" + luma + ",Rgb颜色值为:" + rgb);
            return true;
        }
        return false;
    }

    /**
     * 给当前颜色值设置透明度
     *
     * @param percent 透明值
     * @param rgb     rgb
     * @return
     */
    protected int getTranslucentColor(float percent, int rgb) {
        int blue = Color.blue(rgb);
        int green = Color.green(rgb);
        int red = Color.red(rgb);
        int alpha = Color.alpha(rgb);
        alpha = Math.round(alpha * percent);
        return Color.argb(alpha, red, green, blue);
    }

    /**
     * 颜色加深算法
     */
    private int setColorBurn(int rgb, float val) {
        int r = (rgb >> 16) & 0xff;
        int g = (rgb >> 8) & 0xff;
        int b = rgb & 0xff;
        r = (int) Math.floor(r * (1f + val));
        g = (int) Math.floor(g * (1f + val));
        b = (int) Math.floor(b * (1f + val));
        return Color.rgb(r, g, b);
    }

    /**
     * 颜色浅化算法
     */
    private int setColorShallow(int rgb, float val) {
        int r = (rgb >> 16) & 0xff;
        int g = (rgb >> 8) & 0xff;
        int b = rgb & 0xff;
        r = (int) Math.floor(r * (1f - val));
        g = (int) Math.floor(g * (1f - val));
        b = (int) Math.floor(b * (1f - val));
        return Color.rgb(r, g, b);
    }

 

发布了213 篇原创文章 · 获赞 12 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/a1003434346/article/details/102950069