Exporting Charts as images in Android

只愿长相守 提交于 2019-12-12 15:28:45

问题


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

    }

}

回答1:


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



来源:https://stackoverflow.com/questions/13068495/exporting-charts-as-images-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!