在画布上画画

时间:2012-11-01 14:44:12

标签: android paint

我编写的代码在图像上绘制文本工作正常我在potrait模式下捕获图像但是当我以横向模式捕获图像时应用程序崩溃,我得到异常Java.lang.IllegalStateException:不可变位图传递给Canvas构造函数

Canvas canvas = new Canvas(photo);
                    Typeface tf = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
                    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
                    paint.setStyle(Style.FILL);
                    paint.setTypeface(tf);
                    paint.setColor(Color.WHITE);
                    paint.setStrokeWidth(12); 
                    canvas.drawBitmap(photo, 0, 0, paint);
                    canvas.drawText(topaste, 10, 115, paint);
                    image.setImageBitmap(photo);

1 个答案:

答案 0 :(得分:1)

基本上画布对象需要一个新的位图来绘制,传入不可变的图像会使后面的绘制操作失败。以下代码为画布创建新的位图。您需要替换宽度和高度变量以匹配您的用例:

Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
Canvas canvas = new Canvas(photo);