代码之家  ›  专栏  ›  技术社区  ›  Probas Probas

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

  •  0
  • Probas Probas  · 技术社区  · 8 年前

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

    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);
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   nlopez    8 年前

    您应该尝试以下方法:

    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的图像!!

    希望这能帮助你!!:)