【问题标题】:Android Apply PaletteAndroid 应用调色板
【发布时间】:2015-11-02 10:47:19
【问题描述】:

我正在尝试使用 androids material design 的调色板功能,但我在应用它时遇到了一些麻烦。

我已成功生成调色板,现在我正尝试将调色板传递给应用它的函数。

我遇到的问题是,当我将调色板传递给 applyPalette 函数时,没有任何方法像 palette.getDarkMutedColor().getRgb() , palette.getVibrantColor().getRgb() 被填充调色板中的值。

除了将调色板传递给函数之外,我所遵循的教程没有提及其他任何内容,并且这样做会填充方法

这是生成器函数和应用函数,任何人都可以看到为什么这不起作用吗?

代码

private void colorize(Bitmap photo) {
    Palette palette = new Palette.Builder(photo).generate();
    applyPalette(palette);
}

private void applyPalette(Palette palette) {
    getWindow().setBackgroundDrawable(new ColorDrawable(palette.getDarkMutedColor().getRgb()));

    TextView titleView = (TextView) findViewById(R.id.title);
    titleView.setTextColor(palette.getVibrantColor().getRgb());

    TextView descriptionView = (TextView) findViewById(R.id.description);
    descriptionView.setTextColor(palette.getLightVibrantColor().getRgb());

    colorRipple(R.id.info, palette.getDarkMutedColor().getRgb(),
            palette.getDarkVibrantColor().getRgb());
    colorRipple(R.id.star, palette.getMutedColor().getRgb(),
            palette.getVibrantColor().getRgb());

    View infoView = findViewById(R.id.information_container);
    infoView.setBackgroundColor(palette.getLightMutedColor().getRgb());

    AnimatedPathView star = (AnimatedPathView) findViewById(R.id.star_container);
    star.setFillColor(palette.getVibrantColor().getRgb());
    star.setStrokeColor(palette.getLightVibrantColor().getRgb());
}

【问题讨论】:

  • 尝试移除 getRgb() 函数。
  • 不,没有帮助仍然有同样的错误
  • 当您说颜色功能“不起作用”时,请澄清您的意思。
  • 我收到一个错误,在每个语句下划线DarkMutedColor (int) in Palette cannot be applied to 0
  • 您使用的是哪个版本的 Palette(例如 21.0.0)?

标签: android android-palette


【解决方案1】:

使用 picassopalette 第三方库并将其导入您的项目,然后使用以下代码:

try {
        ContextWrapper cw = new ContextWrapper(OtherUserProfileScreenActivity.this);
        Picasso.with(this).load(image + ".jpg").placeholder(R.drawable.ic_loading).error(R.drawable.ic_error).into(imageView, PicassoPalette.with(Image + ".jpg", imageView).use(PicassoPalette.Profile.MUTED_DARK).intoCallBack(new BitmapPalette.CallBack() {
            @Override
            public void onPaletteLoaded(Palette palette) {

                int mutedColor = palette.getMutedColor(R.attr.colorPrimary);
                mCollapsingToolbarLayout.setContentScrimColor(mutedColor);
            }
        }));
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        System.gc();
    }

【讨论】:

  • 感谢您的回复,但我不想使用第三方库
【解决方案2】:

您已经尝试过同步方式。所以我认为下面的代码会解决你的问题(以异步方式)。

private void colorize(Bitmap photo) {   
Palette.from(photo).generate(new Palette.PaletteAsyncListener() {
            @Override
            public void onGenerated(Palette palette) {
                applyPalette(palette);
            }
        });
}

【讨论】:

  • 感谢您的建议,我尝试用您的错误替换我的代码,但仍然是同样的错误
【解决方案3】:

documentation 中,您在Palette 中使用的所有调用都已返回 RGB 值,但需要传递默认颜色。也许您打算使用返回颜色样本的那些?例如,不要这样做palette.getVibrantColor().getRgb(),而是改为这样做palette.getVibrantSwatch().getRgb()。将所有 getColor 调用替换为适当的 getSwatch() 调用。

此外,请确保您的导入中有 import android.support.v7.graphics.Palette,并且您的依赖项中包含 compile 'com.android.support:palette-v7:22.1.0'。 22.1.0 版本是您使用Palette.Builder 时的最低版本。

【讨论】:

  • 感谢您的建议,但我已经包含并导入了这些内容
【解决方案4】:

首先我不知道为什么你写的时候没有出错

palette.getVibrantColor().getRgb()

我会假设你没有得到错误,所以你必须使用旧库。与更新后的一样,它接受一个参数作为默认颜色值。 提取 RGB 更好的方法是获取 Palette.Swatch 对象并获取 RGB 值。 我确实创建了一个简单的小型应用程序来演示如何使用改进的库,您可以查看here。希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-23
    • 2014-09-18
    • 2014-12-15
    • 1970-01-01
    • 2019-07-25
    • 2018-12-29
    • 1970-01-01
    • 2019-12-27
    相关资源
    最近更新 更多