0

我正在尝试使用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();
            }
          }

}

有人能告诉我应该怎么做才能生成图像并正常插入吗?

4

2 回答 2

0

使用以下代码将您的布局转换为位图。我希望这个对你有用。

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);   

    };
于 2013-07-05T08:31:29.570 回答
0

这不一定完全可以解决您的问题,但是您是否有理由需要将其作为位图?如果您只需要在屏幕上显示图表,请考虑在您希望将图表(如果您还没有)放置在您的 xml 文件中的位置进行线性布局。然后您需要做的就是将 mChartView 添加到该布局中:

yourLinearLayout.addView(mChartView)
于 2013-07-08T21:09:04.577 回答