Getting IllegalStateException when trying to take snapshot of the graph for https://github.com/appsthatmatter/GraphView

情到浓时终转凉″ 提交于 2020-01-02 10:02:21

问题


I'm trying to take snapshot of the GraphView but it gives an error "GraphView must be used in hardware accelerated mode." I'm using the following code to take snapshot

Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); 
Canvas canvas = new Canvas(bitmap); 
view.draw(canvas);

Also, i've already set android:hardwareAccelerated="true" at application level.


回答1:


i used this:

dependencies {
  compile fileTree(dir: 'libs', include: ['*.jar'])
  testCompile 'junit:junit:4.12'
 compile 'com.android.support:appcompat-v7:23.1.1'
 compile files('libs/GraphView-4.0.1.jar')
  compile 'com.jjoe64:graphview:4.1.0'
}

especially

compile 'com.jjoe64:graphview:4.1.0'

this helped me.




回答2:


The problem is that GraphView in recent version include additional conditions - if canvas in hardware accelerated mode or not:

GraphView.java (https://github.com/appsthatmatter/GraphView/commit/0740de3e0a93633f7f168115b96edfdce156b19e)

public class GraphView extends View {

    ...

    protected void drawGraphElements(Canvas canvas) {
        // must be in hardware accelerated mode
        if (android.os.Build.VERSION.SDK_INT >= 11 && !canvas.isHardwareAccelerated()) {
            throw new 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");
        }
        ...
    }

    ...
}

When you create off-screen canvas it doesn't have hardware accelerated mode. But you can just override isHardwareAccelerated for your Canvas instance. Use this workaround:

Bitmap bitmap = Bitmap.createBitmap(graphView.getWidth(), graphView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap) {
    @Override
    public boolean isHardwareAccelerated() {
        return true;
    }
};

// not it's work
graphView.draw(canvas);

// save somewhere
bitmap.compress(Bitmap.CompressFormat.JPEG, 100,  new FileOutputStream(imagePath));



回答3:


In the recent release 4.2.2 the snapshot feature is function properly. You can check out the documentation and the example project:

http://www.android-graphview.org/take-snapshots/

This are basically the API calls:

// directly share it
graph.takeSnapshotAndShare(mActivity, "exampleGraph", "GraphViewSnapshot");

// get the bitmap
Bitmap bitmap = graph.takeSnapshot();


来源:https://stackoverflow.com/questions/41920458/getting-illegalstateexception-when-trying-to-take-snapshot-of-the-graph-for-http

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!