【发布时间】:2012-09-06 17:21:12
【问题描述】:
我正在尝试使用 AndroidPlot 在主屏幕小部件中绘制图表。
我知道在普通应用程序中,它使用自定义视图,从我所见 (Android: AppWidget with custom view not working) 来看,解决方法是将其渲染为 imageView 中的位图。
现在我已经获取了 AndroidPlot 的快速入门代码并将其放入提供程序类中,但是当我将它放在主屏幕上时它似乎没有呈现任何内容。
此代码与原始快速入门代码的区别在于,在快速入门中,它利用了Activity.findViewById,但显然不能在这里使用。
任何人都可以在这里看到我做错了可能导致空渲染的事情吗? 感谢您提供的任何帮助!
private Bitmap getChartImage(Context context)
{
// initialize our XYPlot reference:
mySimpleXYPlot = new XYPlot(context, "My Simple XYPlot");
mySimpleXYPlot.setDrawingCacheEnabled(true);
// add a new series
mySimpleXYPlot.addSeries(new SimpleXYSeries(), LineAndPointRenderer.class, new LineAndPointFormatter(Color.rgb(0, 200, 0), Color.rgb(200, 0, 0)));
// reduce the number of range labels
mySimpleXYPlot.getGraphWidget().setRangeTicksPerLabel(4);
// reposition the domain label to look a little cleaner:
Widget domainLabelWidget = mySimpleXYPlot.getDomainLabelWidget();
mySimpleXYPlot.position(domainLabelWidget, // the widget to position
45, // x position value, in this case 45 pixels
XLayoutStyle.ABSOLUTE_FROM_LEFT, // how the x position value is applied, in this case from the left
0, // y position value
YLayoutStyle.ABSOLUTE_FROM_BOTTOM, // how the y position is applied, in this case from the bottom
AnchorPosition.LEFT_BOTTOM); // point to use as the origin of the widget being positioned
// get rid of the visual aids for positioning:
mySimpleXYPlot.disableAllMarkup();
//mySimpleXYPlot.measure(150, 150);
//mySimpleXYPlot.layout(0, 0, 150, 150);
Bitmap bmp = mySimpleXYPlot.getDrawingCache();
return bmp;
}
【问题讨论】:
-
我对另一个想要将其作为 Bmp 并更新远程视图的组件有同样的问题。在我的情况下,布局将永远不会绘制,因为它没有附加到任何视图,因此大小将始终为 0 。你找到解决办法了吗?
标签: android charts android-appwidget androidplot