9

我正在尝试使用 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());
}
4

4 回答 4

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();
    }
于 2015-11-05T10:03:46.357 回答
0

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

palette.getVibrantColor().getRgb()

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

于 2015-11-11T08:27:22.380 回答
0

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

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

于 2015-11-09T20:41:20.527 回答
0

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

private void colorize(Bitmap photo) {   
Palette.from(photo).generate(new Palette.PaletteAsyncListener() {
            @Override
            public void onGenerated(Palette palette) {
                applyPalette(palette);
            }
        });
}
于 2015-11-08T15:11:14.947 回答