gpt4 book ai didi

android 图表引擎 : trying to export graph as image throws exception

转载 作者:行者123 更新时间:2023-11-29 21:13:17 25 4
gpt4 key购买 nike

在 android 应用程序中,我试图通过此代码将图形(我使用 achartengine 绘制)导出为位图对象

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 不在屏幕上,则应在 view.layout(...) 之前调用 view.measure(widthMeasureSpec, heightMeasureSpec)。请阅读 View文档。

关于android 图表引擎 : trying to export graph as image throws exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22135536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com