Android 应用调色板
Android Apply Palette
我正在尝试使用 androids material 设计的调色板功能,但我在应用它时遇到了一些问题。
我已经成功生成了调色板,现在我正在尝试将调色板传递给应用它的函数。
我遇到的问题是,当我将调色板传递给 applyPalette
函数时,像 palette.getDarkMutedColor().getRgb() , palette.getVibrantColor().getRgb()
这样的方法的 none 正在填充调色板中的值.
除了将调色板传递给函数外,我所遵循的教程没有提及任何其他内容,这样做会填充方法
这是生成器函数和应用函数,有人能看出为什么这不起作用吗?
代码
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());
}
使用 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();
}
您已经尝试过同步方式。所以我认为下面的代码将解决您的问题(以异步方式)。
private void colorize(Bitmap photo) {
Palette.from(photo).generate(new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
applyPalette(palette);
}
});
}
从 documentation 开始,您使用的所有来自 Palette
的调用都已经 return 一个 RGB 值,但需要传递默认颜色。也许您打算使用 return 色样代替?例如,您不应该这样做 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
.
首先我不知道为什么你写的时候没有报错
palette.getVibrantColor().getRgb()
我假设您没有遇到错误,所以您一定是在使用旧库。与更新后的一样,它接受一个参数作为默认颜色值。
要提取 RGB 更好的方法是获取 Palette.Swatch
对象并获取 RGB 值。
我确实创建了一个简单的小应用程序来演示如何使用改进的库,你可以查看 here。希望这有帮助。
我正在尝试使用 androids material 设计的调色板功能,但我在应用它时遇到了一些问题。
我已经成功生成了调色板,现在我正在尝试将调色板传递给应用它的函数。
我遇到的问题是,当我将调色板传递给 applyPalette
函数时,像 palette.getDarkMutedColor().getRgb() , palette.getVibrantColor().getRgb()
这样的方法的 none 正在填充调色板中的值.
除了将调色板传递给函数外,我所遵循的教程没有提及任何其他内容,这样做会填充方法
这是生成器函数和应用函数,有人能看出为什么这不起作用吗?
代码
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());
}
使用 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();
}
您已经尝试过同步方式。所以我认为下面的代码将解决您的问题(以异步方式)。
private void colorize(Bitmap photo) {
Palette.from(photo).generate(new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
applyPalette(palette);
}
});
}
从 documentation 开始,您使用的所有来自 Palette
的调用都已经 return 一个 RGB 值,但需要传递默认颜色。也许您打算使用 return 色样代替?例如,您不应该这样做 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
.
首先我不知道为什么你写的时候没有报错
palette.getVibrantColor().getRgb()
我假设您没有遇到错误,所以您一定是在使用旧库。与更新后的一样,它接受一个参数作为默认颜色值。
要提取 RGB 更好的方法是获取 Palette.Swatch
对象并获取 RGB 值。
我确实创建了一个简单的小应用程序来演示如何使用改进的库,你可以查看 here。希望这有帮助。