如何从AChartEngine GraphicalView中绘制位图

时间:2012-10-22 08:39:17

标签: android canvas achartengine ondraw

我有一个应用程序,它实时接收来自范围传感器的消息,我想用接收到的值更新图表。 我决定使用库GraphicalView中的AChartEngine。 我想在每次收到消息时更新Graphicalview,然后使用BitMap函数将其转换为toBitmap(),然后在画布上绘制位图。

我正在尝试查看是否可以执行此操作(因此我还没有使用消息,只是尝试从图形视图中绘制位图)但是当我运行代码时,我得到Nullpointer exception。这是我的代码:

public class DrawChartView extends View {
Paint paint = new Paint();
Rect rect = new Rect(130, 130, 80, 80);
Context context;
XYSeries currentDataset = new XYSeries("range data series");
XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
XYSeriesRenderer currentRenderer = new XYSeriesRenderer();
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
LineChart lc;
GraphicalView gv;
Bitmap bm;

public DrawChartView(Context context_) {
    super(context_);
    context = context_;
    //imposto i parametri del renderer
    mRenderer.setApplyBackgroundColor(true);
    mRenderer.setBackgroundColor(Color.argb(100, 50, 50, 50));
    mRenderer.setAxisTitleTextSize(16);
    mRenderer.setChartTitleTextSize(20);
    mRenderer.setLabelsTextSize(15);
    mRenderer.setLegendTextSize(15);
    mRenderer.setMargins(new int[] { 20, 30, 15, 0 });
    mRenderer.setZoomButtonsVisible(true);
    mRenderer.setPointSize(10);
    mRenderer.addSeriesRenderer(currentRenderer);
    //imposto il dataset
    currentDataset.add(1,1);
    currentDataset.add(2,2);
    currentDataset.add(3,3);
    mDataset.addSeries(currentDataset);
    //inizializzo il line chart
    lc = new LineChart(mDataset, mRenderer);
    //inizializzo la grahical view
    gv = new GraphicalView(context, lc);
    bm=gv.toBitmap();
}

public DrawChartView(Context context_, AttributeSet attrs) {
    super(context_, attrs);
    // TODO Auto-generated constructor stub
}

public DrawChartView(Context context_, AttributeSet attrs, int defStyle) {
    super(context_, attrs, defStyle);
    // TODO Auto-generated constructor stub
}
@Override
public void onDraw(Canvas canvas) {
    paint.setColor(Color.BLACK);
    paint.setStrokeWidth(3);
    //canvas.drawRect(rect, paint);
   // canvas.drawBitmap(bm,new Rect(0,0,100,100),rect,null);

}

public void setRect(int h, int b)
{
    rect.set(130,130,30+b,30+h);

}

public void setChart(XYSeries xys)
{
    mDataset.addSeries(xys);
    lc = new LineChart(mDataset, mRenderer);
    gv= new GraphicalView(context, lc);
    bm = gv.toBitmap();

}

活动:

public class FragActivity extends Activity {

    Canvas canvas = new Canvas();   
    XYSeries currentSeries = new XYSeries("range data series1");
    DrawChartView dcv/* = new DrawChartView(this)*/;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frag);


        dcv = (DrawChartView)findViewById(R.id.SurfaceView01);
        dcv.onDraw(canvas);
        currentSeries.add(11, 11);
        dcv.setChart(currentSeries);
        dcv.onDraw(canvas);
    }


    }

布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<com.cami.robot_chooser.DrawChartView 
    android:id="@+id/SurfaceView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    />

<TextView
    android:id="@+id/show_message1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world"
    android:layout_weight="1"
    />

</LinearLayout>

你能帮忙找出问题的原因以及如何解决问题吗?

LogCat说:

    10-22 12:52:06.115: I/Choreographer(3061): Skipped 71 frames!  The application may be doing too much work on its main thread.
10-22 12:52:06.155: D/gralloc_goldfish(3061): Emulator without GPU emulation detected.
10-22 12:52:08.406: I/Choreographer(3061): Skipped 34 frames!  The application may be doing too much work on its main thread.
10-22 12:52:08.581: D/AndroidRuntime(3061): Shutting down VM
10-22 12:52:08.581: W/dalvikvm(3061): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
10-22 12:52:08.675: E/AndroidRuntime(3061): FATAL EXCEPTION: main
10-22 12:52:08.675: E/AndroidRuntime(3061): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.cami.robot_chooser/com.cami.robot_chooser.FragActivity}: java.lang.NullPointerException
10-22 12:52:08.675: E/AndroidRuntime(3061):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
10-22 12:52:08.675: E/AndroidRuntime(3061):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-22 12:52:08.675: E/AndroidRuntime(3061):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-22 12:52:08.675: E/AndroidRuntime(3061):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-22 12:52:08.675: E/AndroidRuntime(3061):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-22 12:52:08.675: E/AndroidRuntime(3061):     at android.os.Looper.loop(Looper.java:137)
10-22 12:52:08.675: E/AndroidRuntime(3061):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-22 12:52:08.675: E/AndroidRuntime(3061):     at java.lang.reflect.Method.invokeNative(Native Method)
10-22 12:52:08.675: E/AndroidRuntime(3061):     at java.lang.reflect.Method.invoke(Method.java:511)
10-22 12:52:08.675: E/AndroidRuntime(3061):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-22 12:52:08.675: E/AndroidRuntime(3061):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-22 12:52:08.675: E/AndroidRuntime(3061):     at dalvik.system.NativeStart.main(Native Method)
10-22 12:52:08.675: E/AndroidRuntime(3061): Caused by: java.lang.NullPointerException
10-22 12:52:08.675: E/AndroidRuntime(3061):     at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
10-22 12:52:08.675: E/AndroidRuntime(3061):     at android.view.View.<init>(View.java:3228)
10-22 12:52:08.675: E/AndroidRuntime(3061):     at com.cami.robot_chooser.DrawChartView.<init>(DrawChartView.java:36)
10-22 12:52:08.675: E/AndroidRuntime(3061):     at com.cami.robot_chooser.FragActivity.<init>(FragActivity.java:41)
10-22 12:52:08.675: E/AndroidRuntime(3061):     at java.lang.Class.newInstanceImpl(Native Method)
10-22 12:52:08.675: E/AndroidRuntime(3061):     at java.lang.Class.newInstance(Class.java:1319)
10-22 12:52:08.675: E/AndroidRuntime(3061):     at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
10-22 12:52:08.675: E/AndroidRuntime(3061):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
10-22 12:52:08.675: E/AndroidRuntime(3061):     ... 11 more
10-22 12:52:13.545: I/Process(3061): Sending signal. PID: 3061 SIG: 9

3 个答案:

答案 0 :(得分:4)

我遇到类似的问题,aChartEngine图表返回空位图。围绕这个地方有各种提议的解决方案,但它们都没有为我工作(它们在尝试测量必要位图的大小时都失败了)。出于我的目的,位图需要是固定大小,以下代码可以很好地工作:

private Bitmap bitmapFromChartView(View v, int width, int height) {
Bitmap b = Bitmap.createBitmap(width,
        height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, width, height);
v.draw(c);
return b;
}

然后您可以使用以下内容进行调用:

Bitmap x = bitmapFromChartView(myGraphicalViewObject, 500, 500);

希望这有帮助!

答案 1 :(得分:0)

首先,请确保您已添加了AndroidManifest.xml中外部存储空间上保存文件的权限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

如果您有此功能但仍然无效,请查看围绕此主题的讨论herehere

答案 2 :(得分:0)

我也遇到了同样的问题。显然,renderer.setZoomButtonsVisible(bool)方法的实现存在问题。

将此值设置为FALSE,希望这将在某种程度上解决您的问题....你仍然需要弄清楚你要做什么来缩放---但至少应用程序会运行。 :)