正如莫斯指出的,位图需要是可变的。下面是一些您可以使用的源代码:
//first, get bitmap and make it mutable
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
//now, create canvas and paint as you like
Canvas canvas = new Canvas(mutableBitmap);
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStrokeWidth(5);
canvas.drawLine(0, 0, canvas.getWidth(), canvas.getHeight(), paint);
//finally, convert back to icon
Drawable icon = new BitmapDrawable(context.getResources(), mutableBitmap);
getSupportActionBar().setIcon(icon);