尝试从视图中获取位图时始终出现IllegalStateException错误

时间:2018-02-09 16:00:08

标签: android bitmap android-graphview

我试图从GraphView获取位图,以便不生成两次相同的图形。我不断得到以下错误(尽管有hardwareAccelerated = true):

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

- 这是我尝试这样做的代码:

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);

我尝试了几件事,但没有人成功。我甚至尝试将graphView放在布局中并从布局中获取位图,但这也没有工作(我得到了完全相同的错误)。

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

试试这个:

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);

答案 1 :(得分:0)

尝试做

image.T.gradients()

在您的清单文件中。