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

试图从视图中获取位图时,Getting always IllegalStateException错误

  •  2
  • nlopez  · 技术社区  · 8 年前

    我试图从GraphView中获取位图,以避免生成两次相同的图形。我不断得到以下错误(尽管硬件加速=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 回复  |  直到 8 年前
        1
  •  1
  •   Probas Probas    8 年前

    尝试以下操作:

    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);
    
        2
  •  0
  •   Abdul Kawee    8 年前

    试着去做

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

    在你的清单文件中。