java - 如何使用 Imageview 颜色更改应用栏颜色?

标签 java android colors

我有一个通知日志应用程序,如何将应用程序栏颜色更改为与应用程序图标相同的颜色。

例如,我想更改图标图像颜色的应用栏颜色

enter image description here

我得到这样的图标

  ImageView icon = findViewById(R.id.icon);
                icon.setImageDrawable(Util.getAppIconFromPackage(this, packageName));

那么如何从 ImageView 中获取颜色并将其更改为应用栏颜色

最佳答案

解决了!!

添加此 Gradle

    implementation 'com.android.support:palette-v7:23.1.1'

并在 Activity 中添加此代码

 private void geticoncolor() {
        ImageView icon = findViewById(R.id.icon);
        icon.setImageDrawable(Util.getAppIconFromPackage(this, packageName));

       //Bitmap bitmap = BitmapFactory.decodeResource(getResources(), geticoncolor());

        Bitmap bitmap3 = ((BitmapDrawable)icon.getDrawable()).getBitmap();

        BitmapDrawable drawable = (BitmapDrawable) icon.getDrawable();
        Bitmap bitmap2 = drawable.getBitmap();

        Palette.from(bitmap3).generate(new Palette.PaletteAsyncListener() {
            @Override
            public void onGenerated(Palette palette) {
                //work with the palette here
                int defaultValue = 0x000000;
                int vibrant = palette.getVibrantColor(defaultValue);
                int vibrantLight = palette.getLightVibrantColor(defaultValue);
                int vibrantDark = palette.getDarkVibrantColor(defaultValue);
                int muted = palette.getMutedColor(defaultValue);
                int mutedLight = palette.getLightMutedColor(defaultValue);
                int mutedDark = palette.getDarkMutedColor(defaultValue);

                ActionBar actionBar = getSupportActionBar();
                actionBar.setBackgroundDrawable(new ColorDrawable(vibrant));
               // actionBar.setTitle(Html.fromHtml("<font color='#000099'>Hello World</font>"));

               // Toast.makeText(DetailsActivity.this, ""+vibrant+vibrantDark+mutedLight+mutedDark, Toast.LENGTH_SHORT).show();
                /*vibrantView.setBackgroundColor(vibrant);
                vibrantLightView.setBackgroundColor(vibrantLight);
                vibrantDarkView.setBackgroundColor(vibrantDark);
                mutedView.setBackgroundColor(muted);
                mutedLightView.setBackgroundColor(mutedLight);
                mutedDarkView.setBackgroundColor(mutedDark);*/
            }
        });


    }

关于java - 如何使用 Imageview 颜色更改应用栏颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57932473/

相关文章:

java - 这个通用语法是什么意思?

java - 在 Java 中将角度归一化为 +/- π 弧度的标准方法

java - TestNG try/catch 无法正常工作

android - 如何在 Android 中带边框的 ImageView 中一起旋转、缩放和拖动

c# - WPF 数据网格在文本更改时更改颜色

java - 如何从日志中的日志请求中排除特定属性或数据类型?

android - 如何从 android 平台构建中删除大量模块?

android - 应用程序中是否可以有同一 Activity 的两个实例?

node.js - 颜色.js : Proper syntax for adding a color to a variable (string)?

java - JEdi​​torKit颜色单个字符没有html