2

I am trying to obtain the bitmap from a GraphView in order to not generate twice the same graph. I am continously getting the the following error (despite having the hardwareAccelerated=true):

java.lang.IllegalStateException: GraphView must be used in hardware accelerated mode.You can use android:hardwareAccelerated="true" on your activity

-Here is the code where I try to do this:

ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView);
            Bitmap bitmap = Bitmap.createBitmap(graphView.getWidth(), graphView.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            Drawable bgDrawable = graphView.getBackground();
            if (bgDrawable != null) {
                bgDrawable.draw(canvas);
            } else {
                canvas.drawColor(Color.BLUE);
            }

            graphView.draw(canvas);
            imageView.setImageBitmap(bitmap);

I've tried several things but no one with sucess. I've tried even to put the graphView inside a layout and get the bitmap from the layout but this didn't worked either (i get exactly the same error).

Any help would be appreciated!

nlopez
  • 351
  • 1
  • 13
  • Possible duplicated of https://stackoverflow.com/questions/41920458/getting-illegalstateexception-when-trying-to-take-snapshot-of-the-graph-for-http – quant Feb 09 '18 at 16:10

2 Answers2

1

Try this:

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);
            imageView.setImageBitmap(bitmap);
0

try doing

<activity ... />
<activity android:hardwareAccelerated="true" />

in the manifest file of yours.

Abdul Kawee
  • 2,687
  • 1
  • 14
  • 26