我正在尝试使用AChartEngine将图形添加到我的 Android 应用程序中。与示例唯一不同的是,我想从图形位图制作,然后插入到ImageView
.
我可以毫无问题地向 LinearLayout 添加图形,但是在使用toBitmap()
图形上的方法并将其插入之后,ImageView
我什么都没有,得到NullPointerException
. 我发现的可能问题是生成图的 ID,即 -1。
这是代码:
public class Chart extends Activity{
/** The main dataset that includes all the series that go into a chart. */
private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
/** The main renderer that includes all the renderers customizing a chart. */
private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
/** The most recently added series. */
private XYSeries mCurrentSeries;
/** The most recently created renderer, customizing the current series. */
private XYSeriesRenderer mCurrentRenderer;
/** The chart view that displays the data. */
private GraphicalView mChartView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.widget_layout);
// set some properties on the main renderer
mRenderer.setAxisTitleTextSize(16);
mRenderer.setChartTitleTextSize(20);
mRenderer.setLabelsTextSize(15);
mRenderer.setLegendTextSize(15);
mRenderer.setPointSize(5);
}
@Override
protected void onResume() {
super.onResume();
if (mChartView == null) {
initChart();
addSampleData();
mChartView = ChartFactory.getLineChartView(this, mDataset, mRenderer);
mRenderer.setClickEnabled(true);
mRenderer.setSelectableBuffer(10);
Bitmap img = mChartView.toBitmap();
ImageView imgView = (ImageView)findViewById(R.id.graphView);
imgView.setImageBitmap(img);
} else {
mChartView.repaint();
}
}
}
有人能告诉我应该怎么做才能生成图像并正常插入吗?