Java Code Examples for android.support.v7.graphics.Palette#from()

The following examples show how to use android.support.v7.graphics.Palette#from() . 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: EyepetizerDetailActivity.java    From Ency with Apache License 2.0 6 votes vote down vote up
/**
 * 使用Palette改变状态颜色
 */
private void initPalette() {
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg_video);
    Palette.Builder builder = Palette.from(bitmap);
    builder.generate();
    // 提取颜色
    builder.generate(new Palette.PaletteAsyncListener() {
        @Override
        public void onGenerated(@NonNull Palette palette) {
            // 柔和的颜色
            Palette.Swatch muted = palette.getMutedSwatch();
            if (Build.VERSION.SDK_INT >= 21) {
                Window window = getWindow();
                // 很明显,这两货是新API才有的。
                window.setStatusBarColor(ColorUtil.colorBurn(muted.getRgb()));
                window.setNavigationBarColor(ColorUtil.colorBurn(muted.getRgb()));
            }
        }
    });
}
 
Example 2
Source File: UserInfoActivity.java    From nono-android with GNU General Public License v3.0 5 votes vote down vote up
private int  autoColor(Bitmap bitmap) {
    Palette.Builder b = Palette.from(bitmap);
    Palette palette = b.generate();
    Palette.Swatch s0 = palette.getVibrantSwatch();
    if (s0 != null) {
        return s0.getRgb();
    }else {
        for(Palette.Swatch s:palette.getSwatches()){
            if(s!=null){
                return s.getRgb();
            }
        }
    }
    return 0;
}
 
Example 3
Source File: PaletteActivity.java    From MaterialDesignDemo with MIT License 4 votes vote down vote up
@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_palette);
        mIvPic = (ImageView) findViewById(R.id.ivPic);
        mTvBody = (TextView) findViewById(R.id.tvBody);
        mTvTitle = (TextView) findViewById(R.id.tvTitle);
        mTvDarkMutedColor = (TextView) findViewById(R.id.tvDarkMutedColor);
        mTvLightMutedColor = (TextView) findViewById(R.id.tvLightMutedColor);
        mTvDarkVibrantColor = (TextView) findViewById(R.id.tvDarkVibrantColor);
        mTvLightVibrantColor = (TextView) findViewById(R.id.tvLightVibrantColor);
        mTvMutedColor = (TextView) findViewById(R.id.tvMutedColor);
        mTvVibrantColor = (TextView) findViewById(R.id.tvVibrantColor);

        BitmapDrawable drawable = (BitmapDrawable) mIvPic.getDrawable();
        Bitmap bitmap = drawable.getBitmap();

        Palette.Builder builder = Palette.from(bitmap);
        builder.maximumColorCount(32); // 构建Palette时使用的最大颜色数,默认是16,风景图推荐取值8-16,人脸图像推荐取值24-32(值越大,花费的时间越长,可选择的色彩越多)
//                .setRegion(0, 0, bitmap.getWidth() - 1, bitmap.getHeight()) // 设置Palette颜色分析的图像区域
//                .addFilter(new Palette.Filter() { // 设置过滤器来过滤不需要的颜色(目前还不清楚怎么用,网上也找不到任何教程及代码,无从学习~)
//                    @Override
//                    public boolean isAllowed(int rgb, float[] hsl) {
//                        return false;
//                    }
//                })
//                .clearRegion()
//                .clearFilters();

        builder.generate(new Palette.PaletteAsyncListener() {
            @Override
            public void onGenerated(Palette palette) {
                // 暗、柔和
                int darkMutedColor = palette.getDarkMutedColor(Color.BLACK);
                // 亮、柔和
                int lightMutedColor = palette.getLightMutedColor(Color.BLACK);
                // 暗、鲜艳
                int darkVibrantColor = palette.getDarkVibrantColor(Color.BLACK);
                // 亮、鲜艳
                int lightVibrantColor = palette.getLightVibrantColor(Color.BLACK);
                // 柔和
                int mutedColor = palette.getMutedColor(Color.BLACK);
                // 鲜艳
                int vibrantColor = palette.getVibrantColor(Color.BLACK);

                // 获取某种色调的样品(这里指柔和的暗色)
                Palette.Swatch darkMutedSwatch = palette.getDarkMutedSwatch();
                // 获取图片的整体颜色rgb混合值---主色调
                int rgb = darkMutedSwatch.getRgb();
                // 获取图片中间的文字颜色
                int bodyTextColor = darkMutedSwatch.getBodyTextColor();
                // 获取作为标题的颜色(有一定的和图片的对比度的颜色值)
                int titleTextColor = darkMutedSwatch.getTitleTextColor();
                // 颜色向量
                float[] hsl = darkMutedSwatch.getHsl();
                // 分析该颜色在图片中所占像素值
                int population = darkMutedSwatch.getPopulation();

                mTvBody.setBackgroundColor(generateTransparentColor(0.2f, rgb));
                mTvBody.setTextColor(bodyTextColor);
                mTvTitle.setBackgroundColor(generateTransparentColor(0.6f, rgb));
                mTvTitle.setTextColor(titleTextColor);

                mTvDarkMutedColor.setBackgroundColor(darkMutedColor);
                mTvLightMutedColor.setBackgroundColor(lightMutedColor);
                mTvDarkVibrantColor.setBackgroundColor(darkVibrantColor);
                mTvLightVibrantColor.setBackgroundColor(lightVibrantColor);
                mTvMutedColor.setBackgroundColor(mutedColor);
                mTvVibrantColor.setBackgroundColor(vibrantColor);
            }
        });
    }