I need to implement an eraser for an paint application, that has a canvas:
Canvas canvas = new Canvas(bitmap);
Where bitmap is a mutable Bitmap.
I write on the canvas with the following Paint:
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFF000000);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(50);
Then when i want to erase i do the following:
mPaint.setMaskFilter(null);
mPaint.setColor(Color.TRANSPARENT);
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
But it doesn't work. How can i implement a erase without using the porterDuff mode, or how can i change the code so that it will work.
Like this all it does, it draws lines, just like the pen. but the color instead of being black, its more an grey.
EDIT: I tried something else, and i saved the bitmap value in another bitmap, so that when i press erase, i would get the value back. This works and deletes the drawings but the problem is that, after this if i want to draw something, i draw but it disappears. Is this because the bitmap is no the same as the bitmap from:
Canvas canvas = new Canvas(bitmap);
?
So instead of having a layout on which i add my View. I put one parent layout that includes one layout and one imageview.
I put the background image on the imageview. I put an transparent picture on the child layout. and then bring the child layout to front and it is working now
Related
I wanted to apply gray scale effect to a bitmap using brush strokes, i.e. user can use brush to apply gray scale effect to particlar area of bitmap as shown below:
I displayed bitmap on a view using canvas.drawBitmap(...) method. I learnt to draw brush strokes on bitmap using path and paint instances, I just want to apply Gray Scale effect to bitmap on which brush strokes are drawn.
my Paint object is defined as :
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(this.paintStyle);
paint.setStrokeWidth(this.paintStrokeWidth);
paint.setStrokeCap(this.lineCap);
paint.setDither(true);
paint.setStrokeJoin(Paint.Join.MITER);
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);
paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
paint.setAlpha(255);
But it doesn't draw any strokes. If I remove paint.setColorFilter(...) and instead use paint.setColor(...), then i can see strokes on bitmap.
I tried using paint.setXferMode(...) but it didnt work.
In my view's onDraw() i used
canvas.drawPath(path, paint);
Can somebody please point me to the right direction? Thanks in advance!
I am trying to create a Black screen with a transparent Hole in the middle of the screen. Here is what i have tried.
#Override
public void draw(Canvas canvas)
{
Paint myPaint = new Paint();
myPaint.setColor(0xC0000000);
canvas.drawRect(mBlackRect, myPaint);
myPaint = new Paint();
myPaint.setColor(Color.TRANSPARENT);
myPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
canvas.drawRect(mTransparentRect, myPaint);
}
The second paint, shows black color instead of transparent. How can i punch a transparent hole in MY SemiBlack Canvas?
you didn't save the canvas, try the code below
Paint myPaint = new Paint();
int sc = canvas.saveLayer(mBlackRect.left, mBlackRect.top,
mBlackRect.right, mBlackRect.bottom, myPaint,
Canvas.ALL_SAVE_FLAG);
myPaint.setColor(0xC0000000);
canvas.drawRect(mBlackRect, myPaint);
myPaint.setColor(Color.TRANSPARENT);
myPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
canvas.drawRect(mTransparentRect, myPaint);
myPaint.setXfermode(null);
canvas.restoreToCount(sc);
You can not really "punch" a hole by "removing pixels" from something already drawn, at least not with a hardware layer. And if you use a software layer, it will be bad for performance.
What you want to do is draw your shape with an alpha mask applied to your paint. A mask will prevent some parts of the shape to be drawn on the canvas, like cutting a piece of paper and stick it on a wall before spreading the painting.
To apply an alpha mask to your paint, you first need to create a bitmap containing the "hole" shape (programmatically or by loading a custom image from resources), then create a BitmapShader from this bitmap with the proper Xfermode (depending if you want the transparent part in your mask bitmap to be cut out or the non-transparent part) and finally apply this shader to your paint before drawing the semitransparent rectangle or anything you want.
Be careful with performance: only create the Paint object once (do not allocate any object in onDraw() because this method gets called up to 60 times per second on the UI thread), and recreate the alpha mask bitmap only when the bounds of your View/Drawable change (if its dimensions depend on the View dimensions of course, otherwise you just create it once).
I'm sorry if I don't have time to give you ready-to-use code but I think you should find plenty of information about the technique I just described and you can start experimenting and figuring out the solution by yourself which is more rewarding I think ;)
Hello and sorry for my bad English.
On my application, onDraw() method draws 10x10 field with lines using canvas.
Then, in other method I want some cells to be painted in yellow for example.
The code is:
Paint ship = new Paint();
ship.setColor(getResources().getColor(R.color.ship_color));
Canvas canvas = new Canvas();
Rect r = new Rect(x*(rebro_piece),y*rebro_piece, x*(rebro_piece+1), y*(rebro_piece+1));
canvas.drawRect(r, ship);
but nothing happens. What shall I do?
UPD: am I right, that Canvas only draws within onDraw() method and from nothing else?
I would like to draw bitmap(with specified color) on canvas.
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
// create bitmap
canvas.drawBitmap(bitmap, 0, 0, paint);
Well, the bitmap is visible on the canvas, but color of drawable didn't change.
Where is the problem?
I would like to draw bitmap(with specified color) on canvas.
A bitmap contains an image and drawing an image in single color doesn't make any sense. What do you expect it to do? Draw a red rectangle? Shapes can be drawn with color, not images...
The Color attribute of your Paint will be ignored. That Paint parameter is used to pass other settings such as anti-aliasing.
I hope this clarifies.
paint.setColor(Color.RED) is irrelevant. If your image comes with an alpha channel and you want it to be drawn in a single color, use ColorFilter instead:
paint.setColorFilter(new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
I need to draw shapes on an image in my layout. This image needs to be able to change to another image programically, and I also need to draw shapes (rectangles and circles) over top of this image programically. The shapes will also change. I have an existing xml layout and would like to use this layout with the programmed image view in it. What's the easiest way to do this? Would it be possible to see a short example?
I figured out how to do this:
Here's how:
ImageView image = (ImageView) findViewById(R.id.mainImageView);
Bitmap bMap = BitmapFactory.decodeFile(imageFileString);
bMap = bMap.copy(Bitmap.Config.ARGB_8888 , true);
Canvas canvas = new Canvas(bMap);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(1);
paint.setColor(Color.BLUE);
canvas.drawCircle(x, y, radius, paint);
image.setImageBitmap(bMap);
Explanation: The first line gets the ImageView from the layout. I then grab the new image I want displayed using BitmapFactory.decodeFile, where imageFileString is the file location of the new image I want to display. After that I create a new canvas using the bitmap and draw on it. I then display the bitmap to the ImageView using image.setImageBitmap(bMap);.