Android Achartengine从图表创建位图始终为空

Android Achartengine从图表创建位图始终为空,android,achartengine,Android,Achartengine,在谷歌搜索了几个小时后,我尝试了各种方法。有些方法似乎对某些人有帮助,但对我没有帮助。我错过了什么?也许需要许可?我知道这是一个常见的错误或什么,但必须有一个解决方案 所有的方法都是相似的 第一名: GraphicalView v = ChartFactory.getBarChartView(ChartsDuration.this, buildBarDataset(titles, values), renderer, Type.DEFAULT); v.s

在谷歌搜索了几个小时后,我尝试了各种方法。有些方法似乎对某些人有帮助,但对我没有帮助。我错过了什么?也许需要许可?我知道这是一个常见的错误或什么,但必须有一个解决方案

所有的方法都是相似的

第一名:

GraphicalView v = ChartFactory.getBarChartView(ChartsDuration.this, buildBarDataset(titles, values), renderer, Type.DEFAULT); 
                    v.setDrawingCacheEnabled(true);
                    v.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
                    v.buildDrawingCache(true);
                    Bitmap bitmap = Bitmap.createBitmap(v.toBitmap()); //bitmap is null
                    v.setDrawingCacheEnabled(false);

or this:

                    Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache()); //bitmap is null
                    v.setDrawingCacheEnabled(true);

                 // this is the important code :)  
                 // Without it the view will have a dimension of 0,0 and the bitmap will be null          
                 v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
                             MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                 v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); 

                 v.buildDrawingCache(true);
                 Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
                 v.setDrawingCacheEnabled(false); // clear drawing cache
v.setDrawingCacheEnabled(false);
                    if (!v.isDrawingCacheEnabled()) {
                      v.setDrawingCacheEnabled(true);
                    }
                    if (renderer.isApplyBackgroundColor()) {
                      v.setDrawingCacheBackgroundColor(renderer.getBackgroundColor());
                    }
                    v.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
                    v.getDrawingCache(true);
                    Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
   v.setDrawingCacheEnabled(true);
                    v.requestFocus();
                    v.getRootView();


                    v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());

                    v.buildDrawingCache(true);
                    Bitmap mBitmap = v.getDrawingCache();
我还尝试了以下解决方案:

GraphicalView v = ChartFactory.getBarChartView(ChartsDuration.this, buildBarDataset(titles, values), renderer, Type.DEFAULT); 
                    v.setDrawingCacheEnabled(true);
                    v.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
                    v.buildDrawingCache(true);
                    Bitmap bitmap = Bitmap.createBitmap(v.toBitmap()); //bitmap is null
                    v.setDrawingCacheEnabled(false);

or this:

                    Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache()); //bitmap is null
                    v.setDrawingCacheEnabled(true);

                 // this is the important code :)  
                 // Without it the view will have a dimension of 0,0 and the bitmap will be null          
                 v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
                             MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                 v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); 

                 v.buildDrawingCache(true);
                 Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
                 v.setDrawingCacheEnabled(false); // clear drawing cache
v.setDrawingCacheEnabled(false);
                    if (!v.isDrawingCacheEnabled()) {
                      v.setDrawingCacheEnabled(true);
                    }
                    if (renderer.isApplyBackgroundColor()) {
                      v.setDrawingCacheBackgroundColor(renderer.getBackgroundColor());
                    }
                    v.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
                    v.getDrawingCache(true);
                    Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
   v.setDrawingCacheEnabled(true);
                    v.requestFocus();
                    v.getRootView();


                    v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());

                    v.buildDrawingCache(true);
                    Bitmap mBitmap = v.getDrawingCache();
然后选择页面底部的方法:

GraphicalView v = ChartFactory.getBarChartView(ChartsDuration.this, buildBarDataset(titles, values), renderer, Type.DEFAULT); 
                    v.setDrawingCacheEnabled(true);
                    v.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
                    v.buildDrawingCache(true);
                    Bitmap bitmap = Bitmap.createBitmap(v.toBitmap()); //bitmap is null
                    v.setDrawingCacheEnabled(false);

or this:

                    Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache()); //bitmap is null
                    v.setDrawingCacheEnabled(true);

                 // this is the important code :)  
                 // Without it the view will have a dimension of 0,0 and the bitmap will be null          
                 v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
                             MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                 v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); 

                 v.buildDrawingCache(true);
                 Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
                 v.setDrawingCacheEnabled(false); // clear drawing cache
v.setDrawingCacheEnabled(false);
                    if (!v.isDrawingCacheEnabled()) {
                      v.setDrawingCacheEnabled(true);
                    }
                    if (renderer.isApplyBackgroundColor()) {
                      v.setDrawingCacheBackgroundColor(renderer.getBackgroundColor());
                    }
                    v.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
                    v.getDrawingCache(true);
                    Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
   v.setDrawingCacheEnabled(true);
                    v.requestFocus();
                    v.getRootView();


                    v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());

                    v.buildDrawingCache(true);
                    Bitmap mBitmap = v.getDrawingCache();
我也没有得到任何运气:

GraphicalView v = ChartFactory.getBarChartView(ChartsDuration.this, buildBarDataset(titles, values), renderer, Type.DEFAULT); 
                    v.setDrawingCacheEnabled(true);
                    v.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
                    v.buildDrawingCache(true);
                    Bitmap bitmap = Bitmap.createBitmap(v.toBitmap()); //bitmap is null
                    v.setDrawingCacheEnabled(false);

or this:

                    Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache()); //bitmap is null
                    v.setDrawingCacheEnabled(true);

                 // this is the important code :)  
                 // Without it the view will have a dimension of 0,0 and the bitmap will be null          
                 v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
                             MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                 v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); 

                 v.buildDrawingCache(true);
                 Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
                 v.setDrawingCacheEnabled(false); // clear drawing cache
v.setDrawingCacheEnabled(false);
                    if (!v.isDrawingCacheEnabled()) {
                      v.setDrawingCacheEnabled(true);
                    }
                    if (renderer.isApplyBackgroundColor()) {
                      v.setDrawingCacheBackgroundColor(renderer.getBackgroundColor());
                    }
                    v.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
                    v.getDrawingCache(true);
                    Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
   v.setDrawingCacheEnabled(true);
                    v.requestFocus();
                    v.getRootView();


                    v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());

                    v.buildDrawingCache(true);
                    Bitmap mBitmap = v.getDrawingCache();

位图总是空的。

只是一个想法,不确定它是否有效:

将Achartengine视图放在另一个视图(例如框架布局)中,然后捕获它而不是Achartengine视图

另外,确保代码只在一段时间后运行,所有视图都已绘制完毕?要进行检查,请尝试获取视图的宽度或高度


您也没有任何异常或特殊日志吗?

请问,您解决了吗?如果是,你该怎么办?调用ImageView.setImageBitmap(GraphicalView.toBitmap())时也会遇到同样的问题。图像视图是不可见的。。。但是,如果我有意向并启动了活动,图表是可见的。是否有解决此问题的方法?