2
我正在尝试使用AndroidPlot在主屏幕小部件中绘制图表。 我知道,在一个普通的应用程序中,它使用自定义视图,从我看到的(Android: AppWidget with custom view not working),解决方法是将其渲染为imageView
中的位图。AndroidPlot:如何在AppWidget中呈现图表?
现在我已经采用了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;
}
他如何使用布局之前,计算图表的大小? 150是硬编码的... –