首页 > 解决方案 > 如何从 Android 上不可绘制的图像中选择主色

问题描述

这是我寻找主色的活动

  public class Main3Activity extends AppCompatActivity {  
        private Bitmap bitmap;
        private Palette.Swatch vibrantSwatch;
        private Palette.Swatch lightVibrantSwatch;
        private Palette.Swatch darkVibrantSwatch;
        Uri image_uri;
        ImageView mImageView;
        CheckBox checkBox1, checkBox2, checkBox3;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main3);
            mImageView = findViewById(R.id.image_view);             
            checkBox1 = findViewById(R.id.checkBox);
            checkBox2 = findViewById(R.id.checkBox2);
            checkBox3 = findViewById(R.id.checkBox3);
        }

这是捕获的结果

protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        //// show image
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            mImageView.setImageURI(image_uri);
        }

我想向复选框背景显示主色

这个方法我设置了复选框背景

    private void extractProminentColors(Bitmap bitmap){
            int defaultColor = 0x000000;
            Palette p = Palette.from(bitmap).generate();
            int VibrantColor = p.getVibrantColor(defaultColor);
            checkBox1.setBackgroundColor(VibrantColor);
            int VibrantColorDark = p.getDarkVibrantColor(defaultColor);
            checkBox2.setBackgroundColor(VibrantColorDark);
            int VibrantColorLight = p.getLightVibrantColor(defaultColor);
            checkBox3.setBackgroundColor(VibrantColorLight);
        }

标签: android

解决方案


您可以从 URI 获取位图

MediaStore.Images.Media.getBitmap(contentResolver, imageUri)

推荐阅读