在Android的运行时创建动态彩色背景资源 [英] Create a background resource with dynamic color at runtime in android

查看:184
本文介绍了在Android的运行时创建动态彩色背景资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 CaldroidFragment 我的应用程序。现在我尝试使用设置一个特定日期的背景颜色

  CaldroidFragment.setBackgroundResourceForDate(INT backgroundRes,
                                     java.util.Date日期);

功能。现在,如果我通过资源从XML,像R.color.blue作为第一个参数,然后它的工作原理,但我必须要通过动态色彩值在运行时的背景。我生成的范围内动态颜色(像插值)。我现在想getResources()。的getColor()函数来我的色彩转换资源

 私有静态最终诠释COLOR_END = Color.parseColor(#BD4141);
私有静态最终诠释COLOR_START = Color.parseColor(#69A864);mCaldroidFragment.setBackgroundResourceForDate(
                        getResources()。的getColor(getInterPolateColor(2)),
                        新的Date());
私人诠释getInterPolateColor(int值){
            返回Utility.interpolateColor(COLOR_START,COLOR_END,值/(浮点)15);
}公共静态INT interpolateColor(最终诠释startColor,最终诠释ENDCOLOR,最终浮动比例){
            最终浮动[] = HSVA新的浮动[3];
            最终浮动[] = hsvb新的浮动[3];
            Color.colorToHSV(startColor,HSVA);
            Color.colorToHSV(ENDCOLOR,hsvb);
            的for(int i = 0;我3;;我++){
                hsvb [I] =内插(HSVA [I],hsvb [I]的比例);
            }
            返回Color.HSVToColor(hsvb);
}私有静态插值浮动(浮动最后一个,最终浮动B,最终浮动比例){
            返回(A +((B - A)*比例));
}

但它显示了以下错误

  android.content.res.Resources $ NotFoundException:资源ID#0xff79ab60
            在android.content.res.Resources.getValue(Resources.java:1123)
            在android.content.res.Resources.getColor(Resources.java:805)

现在是有可能创造从颜色值的背景资源,这将像R.color.xxx,或任何解决方案来解决我的问题。


解决方案

据我所知,是没有办法在运行时动态。更换R项目(R.color,R.id等)。这些资源在构建时从你的XML(和其他静态)资源进行编译。

此外,根据本CaldroidFragment GitHub的自述,它确实像你预计在XML来定义自定义颜色:


  

要使用这些方法,你应该color.xml定义颜色和
  背景绘制文件夹:

  caldroidFragment.setBackgroundResourceForDate(R.color.blue,blueDate);
caldroidFragment.setBackgroundResourceForDate(R.color.green,greenDate);


所以,简单的答案似乎是,不,你不能动态地设置你的颜色。

我唯一的建议是,如果你能想出一个办法来覆盖/超载 setBackgroundResourceForDate(),这将会是最好的绘制资源来传递,而不是色为背景。这样,你就可以动态地调整绘制(颜色,图像,形状等),同时仍然保持相同R.id和名称。

I am using CaldroidFragment for my application. Now I am trying to set the background color of a specific date using

CaldroidFragment.setBackgroundResourceForDate(int backgroundRes,
                                     java.util.Date date);

function. Now if I pass resource from xml, like R.color.blue as first parameter then it works, but I have to pass dynamic color value as background in runtime. I generate color dynamically within a range (something like interpolation). Now I am trying getResources().getColor() function to convert my color to resource.

private static final int COLOR_END = Color.parseColor("#BD4141");
private static final int COLOR_START = Color.parseColor("#69A864");



mCaldroidFragment.setBackgroundResourceForDate(
                        getResources().getColor(getInterPolateColor(2)),
                        new Date());


private int getInterPolateColor(int value) {
            return Utility.interpolateColor(COLOR_START, COLOR_END, value / (float) 15);
}



public static int interpolateColor(final int startColor, final int endColor, final float proportion) {
            final float[] hsva = new float[3];
            final float[] hsvb = new float[3];
            Color.colorToHSV(startColor, hsva);
            Color.colorToHSV(endColor, hsvb);
            for (int i = 0; i < 3; i++) {
                hsvb[i] = interpolate(hsva[i], hsvb[i], proportion);
            }
            return Color.HSVToColor(hsvb);
}



private static float interpolate(final float a, final float b, final float proportion) {
            return (a + ((b - a) * proportion));
}

But it shows following error

 android.content.res.Resources$NotFoundException: Resource ID #0xff79ab60
            at android.content.res.Resources.getValue(Resources.java:1123)
            at android.content.res.Resources.getColor(Resources.java:805)

Now is it possible to create background resource from color value which will act like R.color.xxx, or any solution to solve my problem.

解决方案

As far as I know, there is no way to dynamically change R items (R.color, R.id, etc.) at runtime. These resources are compiled at build time from your xml (and other static) resources.

Also, according to the CaldroidFragment GitHub readme, it does indeed look like you're expected to define custom colors in xml:

To use these methods, you should define your colors in color.xml and background in drawable folder:

caldroidFragment.setBackgroundResourceForDate(R.color.blue, blueDate);
caldroidFragment.setBackgroundResourceForDate(R.color.green, greenDate);

So, the short answer seems to be, no, you can't set your colors dynamically.

My only suggestion is that, if you can figure out a way to override/overload setBackgroundResourceForDate(), it'd be best to pass in a drawable resource instead of color for the background. This way, you'll be able to dynamically adjust the drawable (color, image, shape, etc.) while still keeping the same R.id and name.

这篇关于在Android的运行时创建动态彩色背景资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆