canvas.drawRect doesn't draw a rectangle on the image
Krishna
Here canvas.drawRect will be different when using different bitmaps. I want to draw a rectangle over the top image and want the part of the image outside the rectangle to be darkened or blurred. please help me.
draw.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(2);
Bitmap bitmap=((BitmapDrawable)image.getDrawable()).getBitmap();
Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.RGB_565, true);
Bitmap bitmap1 = Bitmap.createBitmap(600,400, Bitmap.Config.RGB_565);
Rect r = new Rect();
r.set(100,100,500,300);
Canvas canvas = new Canvas(mutableBitmap);
Canvas canvas1 = new Canvas(bitmap1);
canvas.drawRect(r,paint);
canvas1.drawRect(r,paint);
image.setImageBitmap(mutableBitmap);
image1.setImageBitmap(bitmap1);
}
});
Venkata
If you want to draw a rectangle and darken the outside, do it this way. Create a bitmap and draw a full-sized semi-transparent rectangle, and then draw another rectangle with a full-transparent color inside it. Then put that bitmap on the image.
Try the following code:
Bitmap bitmap = Bitmap.createBitmap(600,400,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
RectF outerRectangle = new RectF(0, 0, 600, 400);
RectF innerRectangle = new RectF(100, 100, 500, 300);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.BLACK);
paint.setAlpha(80);
canvas.drawRect(outerRectangle, paint);
paint.setColor(Color.TRANSPARENT);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
canvas.drawRect(innerRectangle, paint);
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(2);
canvas.drawRect(innerRectangle, paint);
image1.setImageBitmap(bitmap);