【问题标题】:How To Change App Bar Color With Imageview Color?如何使用 Imageview 颜色更改应用栏颜色?
【发布时间】:2019-09-14 17:24:49
【问题描述】:

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

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

我得到这样的图标

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

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

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 但是等等,你有一个动态的应用程序图标?我的意思是,当您设置应用程序图标时,您会看到颜色,不是吗?为什么要进行动态设置?
  • 我尝试使用 Palette lib 获取颜色
  • 但我怎样才能将图像视图作为位图资源。 ImageView 图标 = findViewById(R.id.icon); icon.setImageDrawable(Util.getAppIconFromPackage(this, packageName));位图位图 = BitmapFactory.decodeResource(getResources(), icon.getDrawable());

标签: java android colors


【解决方案1】:

解决了!!

添加这个 Gradle

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

并在活动中添加此代码

 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);*/
            }
        });


    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多