安卓achartengine:试图导出图像为图像抛出异常 [英] android achartengine : trying to export graph as image throws exception

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

问题描述

在一个Android应用程序,在尝试导出图通过这个code

in an android app, am attempting to export the graph (which I drew using achartengine) as Bitmap object via this code

public static Bitmap loadBitmapFromView(Context context, View view) {

    Bitmap bitmap;

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPurgeable = true;
    options.inInputShareable = true;

    Bitmap dummy = null;
    try {
        dummy = BitmapFactory.decodeStream(context.getAssets().open("icon_add.png"), new Rect(-1,-1,-1,-1), options);
    } catch (IOException e) {
        e.printStackTrace();
    }
     bitmap = Bitmap.createBitmap(view.getLayoutParams().width,
        view.getLayoutParams().height, Bitmap.Config.ARGB_4444);
    Canvas c = new Canvas(bitmap);
    view.layout(0, 0, view.getLayoutParams().width, view.getLayoutParams().height);
    view.draw(c);
    c = null;

    return bitmap;
}

和我调用这个方法:

loadBitmapFromView(getApplicationContext(), this.graphView);

在这里graphView的类型是GraphicalView

where graphView is the object of type GraphicalView

但这抛出异常

java.lang.IllegalArgumentException: width and height must be > 0

在这行

bitmap = Bitmap.createBitmap(view.getLayoutParams().width,
        view.getLayoutParams().height, Bitmap.Config.ARGB_4444);

有人可以帮忙吗?

can someone please help?

推荐答案

每的 http://developer.android.com/reference/android/view/View.html#getLayoutParams()该方法返回

...布局参数。这些电源参数这种观点的家长   指定应该如何安排。

... layout parameters. These supply parameters to the parent of this view specifying how it should be arranged.

即。这些输入到布局的计算:

i.e. these are inputs to the layout computation:

的LayoutParams ......描述的观点有多大希望是宽度和高度。对于每一个   尺寸,它可以指定一个确切的数字之一,MATCH_PARENT,WR​​AP_CONTENT

LayoutParams ... describes how big the view wants to be for both width and height. For each dimension, it can specify one of an exact number, MATCH_PARENT, WRAP_CONTENT

您需要的实际视图大小,也就是布局计算的结果。

You need the actual View size, that is, the result of the layout computation.

如果这种观点是已经奠定了在屏幕上,则 view.getWidth() view.getHeight()应该回到它的实际大小。在这种情况下,调用 view.layout(...)可以把显示视图在一个奇怪的状态,你应该恢复,并再次要通过实际大小的信息,没有布局PARAMS。

If this View is already laid out on screen, then view.getWidth() and view.getHeight() should return its actual size. In that case, calling view.layout(...) may put the displayed View in a weird state that you should restore, and again you want to pass actual size info, not layout params.

如果这种观点并不在屏幕上,则 view.measure(widthMeasureSpec,heightMeasureSpec)应该是所谓的前 view.layout(。 ..)。请阅读视图文档。

If this View is not on screen, then view.measure(widthMeasureSpec, heightMeasureSpec) is supposed to be called before view.layout(...). Do read the View docs.

这篇关于安卓achartengine:试图导出图像为图像抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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