简体   繁体   English

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

[英]How To Change App Bar Color With Imageview Color?

I have one notification log app,how can I change app bar color with same color of app icon. 我有一个通知日志应用程序,如何用相同颜色的应用程序图标更改应用程序栏颜色。

For example i want to change app bar color from the icon image color 例如我想从图标图像颜色更改应用程序栏颜色

在此处输入图片说明

I am getting icon like this 我得到这样的图标

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

So How can I get color from image view and change it to app bar color 因此,如何从图像视图获取颜色并将其更改为应用栏颜色

SOLVED !! 解决了 !!

Add This Gradle 添加此摇篮

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

And Add This Code In 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);*/
            }
        });


    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM