android - 如何从 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);
}
解决方案
您可以从 URI 获取位图
MediaStore.Images.Media.getBitmap(contentResolver, imageUri)
推荐阅读
- r - 仅当使用 mclapply 进行一次以上迭代时,Rstudio 模拟才会在迭代后卡住
- r - 如何使用空格将一列分成两列
- svg - SVG 内联字体大小属性渲染的像素大小与预期大不相同
- javascript - 从 Chrome 扩展程序在外部 iframe 中填写表单数据
- android - 我的 android 应用程序不断停止,我不清楚 logct,请问有人可以帮忙吗?
- gtag.js - 使用 gtag.js 进行离线跟踪
- javascript - 聚合中的条件 $sort
- sql - SQL 在进行分配时排除列表中包含的所有可能性
- netlogo - 获取品种类型
- c++ - find prime numbers up to n digits using only 1 loop statement