问题
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