我试图避免使用 rgb 值,所以这个方法:
public LineAndPointFormatter(Integer lineColor, Integer vertexColor, Integer fillColor) {
this(lineColor, vertexColor, fillColor, FillDirection.BOTTOM);
}
我应该传入的地方:
LineAndPointFormatter lpf = new LineAndPointFormatter(Color.rgb(0, 0, 200), null, Color.rgb(0, 0, 80));
工作得很好,但我想使用这个方法:
public LineAndPointFormatter(Context ctx, int xmlCfgId) {
// prevent configuration of classes derived from this one:
if (getClass().equals(LineAndPointFormatter.class)) {
Configurator.configure(ctx, this, xmlCfgId);
}
}
所以我可以使用colors.xml资源ID并通过设置“colorID”然后将其传递来管理所有图形颜色(colorID在日志中显示为-65536或取决于的一些变体)。这段代码位于扩展 Fragment 的类中,所以我想知道上下文是否正确......
以下是我尝试过的一些方法:
LineAndPointFormatter seriesFormat = new LineAndPointFormatter(getActivity().getApplicationContext(), getResources().getColor(R.color.blue));
LineAndPointFormatter seriesFormat = new LineAndPointFormatter(getActivity().getApplicationContext(), colorID);
LineAndPointFormatter seriesFormat = new LineAndPointFormatter(getActivity().getApplicationContext(), getResources().getColor(colorID));
等等..
我会继续使用插件,但如果有人使用 LineAndPointFormatter 的 Context 版本,也许您遇到了类似的问题?
异常看起来像这样:
05-14 12:47:25.917: E/AndroidRuntime(14023): android.content.res.Resources$NotFoundException: 资源 ID #0xff0000ff
我必须将颜色重写为#ff0000ff 格式吗?
编辑:添加colors.xml
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<color name="white">#FFFFFF</color>
<color name="transparent_white">#66FFFFFF</color>
<color name="yellow">#FFFF00</color>
<color name="fuchsia">#FF00FF</color>
<color name="red">#FF0000</color>
<color name="silver">#C0C0C0</color>
<color name="gray">#808080</color>
<color name="olive">#808000</color>
<color name="purple">#800080</color>
<color name="maroon">#800000</color>
<color name="aqua">#00FFFF</color>
<color name="lime">#00FF00</color>
<color name="teal">#008080</color>
<color name="green">#008000</color>
<color name="blue">#0000ff</color>
<color name="navy">#000080</color>
<color name="black">#000000</color>
</resources>
最佳答案
我认为问题在于您使用的 xml 资源包含与“配置器”配置的对象无关的元素。配置器使用反射来确定如何将 xml 值映射到关联对象的成员。为此,xml 文件仅包含所配置对象的实际成员的元素并且这些元素与这些成员相同名称非常重要。
在上面的示例中,您尝试使用包含银色、灰色等不存在名称的元素的 xml 文件配置 LineAndPointFormatter。相反,您应该将配置存储在一个单独的 xml 文件中,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<config vertexPaint.color="#00FF00"
linePaint.color="#00FF00"
linePaint.strokeWidth="2dp"/>
一旦你定义了这个文件,你可以尝试(我个人没有尝试过这个,但假设它应该有效)是引用colors.xml中的值,这样当你调整颜色灰色的定义时,全局调整。
关于java - AndroidPlot:LineAndPointFormatter 在传入 Context 和 Colors.xml 资源 ID 时导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16551799/