我正试图在放置为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);
任何帮助将受到高度赞赏.谢谢.
解决方法
您的代码中存在一些错误:
首先,你不能在findViewById中为drawable提供参考ID
所以我觉得你的意思是那样的
首先,你不能在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,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);