我正在尝试使用androids材料设计的调色板功能,但我在应用它时遇到了一些麻烦.
我已经成功生成了调色板,现在我试图将调色板传递给应用它的函数.
我遇到的问题是,当我将调色板传递给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());
}
Run Code Online (Sandbox Code Playgroud) 我有一些使用新Palette类的代码,我在Crashlytics上得到这些崩溃报告说的width and height must be > 0.奇怪的是,这就是我调用调色板代码的方式:
if( bitmap == null || bitmap.getHeight() <= 0 || bitmap.getWidth() <= 0){
//do something
}else{
Palette.Builder(bitmap).generate(new Palette.PaletteAsyncListener() {
.....
}
Run Code Online (Sandbox Code Playgroud)
所以我只是不确定突然的位图是否有可能没有正确的高度或宽度.我不知道异常来自哪个代码,因为报告只包含调色板类中的内容.
这是一个例外:
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.IllegalArgumentException: width and height must be > 0
at android.graphics.Bitmap.createBitmap(Bitmap.java:815)
at android.graphics.Bitmap.createBitmap(Bitmap.java:794)
at android.graphics.Bitmap.createBitmap(Bitmap.java:725)
at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:601)
at android.support.v7.graphics.Palette.scaleBitmapDown(Palette.java:282)
at android.support.v7.graphics.Palette.access$100(Palette.java:67)
at android.support.v7.graphics.Palette$Builder.generate(Palette.java:557)
at android.support.v7.graphics.Palette$Builder$1.doInBackground(Palette.java:623) …Run Code Online (Sandbox Code Playgroud) 我最近使用了https://material.io/tools/color/上的材料调色板生成器来生成调色板。
如您所见,该工具是 Material.io 上的官方工具。它生成了几种颜色 - 主要颜色和次要颜色,包括每种颜色的两种变体。这导致我在我的colors.xml文件中创建了这样的东西。
<!-- Color palette -->
<!-- ... -->
<!-- Colors by usage -->
<color name="colorPrimary">@color/black</color>
<color name="colorPrimaryDark">@color/blackDark</color>
<color name="colorPrimaryLight">@color/gray</color>
<color name="colorSecondary">@color/red</color>
<color name="colorSecondaryDark">@color/maroon</color>
<color name="colorSecondaryLight">@color/redLight</color>
<color name="colorTextOnPrimary">@color/white</color>
<color name="colorTextOnSecondary">@color/white</color>
Run Code Online (Sandbox Code Playgroud)
但是,如何在我的主题中应用这些颜色?Android 支持材料 (AppCompat) 主题只需要/允许三种颜色 - 主要、次要和重音。我应该使用另一个主题来应用这些属性吗?或者我只是在这里做错了什么?
我想避免在这里创建一个全新的主题,并且必须为我可能想要使用的每个组件手动设置颜色。
android android-theme android-styles material-design android-palette
我遇到以下情况时,最近一直在玩Bitmaps和调色板:
java.lang.IllegalArgumentException: background can not be translucent
at android.support.v7.graphics.ColorUtils.findMinimumAlpha(ColorUtils.java:90)
at android.support.v7.graphics.ColorUtils.getTextColorForBackground(ColorUtils.java:127)
at android.support.v7.graphics.Palette$Swatch.ensureTextColorsGenerated(Palette.java:621)
at android.support.v7.graphics.Palette$Swatch.getTitleTextColor(Palette.java:605)
Run Code Online (Sandbox Code Playgroud)
深入了解源代码,我发现:
private static int findMinimumAlpha(int foreground, int background, double minContrastRatio) {
if (Color.alpha(background) != 255) {
throw new IllegalArgumentException("background can not be translucent");
}
...
}
Run Code Online (Sandbox Code Playgroud)
我使用的图像是这样的:

我认为这个问题与图像在某种程度上完全透明有关.我目前正在执行与throw子句几乎相同的检查Color.alpha(palette.getSomeColor()) != 255,但这只是错误的.
在处理Palettes时是否有一种解决这个问题的方法?我觉得好像这是一个常见的错误,我必须做错了什么,或者错过了一些关于这个的指南.
我正在尝试制作一个显示来自在线数据库的图像的应用程序。但我想使用调色板 API 从每个图像中获得鲜艳的色彩。但我想我不知道如何使用 Bitmap decodeResource 方法或 Palette。请指导我如何在在线 URL 或 URI 图像文件中使用带有调色板的 Bitmap factory.decoderResource。
这是我在第 171 行“位图无效”中显示的活动类 recylceview 子类错误中使用的方法:
170 Bitmap photo = BitmapFactory.decodeResource(getResources(), mCursor.getPosition());
171 Palette.generateAsync(photo, new Palette.PaletteAsyncListener() {
public void onGenerated(Palette palette) {
int bgColor = palette.getLightVibrantColor(getApplicationContext().getResources().getColor(android.R.color.black));
holder.placeNameHolder.setBackgroundColor(bgColor);
}
});
Run Code Online (Sandbox Code Playgroud)
所以这里是错误:
10-29 17:39:42.067 31692-31750/com.example.xyzreader E/libEGL: load_driver(/system/lib/egl/libGLES_emulation.so): dlopen failed: library "/system/lib/egl/libGLES_emulation.so" not found
10-29 17:39:42.282 31692-31692/com.example.xyzreader E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.xyzreader, PID: 31692
java.lang.IllegalArgumentException: Bitmap is not valid
at android.support.v7.graphics.Palette$Builder.<init>(Palette.java:616)
at android.support.v7.graphics.Palette.from(Palette.java:100)
at android.support.v7.graphics.Palette.generateAsync(Palette.java:134)
at com.example.xyzreader.ui.ArticleListActivity$Adapter.onBindViewHolder(ArticleListActivity.java:171)
at com.example.xyzreader.ui.ArticleListActivity$Adapter.onBindViewHolder(ArticleListActivity.java:125)
at …Run Code Online (Sandbox Code Playgroud) android color-palette bitmapfactory android-bitmap android-palette
2014年10月,Jake Wharton写了Coelcing Picasso to Play With Palette.在其中,有两种方法正在思考:
使用生成调色板的转换.这样做的好处是,调色板是在Picasso的工作线程上生成的,并在加载图像时准备就绪.然而,缺点似乎是我得到了与图片不匹配的调色板.目前我一次只运行其中一个转换.
使用Callback的是onSuccess从获取位图ImageView并生成调色板.我不禁专注于// Ew!Jake在这一行结束时提出的建议,我完全赞同.
上面提到的帖子是在2014年10月写的.现在我们已经过了几个月的讨论,我想知道推荐的方法是什么?还有一个关于meta data与位图关联的讨论.这是实施的吗?我该如何使用它?
我的Transformation代码(不是最终的,我刚开始研究这个):
public final class PaletteGenerator
implements Transformation {
private final int numColors;
@Getter private Palette palette;
@Override public Bitmap transform (final Bitmap source) {
palette = null;
final Palette palette = numColors > 0
? Palette.generate (source, numColors)
: Palette.generate (source);
return source;
}
@Override public String key () …Run Code Online (Sandbox Code Playgroud) 如何为调色板的样本添加透明度值?就像我将颜色(swatch.getRGB())添加到线性布局一样,它显示纯色.而且我不想使用alpha,因为它会使布局中的其他项目也变得透明.
我的代码片段:
Palette palette = Palette.from(myBitmap).generate();
Palette.Swatch swatch1 = palette.getDarkVibrantSwatch();
int color = swatch1.getRgb();
thatLayout.setBackgroundColor(color)
Run Code Online (Sandbox Code Playgroud) 我正在按照本教程Flexibal Space with Image
对于工具栏的设计模式,在Android中使用带有Image的Flexibal Space.在Palette这里使用时,我被困在java文件中跟随错误导致应用程序崩溃.
Should pass resolved color instead of resource id here: `getResources().getColor(R.attr.colorPrimary)`
Run Code Online (Sandbox Code Playgroud)
Follwing是我的java文件YoutubeActivity.java
public class YoutubeActivity extends AppCompatActivity {
Toolbar toolbar;
CollapsingToolbarLayout collapsingToolbar;
int mutedColor = R.attr.colorPrimary;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_youtube);
toolbar = (Toolbar) findViewById(R.id.anim_toolbar);
setSupportActionBar(toolbar);
collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbar.setTitle("Praval Sharma");
ImageView header = (ImageView) findViewById(R.id.header);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.header);
Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
mutedColor = …Run Code Online (Sandbox Code Playgroud) android android-appcompat android-toolbar android-palette android-support-design
我有一个使用android.support.v7.graphics.Palette的android应用程序,可从动态图像获取颜色信息,然后自定义活动的布局,以使用图像中的突变颜色。我的问题是,Angular 5有什么相似之处吗?我想将此项目的Web版本建模为尽可能接近android版本。这将意味着在选择图像后动态设置一些样式颜色。
更新:我一直在寻找JavaScript的ColorThief()。但是我不确定如何从Angular 5组件中访问它。
谢谢PK