问题
I am trying to add graph to my Android application using AChartEngine. The only thing different from examples is that I want to make from graph bitmap and then insert into ImageView
.
I am able to add graph without any problems to the LinearLayout, but after using method toBitmap()
on graph and inserting it into ImageView
I don't have anything and get NullPointerException
. The possible problem that I've found is ID of generated graph which is -1.
This is the code:
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();
}
}
}
Can someone tell me what should I do to generate image and insert it normally?
回答1:
This is not necessarily a solution to your problem exactly, but is there a reason you need it to be a bitmap? If you just need to display the graph on your screen consider having a linear layout where you want the graph(If you don't have one already) to be placed in your xml file. Then all you need to do is add mChartView to that layout with:
yourLinearLayout.addView(mChartView)
回答2:
Use Following code for converting your Layout into Bitmap. I hope it works for You.
public class AndroidLayoutImage extends Activity {
ImageView bmImage;
LinearLayout view;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view = (LinearLayout)findViewById(R.id.screen);
bmImage = (ImageView)findViewById(R.id.image);
view.setDrawingCacheEnabled(true);
// this is the important code :)
// Without it the view will have a dimension of 0,0 and the bitmap will be null
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false); // clear drawing cache
bmImage.setImageBitmap(b);
};
来源:https://stackoverflow.com/questions/17484356/android-converting-graph-achartengine-into-image-using-tobitmap