android achartengine:尝试将图形导出为图像引发异常
作者:互联网
在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