Palette打造你炫酷统一色彩的界面

来个图片:
这里写图片描述
1.Palette类
这个类是一个图片色彩分析类。相当于调色板。可以将你的图片分析出和界面统一且搭调的色彩。

2.Palette需要as中依赖
build.gradle中依赖compile ‘com.android.support:palette-v7:26.+’

3代码:都有注释的:

public class MainActivity extends AppCompatActivity {
    private ImageView iv;
    private TextView tv, tv1, tv2, tv3, tv4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView) findViewById(R.id.iv_id);
        tv = (TextView) findViewById(R.id.title_tv);
        tv1 = (TextView) findViewById(R.id.title_tv1);
        tv2 = (TextView) findViewById(R.id.title_tv2);
        tv3 = (TextView) findViewById(R.id.title_tv3);
        tv4 = (TextView) findViewById(R.id.title_tv4);

        BitmapDrawable drawable = (BitmapDrawable) iv.getDrawable();
        Bitmap bitmap = drawable.getBitmap();
        //得到bitmap里面的所有的信息 通过Palette类分析出来
        //Palette palette=Palette.generate(bitmap);//这个方法会分析可能分析特别耗时
        //异步任务可能分析的图片会比较大或者颜色分布比较复杂。耗时比较久,防止卡死主线程。
        Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
            @Override
            public void onGenerated(Palette palette) {
                int darkMutedColor = palette.getDarkMutedColor(Color.BLUE);
                //暗 柔和
                int lightMutedColor = palette.getLightMutedColor(Color.BLUE);
                //暗 鲜艳
                int vibrantcolor = palette.getDarkVibrantColor(Color.BLUE);
                //量 鲜艳
                int lightvibrantcolro = palette.getLightVibrantColor(Color.BLUE);
                //柔和
                int mutedColor = palette.getMutedColor(Color.BLUE);
                int verberColor = palette.getVibrantColor(Color.BLUE);
                tv.setBackgroundColor(lightvibrantcolro);
                tv1.setBackgroundColor(vibrantcolor);
                tv2.setBackgroundColor(lightMutedColor);
                tv3.setBackgroundColor(mutedColor);
                tv4.setBackgroundColor(verberColor);
                tv1.setText("vibrantcolor");
                tv2.setText("lightMutedColor");
                tv3.setText("mutedColor");
                tv4.setText("verberColor");


                Palette.Swatch lightVibrantSwatch = palette.getLightVibrantSwatch();
                //谷歌推荐的:图片的整体的颜色rgb的混合痔---主色调
                int rgb = lightVibrantSwatch.getRgb();
                int bodyTextColor = lightVibrantSwatch.getBodyTextColor();
                //谷歌推荐:作为标题的颜色(有一定的和图片对比度的颜色)
                int titleTextColor = lightVibrantSwatch.getTitleTextColor();
                //颜色向量
                float[] hsl = lightVibrantSwatch.getHsl();
                //分析该颜色在图片中所占的像素多少值
                int population = lightVibrantSwatch.getPopulation();

                tv.setTextColor(titleTextColor);
                tv.setBackgroundColor(getTranslucentColor(0.7f, rgb));


            }
        });


    }

    /**
     * rgb & 0xff运算如下:
     *    a           r       g          b
     * 11011010  01111010  10001010  10111010
     *                               11111111 做运算
     *                               10111010这样就取出了blue了
     * int green = rgb>> 8 & 0xff运算如下:
     *           rgb>>8运算:
     *           11011010  01111010  10001010  这里砍掉10111010
     *           和0xff运算
     *                               11111111 做运算
     *                               100001010  这样就取出了g了
     *
     * @param
     * @param rgb
     * @return
     */
    private int getTranslucentColor(float percent, int rgb) {
        //10101011110001111
        int blue = rgb & 0xff;//源码就是这样玩的
        int green = rgb>> 8 & 0xff;
        int red = rgb>> 16 & 0xff;
        int alpha = rgb>>>24;
        alpha=Math.round(alpha*percent);
        //int blue=Color.blue(rgb);//会自动给你分析出颜色
        return Color.argb(alpha,red,green,blue);
    }
}

github地址:https://github.com/luhenchang/Lsn11_MateriaDesign_Palette.git

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值