在Android上绘制加载的图像

时间:2016-07-04 17:07:11

标签: java android canvas bitmap imageview

我使用此代码加载图片

Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };

Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();

ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));`

它是工作并在imageview上显示图片而不是我想在它上画一个圆并保存,我可以使用此代码绘制它

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.ic_luncher);
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.imageView);
imageView.setAdjustViewBounds(true);
imageView.setImageBitmap(mutableBitmap);`

但我不能画,因为它设置了' ic_luncher'在位图资源上。 所以用户无法在加载的图片上绘制,我应该替换哪些代码

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_luncher);

1 个答案:

答案 0 :(得分:0)

不要使用Bitmap.copy,而是尝试通过createBitmap创建相同大小的新位图,然后将启动器图标绘制到此新位图。然后你可以在上面画画。