0

我使用 achartengine 图表库创建了一个 BAR GRAPH,并尝试将此条形图保存为 .jpg 图像。条形图已成功显示在文本视图上,但是当我保存此条形图时,仅保存第一次显示文本视图时可见的条形图部分。我想将整个视图保存为图像,包括由于滚动而遗漏的部分。

代码:

public class MainActivity extends Activity {
String[] firstData1={"IDLE","IDLE","DCH","DCH","FACH"};
int[] firstData=new int[firstData1.length];
String[] m={"IDLE","FACH","DCH"};


public GraphicalView createIntent()
{
    int[] x={0,1,2,3,4};
    double[] time={1361292289.238427,1361292289.240838,1361292289.430260,1361292289.596886,1361292290.002587};
    String time1[]=new String[time.length];
    for(int i=0;i<firstData1.length;i++)
    {
        if(firstData1[i]=="IDLE")
        firstData1[i]="0";
        else if(firstData1[i]=="FACH")
            firstData1[i]="1";
            else
                firstData1[i]="2";

    }
    firstData=new int[firstData1.length];
    for(int i=0;i<firstData1.length;i++)
    {
        firstData[i]=Integer.parseInt(firstData1[i]);
    }

    XYSeries stateSeries = new XYSeries("Device State");
    for(int i=0;i<x.length;i++){
        stateSeries.add(i,firstData[i]);

    }
    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
    dataset.addSeries(stateSeries);
    XYSeriesRenderer stateRenderer = new XYSeriesRenderer();
    stateRenderer.setColor(Color.rgb(0, 250, 154));
    stateRenderer.setDisplayChartValues(true);

    XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
    multiRenderer.setAxisTitleTextSize(16);
    multiRenderer.setChartTitleTextSize(20);
    multiRenderer.setLabelsTextSize(10);
    multiRenderer.setLegendTextSize(10);
    multiRenderer.setBarSpacing(0.5);
    multiRenderer.setXLabelsAlign(Align.LEFT);
    multiRenderer.setLabelsColor(Color.BLACK);
    multiRenderer.setOrientation(Orientation.HORIZONTAL);
    multiRenderer.setYLabels(0); 
    multiRenderer.setMarginsColor(Color.parseColor("#EEEDED"));
    multiRenderer.setXLabelsColor(Color.BLACK);
    multiRenderer.setYLabelsColor(0,Color.BLACK);
    multiRenderer.setMargins(new int[] { 50, 45, 40, 40 }); 
    multiRenderer.setApplyBackgroundColor(true);
    multiRenderer.setBackgroundColor(Color.parseColor("#FBFBFC"));
    multiRenderer.setXLabels(0);
    multiRenderer.setChartTitle("Device State Chart");
    multiRenderer.setXTitle("Timestamp");
    multiRenderer.setYTitle("Device State");
    multiRenderer.setZoomButtonsVisible(true);
    for(int i=0; i<3;i++){
        multiRenderer.addYTextLabel(i, m[i]);
    }
    for(int i=0;i<time.length;i++)
    {
        DecimalFormat myFormatter = new DecimalFormat("0.000000");
        time1[i] = myFormatter.format(time[i]);
    }
    for(int i=0; i< x.length;i++){
        multiRenderer.addXTextLabel(i, time1[i]);
    }
    multiRenderer.addSeriesRenderer(stateRenderer);
    final GraphicalView grfv  = ChartFactory.getBarChartView(getBaseContext(), dataset, multiRenderer, BarChart.Type.DEFAULT);
    return grfv;

}



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final GraphicalView gv=createIntent();
    RelativeLayout rl=(RelativeLayout)findViewById(R.id.graph);
    rl.addView(gv);
    rl.measure(MeasureSpec.makeMeasureSpec(800, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(800, MeasureSpec.EXACTLY));
    rl.layout(0, 0, rl.getMeasuredWidth(), rl.getMeasuredHeight()); 
    Bitmap bm=Bitmap.createBitmap(rl.getWidth(), rl.getHeight(),Bitmap.Config.ARGB_8888);
    Canvas cn=new Canvas(bm);
    rl.draw(cn);
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/_images");
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "Image-" + n + ".jpg";
    File file = new File(myDir, fname);
    if (file.exists())
        file.delete();
    try {
        FileOutputStream out = new FileOutputStream(file);
        bm.compress(Bitmap.CompressFormat.JPEG, 30, out);
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
}
4

0 回答 0