我试图在放置为res/drawable/schoolboard.png
的图片上画一个圆圈。图像填充活动背景。以下不起作用:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.schoolboard);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.BLUE);
Canvas canvas = new Canvas(bitmap);
canvas.drawCircle(60, 50, 25, paint);
ImageView imageView = (ImageView)findViewById(R.drawable.schoolboard);
imageView.setAdjustViewBounds(true);
imageView.setImageBitmap(bitmap);
任何帮助都将受到高度赞赏。感谢。
答案 0 :(得分:13)
您的代码中存在一些错误:
首先,你不能在findViewById
中为drawable提供参考ID
所以我认为你的意思是那样的
ImageView imageView = (ImageView)findViewById(R.id.schoolboard_image_view);
schoolboard_image_view
是xml布局中的图片ID(检查您的布局是否有正确的ID)
BitmapFactory.Options myOptions = new BitmapFactory.Options();
myOptions.inDither = true;
myOptions.inScaled = false;
myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important
myOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.schoolboard,myOptions);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.BLUE);
Bitmap workingBitmap = Bitmap.createBitmap(bitmap);
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);
canvas.drawCircle(60, 50, 25, paint);
ImageView imageView = (ImageView)findViewById(R.id.schoolboard_image_view);
imageView.setAdjustViewBounds(true);
imageView.setImageBitmap(mutableBitmap);
请务必使用正确的图片ID:
ImageView imageView =(ImageView)findViewById( R.id.schoolboard_image_view );
答案 1 :(得分:5)
首先,您需要创建一个新的位图,因为BitmapFactory.decodeResource()方法中的位图是不可变的。您可以使用以下代码执行此操作:
Bitmap canvasBitmap = Bitmap.createBitmap([bitmap_width], [bitmap_height], Config.ARGB_8888);
在Canvas构造函数中使用此位图。然后在画布上绘制你的位图。
Canvas canvas = new Canvas(canvasBitmap);
canvas.drawBitmap(bitmap, 0, 0, bitmapPaint);
canvas.drawCircle(60, 50, 25, paint);
R.drawable.schoolboard也不是正确的视图ID。
ImageView imageView =(ImageView)findViewById(R.drawable.schoolboard);