ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android achartengine:尝试将图形导出为图像引发异常

2019-10-13 04:27:42  阅读:205  来源: 互联网

标签:achartengine android


Android应用程序中,正尝试通过此代码将图形(我使用achartengine绘制)导出为Bitmap对象

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类型的对象

但这引发了异常

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

在这条线

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

有人可以帮忙吗?

解决方法:

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.

即这些是布局计算的输入:

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

您需要实际的View大小,即布局计算的结果.

如果此View已经布置在屏幕上,则view.getWidth()和view.getHeight()应该返回其实际大小.在这种情况下,调用view.layout(…)可能会使显示的View处于您应该还原的怪异状态,并且再次要传递实际的尺寸信息,而不是布局参数.

如果此视图不在屏幕上,则应该在view.layout(…)之前调用view.measure(widthMeasureSpec,heightMeasureSpec).请务必阅读View文档.

标签:achartengine,android
来源: https://codeday.me/bug/20191013/1905361.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有