安卓应用调色板

9
我正在尝试使用Android的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()函数。 - Sumighosh Charuvil
不,那没帮助,仍然有相同的错误。 - Hayes121
请澄清您所说的颜色函数“不起作用”是什么意思。 - stkent
我得到了一个错误,每个都说“调色板中的DarkMutedColor(int)不能应用于0”。 - Hayes121
你使用的Palette版本是哪个(例如21.0.0)? - Kai
我不知道,我唯一的依赖是 dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.google.android.gms:play-services:6.5.87' } 我是否应该使用另一个来处理调色板? - Hayes121
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();
    }

谢谢回复,但我不想使用第三方库。 - Hayes121

0

首先,我不知道为什么您在编写

时没有出现错误。
palette.getVibrantColor().getRgb()

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


0

你已经尝试了同步方式。因此,我认为以下代码将以异步方式解决您的问题。

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

谢谢您的建议,我尝试使用您的代码替换我的代码,但仍然出现相同的错误。 - Hayes121

0
文档中可以看出,您正在使用的所有来自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'。由于您正在使用Palette.Builder,因此版本22.1.0是最低要求。

谢谢你的建议,但我已经包含和导入了这些。 - Hayes121

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接