palette获取图片的颜色

引入
implementation ‘com.android.support:palette-v7:28.0.0’

Palette可以提取的颜色如下:

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

通过viewPager滑动图片 动态获取当前图片的颜色改变布局的颜色例子加深对palette的学习,当然palette还有很多知识在本文中是不做讲解

PagerAdapter adapter;
    String[] imgUrl={
            "https://m.360buyimg.com/babel/jfs/t1/20944/15/436/98674/5c09db63Efe8b5109/3e33075a0c75cb6e.jpg",
            "https://img1.360buyimg.com/pop/jfs/t1/18789/15/932/82976/5c0e08feE50db005f/587733e110200092.jpg",
            "https://m.360buyimg.com/babel/jfs/t1/10058/10/4258/97712/5c0b4e68E76055004/8bde7cc1f21a47a8.jpg",
            "https://m.360buyimg.com/babel/jfs/t1/28404/26/446/72391/5c0a15c3E30599999/85d7c5008e4fa53f.jpg",
    };


    private void initViewPager() {
        final List views=new ArrayList<>();
        for (String s:imgUrl){
            ImageView imageView=new ImageView(this);
            Glide.with(LoginActivity.this)
                    .load(s)
                    .into(imageView);
            views.add(imageView);
        }

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
            }

            @Override
            public void onPageSelected(final int i) {
                ImageView imageView = (ImageView) views.get(i);
                Drawable d = imageView.getDrawable();
                BitmapDrawable bitmap=(BitmapDrawable)d ;
                Palette.from(bitmap.getBitmap()).generate(new Palette.PaletteAsyncListener() {
                    public void onGenerated(Palette palette) {
                        Palette.Swatch vibrant = palette.getVibrantSwatch();
                        if (vibrant != null) {
                            View view=getWindow().getDecorView();
                            view.setBackgroundColor( vibrant.getRgb());
                        }
                    }
                });
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });


        viewPager.setAdapter(adapter=new PagerAdapter() {
            @Override
            public int getCount() {
                return views.size();
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
                return view==o;
            }

            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                final ImageView imageView= (ImageView) views.get(position);
                container.addView(imageView);
                return imageView;
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
//                super.destroyItem(container, position, object);
                container.removeView(views.get(position));
            }
        });
    }

你可能感兴趣的:(工作笔记)