导出图表作为Android的图像 [英] Exporting Charts as images in Android

查看:225
本文介绍了导出图表作为Android的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个stucked了将近一个月。我在我的Andr​​oid code完美的图表使用afreechart,然而,图书馆似乎并没有对出口的图表作为图像,但(因为它是基于jfreechart的',这一次做这项工作)的支持

我试图让图表的角度,将其转换成画布和保存使用位图库的COM preSS的功能,但每次我尝试这方面,我得到一个全黑图像作为一个结果。我想这同样的方法,它适用于我的code的其他视图(简单的观点,像的LinearLayout和RelativeLayout的)。

之后,我试图创建一个程序,使图表活动的截图,之后关闭该活动。但我无法找到一个方法来做到这一点的code,我得到的最接近与monkeyrunner。

所以我放弃了这个想法,并试图寻找其他库,如kichart','achartengine等,但他们都不做了,我吓坏了工作,我认为图表导出到图像不会那么难...任何想法?

在我的布局设置背景颜色,返回的图像是一个完整的矩形背景的颜色,所以它得到的布局,只是没有图表。

我的code:

 包com.kichart;进口的java.io.File;
进口java.io.FileOutputStream中;进口android.app.Activity;
进口android.content.Context;
进口android.graphics.Canvas;
进口android.graphics.Bitmap;
进口android.graphics.Color;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.View;
进口android.widget.LinearLayout;公共类主要活动扩展{    LL的LinearLayout;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        浮动[] =值新的浮动[] {2.0F,1.5F,2.5F,1.0F,3.0F};
        的String [] = verlabels新的String [] {伟大,好,坏};
        的String [] = horlabels新的String [] {今天,明天,下星期,下个月};
        GraphView graphView =新GraphView(这一点,价值观,GraphViewDemo,horlabels,verlabels,GraphView.BAR);        LL =新的LinearLayout(本);
        ll.addView(graphView);        在Draw2D D =新Draw2D的(本);
        的setContentView(D);        //的setContentView(graphView);
    }
    公共类Draw2D的扩展视图{        公众在Draw2D(上下文的背景下){
            超级(上下文);
            setDrawingCacheEnabled(真);
        }        @覆盖
        保护无效的onDraw(帆布C){
            ll.setBackgroundColor(Color.WHITE);
            ll.measure(MeasureSpec.getSize(ll.getWidth()),MeasureSpec.getSize(ll.getHeight()));
            ll.layout(400,400,400,400);
            ll.draw(C);            尝试{
                getDrawingCache()的COM preSS(Bitmap.Com pressFormat.PNG,100,新的FileOutputStream(新文件(到/ mnt / SD卡/ graph2.png)))。
            }赶上(例外五){
                Log.e(错误--------->中,e.toString());
            }
            super.onDraw(C);
        }    }}


解决方案

您可以通过执行从一个视图图像位图:

 位图位图;
    chart.setDrawingCacheEnabled(真);
    位图= Bitmap.createBitmap(chart.getDrawingCache());
    chart.setDrawingCacheEnabled(假);

如果你不能叫getDrawingCache图表上,尝试把它像一个RelativeLayout的布局或类似的东西里面。并调用它的布局。

这之后,你可以将其保存为图像。

如果图像仍是黑色的,你需要确保图表越来越位图之前创建的。

希望这会帮助你。

I'm stucked in this for almost a month. I have a perfect chart in my android code using 'afreechart', however, the library does not seem to have support for exporting the chart as an image yet (since it's based on 'jfreechart' and this one does the job).

I tried to get the view of the chart, convert it into canvas and save using the compress functions of the bitmap library, however everytime i try this i get a totally black image as a result. I tried this same method and it works for the other views of my code (simple views, like linearlayout and relativelayout).

After that i tried to create a routine to make a screenshot of the chart activity and close that activity after that. But I couldn't find a way to do that by code, the closest i got was with monkeyrunner.

So i gave up of this idea and tried to look for other libraries, such as 'kichart', 'achartengine', etc, but none of them seem to do the job for, i'm freaking out and i thought that exporting the chart to an image wouldn't be that hard... any ideas?

When i set a background color for the layout, the returned image is a full rectangle with the color of the background, so it's getting the layout, just not the chart.

My Code:

package com.kichart;

import java.io.File;
import java.io.FileOutputStream;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

public class Main extends Activity {

    LinearLayout ll;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        float[] values = new float[] { 2.0f,1.5f, 2.5f, 1.0f , 3.0f };
        String[] verlabels = new String[] { "great", "ok", "bad" };
        String[] horlabels = new String[] { "today", "tomorrow", "next week", "next month" };
        GraphView graphView = new GraphView(this, values, "GraphViewDemo",horlabels, verlabels, GraphView.BAR);

        ll = new LinearLayout(this);
        ll.addView(graphView);

        Draw2d d = new Draw2d(this);
        setContentView(d);

        //setContentView(graphView);
    }


    public class Draw2d extends View {

        public Draw2d(Context context) {
            super(context);
            setDrawingCacheEnabled(true);
        }

        @Override
        protected void onDraw(Canvas c) {
            ll.setBackgroundColor(Color.WHITE);
            ll.measure(MeasureSpec.getSize(ll.getWidth()), MeasureSpec.getSize(ll.getHeight()));
            ll.layout(400, 400, 400, 400);
            ll.draw(c);

            try {
                getDrawingCache().compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(new File("/mnt/sdcard/graph2.png")));
            } catch (Exception e) {
                Log.e("Error--------->", e.toString());
            }
            super.onDraw(c);
        }

    }

}

解决方案

You can get a image bitmap from a view by doing:

  Bitmap bitmap;
    chart.setDrawingCacheEnabled(true);
    bitmap = Bitmap.createBitmap(chart.getDrawingCache());
    chart.setDrawingCacheEnabled(false);

if you can't call getDrawingCache on the chart, try to place it inside a layout like RelativeLayout or something like that. and call it on the layout.

After that you can save it as a image..

If the image is still black, you need to make sure the chart is created before getting the bitmap.

Hope this will help you

这篇关于导出图表作为Android的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆