【问题标题】:How to convert a AChartEngine chart to bitmap如何将 AChartEngine 图表转换为位图
【发布时间】:2013-04-05 20:43:30
【问题描述】:

我想将使用 AChartEngine lib 制作的折线图转换为位图。我该怎么做?我在网上没有找到任何可以提供帮助的东西。 toBitmap() 方法合适吗?如果合适那怎么用?

更新: 我用了这个方法:

 public static Bitmap loadBitmapFromView(View v) {  
v.setDrawingCacheEnabled(true);
v.layout( 0,0,800,600);
v.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
v.buildDrawingCache();
v.getDrawingCache();
 Bitmap bmp = Bitmap.createBitmap(800,600, Bitmap.Config.ARGB_8888);
v.setDrawingCacheEnabled(false);
return bmp; }

并将结果保存在 png 文件中,但我得到的只是一个空文件!

【问题讨论】:

    标签: android bitmap achartengine


    【解决方案1】:

    toBitmap() 似乎总是返回 null。

    试试这个:

                //Get the graphical view 
                GraphicalView v = ChartFactory.getLineChartView(context, 
                                buildDataset(titles, x, values), renderer); 
    
               //Enable the cache 
                v.setDrawingCacheEnabled(true); 
    
                //Set the layout manually to 800*600 
                v.layout(0, 0, 800, 600); 
    
                //Set the quality to high 
                v.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH); 
    
                //Build the cache, get the bitmap and close the cache 
                v.buildDrawingCache(true); 
                Bitmap b = Bitmap.createBitmap(v.getDrawingCache()); 
                v.setDrawingCacheEnabled(false); 
    

    【讨论】:

      【解决方案2】:

      有点晚了,但这是我从 GraphicalView 创建 JPG(通过位图)所做的:

      Bitmap bmp = Bitmap.createBitmap( 600, 1000, Bitmap.Config.ARGB_8888 );
      Canvas canvas = new Canvas(bmp);
      graphicalView.draw( canvas );
      
      FileOutputStream out = new FileOutputStream( filename );
      bmp.compress( Bitmap.CompressFormat.JPEG, 97, out );
      out.close();
      

      【讨论】:

        【解决方案3】:

        SVG 对图表更有效。对于我们正在使用的特定类型的图表,我破解了 AChartEngine 以输出 SVG。 hack 很容易扩展到其他图表。如果有人对此感兴趣,请回复此答案,我会将我所做的事情放在 Gist 中。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-11-04
          • 2012-03-11
          • 2016-03-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-07
          • 2011-03-03
          相关资源
          最近更新 更多