尝试从 GraphView 生成图像时出现非法状态异常

Illegal State Exception when trying to generate an image from a GraphView

尽管我已将 hardwareAccelerated 设置为 true,但我尝试从 GrapView 生成图像时出现以下错误:

java.lang.IllegalStateException: GraphView must be used in hardware accelerated mode.You can use android:hardwareAccelerated="true" on your activity. Read this for more info:https://developer.android.com/guide/topics/graphics/hardware-accel.html

这是我收到错误的代码:

graphView.addSeries(RepresentationHelper.getHashMapnewAPI().get(object.getName()));
            graphView.setTitle("");

            //setting y label for bounds with the min and max value obtained from the request
            graphView.getViewport().setYAxisBoundsManual(true);
            double low = graphView.getSeries().get(0).getLowestValueY();
            double high = graphView.getSeries().get(0).getHighestValueY();
            graphView.getViewport().setMinY(low);
            graphView.getViewport().setMaxY(high);

            graphView.getGridLabelRenderer().setHorizontalLabelsVisible(false);
            graphView.getGridLabelRenderer().setVerticalLabelsVisible(false);
            graphView.getGridLabelRenderer().setHighlightZeroLines(false);
            graphView.getGridLabelRenderer().setGridColor(Color.WHITE);

            Bitmap bitmap;
            graphView.setDrawingCacheEnabled(true);
            bitmap = Bitmap.createBitmap(graphView.getDrawingCache());
            graphView.setDrawingCacheEnabled(false);

            ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView);
            //imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.grafico_definitivo));
            imageView.setImageBitmap(bitmap);

你应该尝试这样的事情:

try {
                Bitmap bitmap = Bitmap.createBitmap(graphView.getWidth(), graphView.getHeight(), Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(bitmap) {

                    @Override
                    public boolean isHardwareAccelerated() {
                        return true;
                    }
                };

                graphView.draw(canvas);
                RepresentationHelper.addBitmap(string, bitmap);

                if (firstRequest) {
                    adapter.notifyDataSetChanged();
                    firstRequest = false;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

现在,您在位图上拥有了 graphView 的图像!!

希望对您有所帮助!! :)